Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcc0c13

Browse files
committed
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().
1 parent f30bec7 commit dcc0c13

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

Python/pythonrun.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
6060

6161
/* Forward */
6262
static void initmain Py_PROTO((void));
63+
static void initsite Py_PROTO((void));
6364
static PyObject *run_err_node Py_PROTO((node *n, char *filename,
6465
PyObject *globals, PyObject *locals));
6566
static PyObject *run_node Py_PROTO((node *n, char *filename,
@@ -75,6 +76,7 @@ static void call_ll_exitfuncs Py_PROTO((void));
7576
int Py_DebugFlag; /* Needed by parser.c */
7677
int Py_VerboseFlag; /* Needed by import.c */
7778
int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
79+
int Py_NoSiteFlag; /* Suppress 'import site' */
7880
int Py_UseClassExceptionsFlag; /* Needed by bltinmodule.c */
7981

8082
static int initialized = 0;
@@ -107,8 +109,9 @@ Py_Initialize()
107109
PyObject *bimod, *sysmod;
108110
char *p;
109111

110-
if (++initialized > 1)
112+
if (initialized)
111113
return;
114+
initialized = 1;
112115

113116
if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
114117
Py_DebugFlag = 1;
@@ -153,6 +156,8 @@ Py_Initialize()
153156
initsigs(); /* Signal handling stuff, including initintr() */
154157

155158
initmain(); /* Module __main__ */
159+
if (!Py_NoSiteFlag)
160+
initsite(); /* Module site */
156161
}
157162

158163
/* Undo the effect of Py_Initialize().
@@ -177,10 +182,9 @@ Py_Finalize()
177182

178183
call_sys_exitfunc();
179184

180-
if (--initialized > 0)
185+
if (!initialized)
181186
return;
182-
if (initialized < 0)
183-
Py_FatalError("Py_Finalize: not initialized");
187+
initialized = 0;
184188

185189
/* We must call this before the current thread gets removed because
186190
it decrefs class instances, which in turn save and restore the
@@ -291,6 +295,8 @@ Py_NewInterpreter()
291295
PyDict_SetItemString(interp->sysdict, "modules",
292296
interp->modules);
293297
initmain();
298+
if (!Py_NoSiteFlag)
299+
initsite();
294300
}
295301

296302
if (!PyErr_Occurred())
@@ -371,6 +377,31 @@ initmain()
371377
}
372378
}
373379

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);
402+
}
403+
}
404+
374405
/* Parse input from a file and execute it */
375406

376407
int

0 commit comments

Comments
 (0)