diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py index 71f192f90d9a1db..acd91c125e3e89f 100644 --- a/Lib/test/test_repl.py +++ b/Lib/test/test_repl.py @@ -93,6 +93,21 @@ def test_multiline_string_parsing(self): output = kill_python(p) self.assertEqual(p.returncode, 0) + @cpython_only + @unittest.skipIf(sys.platform =="win32", 'Skip Windows') + def test_close_fd_zero(self): + user_input = '''\ + import os + os.close(0) + ''' + user_input = dedent(user_input) + user_input = user_input.encode() + p = spawn_repl() + with SuppressCrashReport(): + p.stdin.write(user_input) + output = kill_python(p) + self.assertEqual(p.returncode, 0) + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-05-31-13-57-36.bpo-40826.TKBXAB.rst b/Misc/NEWS.d/next/Core and Builtins/2020-05-31-13-57-36.bpo-40826.TKBXAB.rst new file mode 100644 index 000000000000000..fff84b830651107 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-05-31-13-57-36.bpo-40826.TKBXAB.rst @@ -0,0 +1,2 @@ +Fix segfaults issue when close file descriptor 0 on REPL. Patch by Dong-hee +Na. diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 8348971c353ba75..3216f408504850c 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -1782,8 +1782,12 @@ PyOS_FiniInterrupts(void) int PyOS_InterruptOccurred(void) { - PyInterpreterState *interp = _PyInterpreterState_GET(); - if (!_Py_ThreadCanHandleSignals(interp)) { + PyThreadState *tstate = _PyThreadState_GET(); + if (tstate == NULL) { + // FIXME: PyGILState_GetThisThreadState doesn't support subinterpreters. + tstate = PyGILState_GetThisThreadState(); + } + if (!_Py_ThreadCanHandleSignals(tstate->interp)) { return 0; }