summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-08-29 22:32:42 (GMT)
committerGuido van Rossum <guido@python.org>1997-08-29 22:32:42 (GMT)
commitdcc0c13f74e3f62b00847f4aec93bb5c65e8cf64 (patch)
treeb7edb23965c3b33321f070a6dfe7f89bf7b53be6 /Python
parentf30bec7bb2d06da0543d435b1edcb983cbd2801b (diff)
downloadcpython-dcc0c13f74e3f62b00847f4aec93bb5c65e8cf64.zip
cpython-dcc0c13f74e3f62b00847f4aec93bb5c65e8cf64.tar.gz
cpython-dcc0c13f74e3f62b00847f4aec93bb5c65e8cf64.tar.bz2
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().
Diffstat (limited to 'Python')
-rw-r--r--Python/pythonrun.c39
1 files changed, 35 insertions, 4 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index f9b33fc..c7832dc 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -60,6 +60,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
/* Forward */
static void initmain Py_PROTO((void));
+static void initsite Py_PROTO((void));
static PyObject *run_err_node Py_PROTO((node *n, char *filename,
PyObject *globals, PyObject *locals));
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_VerboseFlag; /* Needed by import.c */
int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
+int Py_NoSiteFlag; /* Suppress 'import site' */
int Py_UseClassExceptionsFlag; /* Needed by bltinmodule.c */
static int initialized = 0;
@@ -107,8 +109,9 @@ Py_Initialize()
PyObject *bimod, *sysmod;
char *p;
- if (++initialized > 1)
+ if (initialized)
return;
+ initialized = 1;
if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
Py_DebugFlag = 1;
@@ -153,6 +156,8 @@ Py_Initialize()
initsigs(); /* Signal handling stuff, including initintr() */
initmain(); /* Module __main__ */
+ if (!Py_NoSiteFlag)
+ initsite(); /* Module site */
}
/* Undo the effect of Py_Initialize().
@@ -177,10 +182,9 @@ Py_Finalize()
call_sys_exitfunc();
- if (--initialized > 0)
+ if (!initialized)
return;
- if (initialized < 0)
- Py_FatalError("Py_Finalize: not initialized");
+ initialized = 0;
/* We must call this before the current thread gets removed because
it decrefs class instances, which in turn save and restore the
@@ -291,6 +295,8 @@ Py_NewInterpreter()
PyDict_SetItemString(interp->sysdict, "modules",
interp->modules);
initmain();
+ if (!Py_NoSiteFlag)
+ initsite();
}
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 */
int