comparison Python/pythonrun.c @ 6201:dc974b3a6cf4 legacy-trunk

Two independent changes (oops): - Changed semantics for initialized flag (again); forget the ref counting, forget the fatal errors -- redundant calls to Py_Initialize() or Py_Finalize() calls are simply ignored. - Automatically import site.py on initialization, unless a flag is set not to do this by main().
author Guido van Rossum <guido@python.org>
date Fri, 29 Aug 1997 22:32:42 +0000
parents 7da7ba5c6525
children 4cf20fa883a9
comparison
equal deleted inserted replaced
6200:c57cede3c7c0 6201:dc974b3a6cf4
58 58
59 extern grammar _PyParser_Grammar; /* From graminit.c */ 59 extern grammar _PyParser_Grammar; /* From graminit.c */
60 60
61 /* Forward */ 61 /* Forward */
62 static void initmain Py_PROTO((void)); 62 static void initmain Py_PROTO((void));
63 static void initsite Py_PROTO((void));
63 static PyObject *run_err_node Py_PROTO((node *n, char *filename, 64 static PyObject *run_err_node Py_PROTO((node *n, char *filename,
64 PyObject *globals, PyObject *locals)); 65 PyObject *globals, PyObject *locals));
65 static PyObject *run_node Py_PROTO((node *n, char *filename, 66 static PyObject *run_node Py_PROTO((node *n, char *filename,
66 PyObject *globals, PyObject *locals)); 67 PyObject *globals, PyObject *locals));
67 static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename, 68 static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename,
73 static void call_ll_exitfuncs Py_PROTO((void)); 74 static void call_ll_exitfuncs Py_PROTO((void));
74 75
75 int Py_DebugFlag; /* Needed by parser.c */ 76 int Py_DebugFlag; /* Needed by parser.c */
76 int Py_VerboseFlag; /* Needed by import.c */ 77 int Py_VerboseFlag; /* Needed by import.c */
77 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ 78 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
79 int Py_NoSiteFlag; /* Suppress 'import site' */
78 int Py_UseClassExceptionsFlag; /* Needed by bltinmodule.c */ 80 int Py_UseClassExceptionsFlag; /* Needed by bltinmodule.c */
79 81
80 static int initialized = 0; 82 static int initialized = 0;
81 83
82 /* API to access the initialized flag -- useful for eroteric use */ 84 /* API to access the initialized flag -- useful for eroteric use */
105 PyInterpreterState *interp; 107 PyInterpreterState *interp;
106 PyThreadState *tstate; 108 PyThreadState *tstate;
107 PyObject *bimod, *sysmod; 109 PyObject *bimod, *sysmod;
108 char *p; 110 char *p;
109 111
110 if (++initialized > 1) 112 if (initialized)
111 return; 113 return;
114 initialized = 1;
112 115
113 if ((p = getenv("PYTHONDEBUG")) && *p != '\0') 116 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
114 Py_DebugFlag = 1; 117 Py_DebugFlag = 1;
115 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0') 118 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
116 Py_VerboseFlag = 1; 119 Py_VerboseFlag = 1;
151 _PyImport_Init(); 154 _PyImport_Init();
152 155
153 initsigs(); /* Signal handling stuff, including initintr() */ 156 initsigs(); /* Signal handling stuff, including initintr() */
154 157
155 initmain(); /* Module __main__ */ 158 initmain(); /* Module __main__ */
159 if (!Py_NoSiteFlag)
160 initsite(); /* Module site */
156 } 161 }
157 162
158 /* Undo the effect of Py_Initialize(). 163 /* Undo the effect of Py_Initialize().
159 164
160 Beware: if multiple interpreter and/or thread states exist, these 165 Beware: if multiple interpreter and/or thread states exist, these
175 PyInterpreterState *interp; 180 PyInterpreterState *interp;
176 PyThreadState *tstate; 181 PyThreadState *tstate;
177 182
178 call_sys_exitfunc(); 183 call_sys_exitfunc();
179 184
180 if (--initialized > 0) 185 if (!initialized)
181 return; 186 return;
182 if (initialized < 0) 187 initialized = 0;
183 Py_FatalError("Py_Finalize: not initialized");
184 188
185 /* We must call this before the current thread gets removed because 189 /* We must call this before the current thread gets removed because
186 it decrefs class instances, which in turn save and restore the 190 it decrefs class instances, which in turn save and restore the
187 current error state, which is a per thread data structure. 191 current error state, which is a per thread data structure.
188 */ 192 */
289 Py_INCREF(interp->sysdict); 293 Py_INCREF(interp->sysdict);
290 PySys_SetPath(Py_GetPath()); 294 PySys_SetPath(Py_GetPath());
291 PyDict_SetItemString(interp->sysdict, "modules", 295 PyDict_SetItemString(interp->sysdict, "modules",
292 interp->modules); 296 interp->modules);
293 initmain(); 297 initmain();
298 if (!Py_NoSiteFlag)
299 initsite();
294 } 300 }
295 301
296 if (!PyErr_Occurred()) 302 if (!PyErr_Occurred())
297 return tstate; 303 return tstate;
298 304
366 d = PyModule_GetDict(m); 372 d = PyModule_GetDict(m);
367 if (PyDict_GetItemString(d, "__builtins__") == NULL) { 373 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
368 if (PyDict_SetItemString(d, "__builtins__", 374 if (PyDict_SetItemString(d, "__builtins__",
369 PyEval_GetBuiltins())) 375 PyEval_GetBuiltins()))
370 Py_FatalError("can't add __builtins__ to __main__"); 376 Py_FatalError("can't add __builtins__ to __main__");
377 }
378 }
379
380 /* Import the site module (not into __main__ though) */
381
382 static void
383 initsite()
384 {
385 PyObject *m, *f;
386 m = PyImport_ImportModule("site");
387 if (m == NULL) {
388 f = PySys_GetObject("stderr");
389 if (Py_VerboseFlag) {
390 PyFile_WriteString(
391 "'import site' failed; traceback:\n", f);
392 PyErr_Print();
393 }
394 else {
395 PyFile_WriteString(
396 "'import site' failed; use -v for traceback\n", f);
397 PyErr_Clear();
398 }
399 }
400 else {
401 Py_DECREF(m);
371 } 402 }
372 } 403 }
373 404
374 /* Parse input from a file and execute it */ 405 /* Parse input from a file and execute it */
375 406