Skip to content

Commit ea16429

Browse files
author
Victor Stinner
committed
Partial backport of r78826: leave import site error handler unchanged (print
the error and continue). Merged revisions 78826-78827 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78826 | victor.stinner | 2010-03-10 23:30:19 +0100 (mer., 10 mars 2010) | 5 lines Issue python#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. ........ r78827 | victor.stinner | 2010-03-10 23:45:04 +0100 (mer., 10 mars 2010) | 4 lines ooops, fix error message in execusercustomize() Copy/paste failure :-) ........
1 parent 57d43b2 commit ea16429

5 files changed

Lines changed: 39 additions & 11 deletions

File tree

‎Lib/site.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ def execsitecustomize():
472472
import sitecustomize
473473
except ImportError:
474474
pass
475+
except Exception:
476+
if sys.flags.verbose:
477+
sys.excepthook(*sys.exc_info())
478+
else:
479+
print >>sys.stderr, \
480+
"'import sitecustomize' failed; use -v for traceback"
475481

476482

477483
def execusercustomize():
@@ -480,6 +486,12 @@ def execusercustomize():
480486
import usercustomize
481487
except ImportError:
482488
pass
489+
except Exception:
490+
if sys.flags.verbose:
491+
sys.excepthook(*sys.exc_info())
492+
else:
493+
print>>sys.stderr, \
494+
"'import usercustomize' failed; use -v for traceback"
483495

484496

485497
def main():

‎Modules/main.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,16 @@ Py_Main(int argc, char **argv)
569569
}
570570

571571
if (sts==-1) {
572-
sts = PyRun_AnyFileExFlags(
573-
fp,
574-
filename == NULL ? "<stdin>" : filename,
575-
filename != NULL, &cf) != 0;
572+
/* call pending calls like signal handlers (SIGINT) */
573+
if (Py_MakePendingCalls() == -1) {
574+
PyErr_Print();
575+
sts = 1;
576+
} else {
577+
sts = PyRun_AnyFileExFlags(
578+
fp,
579+
filename == NULL ? "<stdin>" : filename,
580+
filename != NULL, &cf) != 0;
581+
}
576582
}
577583

578584
}

‎Parser/tokenizer.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,12 @@ tok_stdin_decode(struct tok_state *tok, char **inp)
764764
return -1;
765765

766766
error_clear:
767-
/* Fallback to iso-8859-1: for backward compatibility */
768767
Py_DECREF(enc);
768+
if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
769+
tok->done = E_ERROR;
770+
return -1;
771+
}
772+
/* Fallback to iso-8859-1: for backward compatibility */
769773
PyErr_Clear();
770774
return 0;
771775
}

‎Python/import.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,8 +2744,6 @@ PyImport_Import(PyObject *module_name)
27442744
}
27452745
else {
27462746
/* No globals -- use standard builtins, and fake globals */
2747-
PyErr_Clear();
2748-
27492747
builtins = PyImport_ImportModuleLevel("__builtin__",
27502748
NULL, NULL, NULL, 0);
27512749
if (builtins == NULL)

‎Python/pythonrun.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,15 @@ Py_InitializeEx(int install_sigs)
245245
}
246246

247247
initmain(); /* Module __main__ */
248-
if (!Py_NoSiteFlag)
249-
initsite(); /* Module site */
250248

251249
/* auto-thread-state API, if available */
252250
#ifdef WITH_THREAD
253251
_PyGILState_Init(interp, tstate);
254252
#endif /* WITH_THREAD */
255253

254+
if (!Py_NoSiteFlag)
255+
initsite(); /* Module site */
256+
256257
if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
257258
p = icodeset = codeset = strdup(p);
258259
free_codeset = 1;
@@ -281,8 +282,13 @@ Py_InitializeEx(int install_sigs)
281282
loc_codeset = strdup(loc_codeset);
282283
Py_DECREF(enc);
283284
} else {
284-
loc_codeset = NULL;
285-
PyErr_Clear();
285+
if (PyErr_ExceptionMatches(PyExc_LookupError)) {
286+
PyErr_Clear();
287+
loc_codeset = NULL;
288+
} else {
289+
PyErr_Print();
290+
exit(1);
291+
}
286292
}
287293
} else
288294
loc_codeset = NULL;
@@ -1543,6 +1549,8 @@ err_input(perrdetail *err)
15431549
char *msg = NULL;
15441550
errtype = PyExc_SyntaxError;
15451551
switch (err->error) {
1552+
case E_ERROR:
1553+
return;
15461554
case E_SYNTAX:
15471555
errtype = PyExc_IndentationError;
15481556
if (err->expected == INDENT)

0 commit comments

Comments
 (0)