Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt
(SIGINT). If an error occurs while importing the site module, the error is
printed and Python exits. Initialize the GIL before importing the site
module.
diff --git a/Lib/site.py b/Lib/site.py
index 612122e..02129aa 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -489,6 +489,12 @@
import sitecustomize
except ImportError:
pass
+ except Exception:
+ if sys.flags.verbose:
+ sys.excepthook(*sys.exc_info())
+ else:
+ print >>sys.stderr, \
+ "'import sitecustomize' failed; use -v for traceback"
def execusercustomize():
@@ -497,6 +503,12 @@
import usercustomize
except ImportError:
pass
+ except Exception:
+ if sys.flags.verbose:
+ sys.excepthook(*sys.exc_info())
+ else:
+ print>>sys.stderr, \
+ "'import sitecustomize' failed; use -v for traceback"
def main():
diff --git a/Misc/NEWS b/Misc/NEWS
index 611f187..85fe3c0 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,11 @@
Core and Builtins
-----------------
+- Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt
+ (SIGINT). If an error occurs while importing the site module, the error is
+ printed and Python exits. Initialize the GIL before importing the site
+ module.
+
Library
-------
diff --git a/Modules/main.c b/Modules/main.c
index 1511dd9..7f98ed0 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -573,10 +573,16 @@
}
if (sts==-1) {
- sts = PyRun_AnyFileExFlags(
- fp,
- filename == NULL ? "<stdin>" : filename,
- filename != NULL, &cf) != 0;
+ /* call pending calls like signal handlers (SIGINT) */
+ if (Py_MakePendingCalls() == -1) {
+ PyErr_Print();
+ sts = 1;
+ } else {
+ sts = PyRun_AnyFileExFlags(
+ fp,
+ filename == NULL ? "<stdin>" : filename,
+ filename != NULL, &cf) != 0;
+ }
}
}
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index b881e7c..d60b256 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -817,8 +817,12 @@
return -1;
error_clear:
- /* Fallback to iso-8859-1: for backward compatibility */
Py_DECREF(enc);
+ if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
+ tok->done = E_ERROR;
+ return -1;
+ }
+ /* Fallback to iso-8859-1: for backward compatibility */
PyErr_Clear();
return 0;
}
diff --git a/Python/import.c b/Python/import.c
index eb50bc5..bcdd68e 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -2748,8 +2748,6 @@
}
else {
/* No globals -- use standard builtins, and fake globals */
- PyErr_Clear();
-
builtins = PyImport_ImportModuleLevel("__builtin__",
NULL, NULL, NULL, 0);
if (builtins == NULL)
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 4f8417a..ada1481 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -248,14 +248,15 @@
}
initmain(); /* Module __main__ */
- if (!Py_NoSiteFlag)
- initsite(); /* Module site */
/* auto-thread-state API, if available */
#ifdef WITH_THREAD
_PyGILState_Init(interp, tstate);
#endif /* WITH_THREAD */
+ if (!Py_NoSiteFlag)
+ initsite(); /* Module site */
+
if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
p = icodeset = codeset = strdup(p);
free_codeset = 1;
@@ -284,8 +285,13 @@
loc_codeset = strdup(loc_codeset);
Py_DECREF(enc);
} else {
- loc_codeset = NULL;
- PyErr_Clear();
+ if (PyErr_ExceptionMatches(PyExc_LookupError)) {
+ PyErr_Clear();
+ loc_codeset = NULL;
+ } else {
+ PyErr_Print();
+ exit(1);
+ }
}
} else
loc_codeset = NULL;
@@ -704,20 +710,12 @@
static void
initsite(void)
{
- PyObject *m, *f;
+ PyObject *m;
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();
- }
+ PyErr_Print();
+ Py_Finalize();
+ exit(1);
}
else {
Py_DECREF(m);
@@ -1546,6 +1544,8 @@
char *msg = NULL;
errtype = PyExc_SyntaxError;
switch (err->error) {
+ case E_ERROR:
+ return;
case E_SYNTAX:
errtype = PyExc_IndentationError;
if (err->expected == INDENT)