From 5f02174269f29c65010ddabeeca14ca2560075be Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sun, 31 May 2020 12:30:45 +0900 Subject: [PATCH 1/2] bpo-40826: Update PyOS_InterruptOccurred to handle tstate is NULL --- Lib/test/test_repl.py | 15 +++++++++++++++ .../2020-05-31-13-57-36.bpo-40826.TKBXAB.rst | 2 ++ Modules/signalmodule.c | 8 ++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-05-31-13-57-36.bpo-40826.TKBXAB.rst diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py index 71f192f90d9a1d..74b3b7e621cddc 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", '') + 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 00000000000000..fff84b83065110 --- /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 8348971c353ba7..3216f408504850 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; } From 9524994d03c853607489bdeb96ecb37b0e4b444e Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sun, 31 May 2020 14:52:03 +0900 Subject: [PATCH 2/2] bpo-40826: Update --- Lib/test/test_repl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py index 74b3b7e621cddc..acd91c125e3e89 100644 --- a/Lib/test/test_repl.py +++ b/Lib/test/test_repl.py @@ -94,7 +94,7 @@ def test_multiline_string_parsing(self): self.assertEqual(p.returncode, 0) @cpython_only - @unittest.skipIf(sys.platform =="win32", '') + @unittest.skipIf(sys.platform =="win32", 'Skip Windows') def test_close_fd_zero(self): user_input = '''\ import os