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().
This commit is contained in:
Guido van Rossum 1997-08-29 22:32:42 +00:00
parent f30bec7bb2
commit dcc0c13f74

View file

@ -60,6 +60,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
/* Forward */ /* Forward */
static void initmain Py_PROTO((void)); static void initmain Py_PROTO((void));
static void initsite Py_PROTO((void));
static PyObject *run_err_node Py_PROTO((node *n, char *filename, static PyObject *run_err_node Py_PROTO((node *n, char *filename,
PyObject *globals, PyObject *locals)); PyObject *globals, PyObject *locals));
static PyObject *run_node Py_PROTO((node *n, char *filename, static PyObject *run_node Py_PROTO((node *n, char *filename,
@ -75,6 +76,7 @@ static void call_ll_exitfuncs Py_PROTO((void));
int Py_DebugFlag; /* Needed by parser.c */ int Py_DebugFlag; /* Needed by parser.c */
int Py_VerboseFlag; /* Needed by import.c */ int Py_VerboseFlag; /* Needed by import.c */
int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
int Py_NoSiteFlag; /* Suppress 'import site' */
int Py_UseClassExceptionsFlag; /* Needed by bltinmodule.c */ int Py_UseClassExceptionsFlag; /* Needed by bltinmodule.c */
static int initialized = 0; static int initialized = 0;
@ -107,8 +109,9 @@ Py_Initialize()
PyObject *bimod, *sysmod; PyObject *bimod, *sysmod;
char *p; char *p;
if (++initialized > 1) if (initialized)
return; return;
initialized = 1;
if ((p = getenv("PYTHONDEBUG")) && *p != '\0') if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
Py_DebugFlag = 1; Py_DebugFlag = 1;
@ -153,6 +156,8 @@ Py_Initialize()
initsigs(); /* Signal handling stuff, including initintr() */ initsigs(); /* Signal handling stuff, including initintr() */
initmain(); /* Module __main__ */ initmain(); /* Module __main__ */
if (!Py_NoSiteFlag)
initsite(); /* Module site */
} }
/* Undo the effect of Py_Initialize(). /* Undo the effect of Py_Initialize().
@ -177,10 +182,9 @@ Py_Finalize()
call_sys_exitfunc(); call_sys_exitfunc();
if (--initialized > 0) if (!initialized)
return; return;
if (initialized < 0) initialized = 0;
Py_FatalError("Py_Finalize: not initialized");
/* We must call this before the current thread gets removed because /* We must call this before the current thread gets removed because
it decrefs class instances, which in turn save and restore the it decrefs class instances, which in turn save and restore the
@ -291,6 +295,8 @@ Py_NewInterpreter()
PyDict_SetItemString(interp->sysdict, "modules", PyDict_SetItemString(interp->sysdict, "modules",
interp->modules); interp->modules);
initmain(); initmain();
if (!Py_NoSiteFlag)
initsite();
} }
if (!PyErr_Occurred()) if (!PyErr_Occurred())
@ -371,6 +377,31 @@ initmain()
} }
} }
/* Import the site module (not into __main__ though) */
static void
initsite()
{
PyObject *m, *f;
m = PyImport_ImportModule("site");
if (m == NULL) {
f = PySys_GetObject("stderr");
if (Py_VerboseFlag) {
PyFile_WriteString(
"'import site' failed; traceback:\n", f);
PyErr_Print();
}
else {
PyFile_WriteString(
"'import site' failed; use -v for traceback\n", f);
PyErr_Clear();
}
}
else {
Py_DECREF(m);
}
}
/* Parse input from a file and execute it */ /* Parse input from a file and execute it */
int int