@@ -589,8 +589,13 @@ parse_exit_code(PyObject *code, int *exitcode_p)
589
589
}
590
590
591
591
int
592
- _Py_HandleSystemExit (int * exitcode_p )
592
+ _Py_HandleSystemExitAndKeyboardInterrupt (int * exitcode_p )
593
593
{
594
+ if (PyErr_ExceptionMatches (PyExc_KeyboardInterrupt )) {
595
+ _Py_atomic_store_int (& _PyRuntime .signals .unhandled_keyboard_interrupt , 1 );
596
+ return 0 ;
597
+ }
598
+
594
599
int inspect = _Py_GetConfig ()-> inspect ;
595
600
if (inspect ) {
596
601
/* Don't exit if -i flag was given. This flag is set to 0
@@ -646,7 +651,7 @@ static void
646
651
handle_system_exit (void )
647
652
{
648
653
int exitcode ;
649
- if (_Py_HandleSystemExit (& exitcode )) {
654
+ if (_Py_HandleSystemExitAndKeyboardInterrupt (& exitcode )) {
650
655
Py_Exit (exitcode );
651
656
}
652
657
}
@@ -1105,8 +1110,6 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
1105
1110
}
1106
1111
}
1107
1112
1108
- int unhandled_keyboard_interrupt = _PyRuntime .signals .unhandled_keyboard_interrupt ;
1109
-
1110
1113
// Try first with the stdlib traceback module
1111
1114
PyObject * print_exception_fn = PyImport_ImportModuleAttrString (
1112
1115
"traceback" ,
@@ -1120,11 +1123,9 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
1120
1123
Py_XDECREF (print_exception_fn );
1121
1124
if (result ) {
1122
1125
Py_DECREF (result );
1123
- _PyRuntime .signals .unhandled_keyboard_interrupt = unhandled_keyboard_interrupt ;
1124
1126
return ;
1125
1127
}
1126
1128
fallback :
1127
- _PyRuntime .signals .unhandled_keyboard_interrupt = unhandled_keyboard_interrupt ;
1128
1129
#ifdef Py_DEBUG
1129
1130
if (PyErr_Occurred ()) {
1130
1131
PyErr_FormatUnraisable (
@@ -1297,20 +1298,6 @@ flush_io(void)
1297
1298
static PyObject *
1298
1299
run_eval_code_obj (PyThreadState * tstate , PyCodeObject * co , PyObject * globals , PyObject * locals )
1299
1300
{
1300
- PyObject * v ;
1301
- /*
1302
- * We explicitly re-initialize _Py_UnhandledKeyboardInterrupt every eval
1303
- * _just in case_ someone is calling into an embedded Python where they
1304
- * don't care about an uncaught KeyboardInterrupt exception (why didn't they
1305
- * leave config.install_signal_handlers set to 0?!?) but then later call
1306
- * Py_Main() itself (which _checks_ this flag and dies with a signal after
1307
- * its interpreter exits). We don't want a previous embedded interpreter's
1308
- * uncaught exception to trigger an unexplained signal exit from a future
1309
- * Py_Main() based one.
1310
- */
1311
- // XXX Isn't this dealt with by the move to _PyRuntimeState?
1312
- _PyRuntime .signals .unhandled_keyboard_interrupt = 0 ;
1313
-
1314
1301
/* Set globals['__builtins__'] if it doesn't exist */
1315
1302
if (!globals || !PyDict_Check (globals )) {
1316
1303
PyErr_SetString (PyExc_SystemError , "globals must be a real dict" );
@@ -1328,11 +1315,7 @@ run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, Py
1328
1315
}
1329
1316
}
1330
1317
1331
- v = PyEval_EvalCode ((PyObject * )co , globals , locals );
1332
- if (!v && _PyErr_Occurred (tstate ) == PyExc_KeyboardInterrupt ) {
1333
- _PyRuntime .signals .unhandled_keyboard_interrupt = 1 ;
1334
- }
1335
- return v ;
1318
+ return PyEval_EvalCode ((PyObject * )co , globals , locals );
1336
1319
}
1337
1320
1338
1321
static PyObject *
0 commit comments