Skip to content

Commit 5f02174

Browse files
committed
bpo-40826: Update PyOS_InterruptOccurred to handle tstate is NULL
1 parent 7b78e7f commit 5f02174

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Lib/test/test_repl.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ def test_multiline_string_parsing(self):
9393
output = kill_python(p)
9494
self.assertEqual(p.returncode, 0)
9595

96+
@cpython_only
97+
@unittest.skipIf(sys.platform =="win32", '')
98+
def test_close_fd_zero(self):
99+
user_input = '''\
100+
import os
101+
os.close(0)
102+
'''
103+
user_input = dedent(user_input)
104+
user_input = user_input.encode()
105+
p = spawn_repl()
106+
with SuppressCrashReport():
107+
p.stdin.write(user_input)
108+
output = kill_python(p)
109+
self.assertEqual(p.returncode, 0)
110+
96111

97112
if __name__ == "__main__":
98113
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix segfaults issue when close file descriptor 0 on REPL. Patch by Dong-hee
2+
Na.

Modules/signalmodule.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,8 +1782,12 @@ PyOS_FiniInterrupts(void)
17821782
int
17831783
PyOS_InterruptOccurred(void)
17841784
{
1785-
PyInterpreterState *interp = _PyInterpreterState_GET();
1786-
if (!_Py_ThreadCanHandleSignals(interp)) {
1785+
PyThreadState *tstate = _PyThreadState_GET();
1786+
if (tstate == NULL) {
1787+
// FIXME: PyGILState_GetThisThreadState doesn't support subinterpreters.
1788+
tstate = PyGILState_GetThisThreadState();
1789+
}
1790+
if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
17871791
return 0;
17881792
}
17891793

0 commit comments

Comments
 (0)