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

Skip to content

Commit 52f6dd7

Browse files
author
Victor Stinner
committed
Merged revisions 78826 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r78826 | victor.stinner | 2010-03-10 23:30:19 +0100 (mer., 10 mars 2010) | 5 lines 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. ........
1 parent 2743139 commit 52f6dd7

6 files changed

Lines changed: 50 additions & 32 deletions

File tree

Lib/site.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,12 @@ def execsitecustomize():
489489
pass
490490
except Exception as err:
491491
if os.environ.get("PYTHONVERBOSE"):
492-
raise
493-
sys.stderr.write(
494-
"Error in sitecustomize; set PYTHONVERBOSE for traceback:\n"
495-
"%s: %s\n" %
496-
(err.__class__.__name__, err))
492+
sys.excepthook(*sys.exc_info())
493+
else:
494+
sys.stderr.write(
495+
"Error in sitecustomize; set PYTHONVERBOSE for traceback:\n"
496+
"%s: %s\n" %
497+
(err.__class__.__name__, err))
497498

498499

499500
def execusercustomize():
@@ -502,6 +503,14 @@ def execusercustomize():
502503
import usercustomize
503504
except ImportError:
504505
pass
506+
except Exception as err:
507+
if os.environ.get("PYTHONVERBOSE"):
508+
sys.excepthook(*sys.exc_info())
509+
else:
510+
sys.stderr.write(
511+
"Error in usercustomize; set PYTHONVERBOSE for traceback:\n"
512+
"%s: %s\n" %
513+
(err.__class__.__name__, err))
505514

506515

507516
def main():

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt
16+
(SIGINT). If an error occurs while importing the site module, the error is
17+
printed and Python exits. Initialize the GIL before importing the site
18+
module.
19+
1520
- Issue #7173: Generator finalization could invalidate sys.exc_info().
1621

1722
- Issue #7544: Preallocate thread memory before creating the thread to avoid

Modules/main.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,16 @@ Py_Main(int argc, wchar_t **argv)
595595
else
596596
p_cfilename = "<decoding error>";
597597
}
598-
sts = PyRun_AnyFileExFlags(
599-
fp,
600-
p_cfilename,
601-
filename != NULL, &cf) != 0;
598+
/* call pending calls like signal handlers (SIGINT) */
599+
if (Py_MakePendingCalls() == -1) {
600+
PyErr_Print();
601+
sts = 1;
602+
} else {
603+
sts = PyRun_AnyFileExFlags(
604+
fp,
605+
p_cfilename,
606+
filename != NULL, &cf) != 0;
607+
}
602608
Py_XDECREF(filenameObj);
603609
}
604610

Parser/tokenizer.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,21 +1242,28 @@ indenterror(struct tok_state *tok)
12421242
}
12431243

12441244
#ifdef PGEN
1245-
#define verify_identifier(s,e) 1
1245+
#define verify_identifier(tok) 1
12461246
#else
12471247
/* Verify that the identifier follows PEP 3131. */
12481248
static int
1249-
verify_identifier(char *start, char *end)
1249+
verify_identifier(struct tok_state *tok)
12501250
{
12511251
PyObject *s;
12521252
int result;
1253-
s = PyUnicode_DecodeUTF8(start, end-start, NULL);
1253+
s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL);
12541254
if (s == NULL) {
1255-
PyErr_Clear();
1255+
if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
1256+
PyErr_Clear();
1257+
tok->done = E_IDENTIFIER;
1258+
} else {
1259+
tok->done = E_ERROR;
1260+
}
12561261
return 0;
12571262
}
12581263
result = PyUnicode_IsIdentifier(s);
12591264
Py_DECREF(s);
1265+
if (result == 0)
1266+
tok->done = E_IDENTIFIER;
12601267
return result;
12611268
}
12621269
#endif
@@ -1405,7 +1412,7 @@ tok_get(register struct tok_state *tok, char **p_start, char **p_end)
14051412
}
14061413
tok_backup(tok, c);
14071414
if (nonascii &&
1408-
!verify_identifier(tok->start, tok->cur)) {
1415+
!verify_identifier(tok)) {
14091416
tok->done = E_IDENTIFIER;
14101417
return ERRORTOKEN;
14111418
}

Python/import.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,8 +2774,6 @@ PyImport_Import(PyObject *module_name)
27742774
}
27752775
else {
27762776
/* No globals -- use standard builtins, and fake globals */
2777-
PyErr_Clear();
2778-
27792777
builtins = PyImport_ImportModuleLevel("builtins",
27802778
NULL, NULL, NULL, 0);
27812779
if (builtins == NULL)

Python/pythonrun.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,14 @@ Py_InitializeEx(int install_sigs)
296296
if (initstdio() < 0)
297297
Py_FatalError(
298298
"Py_Initialize: can't initialize sys standard streams");
299-
if (!Py_NoSiteFlag)
300-
initsite(); /* Module site */
301299

302300
/* auto-thread-state API, if available */
303301
#ifdef WITH_THREAD
304302
_PyGILState_Init(interp, tstate);
305303
#endif /* WITH_THREAD */
304+
305+
if (!Py_NoSiteFlag)
306+
initsite(); /* Module site */
306307
}
307308

308309
void
@@ -711,22 +712,12 @@ initmain(void)
711712
static void
712713
initsite(void)
713714
{
714-
PyObject *m, *f;
715+
PyObject *m;
715716
m = PyImport_ImportModule("site");
716717
if (m == NULL) {
717-
f = PySys_GetObject("stderr");
718-
if (f == NULL || f == Py_None)
719-
return;
720-
if (Py_VerboseFlag) {
721-
PyFile_WriteString(
722-
"'import site' failed; traceback:\n", f);
723-
PyErr_Print();
724-
}
725-
else {
726-
PyFile_WriteString(
727-
"'import site' failed; use -v for traceback\n", f);
728-
PyErr_Clear();
729-
}
718+
PyErr_Print();
719+
Py_Finalize();
720+
exit(1);
730721
}
731722
else {
732723
Py_DECREF(m);
@@ -1907,6 +1898,8 @@ err_input(perrdetail *err)
19071898
char *msg = NULL;
19081899
errtype = PyExc_SyntaxError;
19091900
switch (err->error) {
1901+
case E_ERROR:
1902+
return;
19101903
case E_SYNTAX:
19111904
errtype = PyExc_IndentationError;
19121905
if (err->expected == INDENT)

0 commit comments

Comments
 (0)