Skip to content

Commit fa7ab6a

Browse files
authored
bpo-40826: Add _PyOS_InterruptOccurred(tstate) function (GH-20599)
my_fgets() now calls _PyOS_InterruptOccurred(tstate) to check for pending signals, rather calling PyOS_InterruptOccurred(). my_fgets() is called with the GIL released, whereas PyOS_InterruptOccurred() must be called with the GIL held. test_repl: use text=True and avoid SuppressCrashReport in test_multiline_string_parsing(). Fix my_fgets() on Windows: fgets(fp) does crash if fileno(fp) is closed.
1 parent 18a9024 commit fa7ab6a

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

Include/internal/pycore_pystate.h

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ PyAPI_FUNC(int) _PyState_AddModule(
144144
PyObject* module,
145145
struct PyModuleDef* def);
146146

147+
148+
PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate);
149+
147150
#ifdef __cplusplus
148151
}
149152
#endif

Lib/test/test_repl.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
2929
# test.support.script_helper.
3030
env = kw.setdefault('env', dict(os.environ))
3131
env['TERM'] = 'vt100'
32-
return subprocess.Popen(cmd_line, executable=sys.executable,
32+
return subprocess.Popen(cmd_line,
33+
executable=sys.executable,
34+
text=True,
3335
stdin=subprocess.PIPE,
3436
stdout=stdout, stderr=stderr,
3537
**kw)
@@ -49,12 +51,11 @@ def test_no_memory(self):
4951
sys.exit(0)
5052
"""
5153
user_input = dedent(user_input)
52-
user_input = user_input.encode()
5354
p = spawn_repl()
5455
with SuppressCrashReport():
5556
p.stdin.write(user_input)
5657
output = kill_python(p)
57-
self.assertIn(b'After the exception.', output)
58+
self.assertIn('After the exception.', output)
5859
# Exit code 120: Py_FinalizeEx() failed to flush stdout and stderr.
5960
self.assertIn(p.returncode, (1, 120))
6061

@@ -86,13 +87,22 @@ def test_multiline_string_parsing(self):
8687
</test>"""
8788
'''
8889
user_input = dedent(user_input)
89-
user_input = user_input.encode()
9090
p = spawn_repl()
91-
with SuppressCrashReport():
92-
p.stdin.write(user_input)
91+
p.stdin.write(user_input)
9392
output = kill_python(p)
9493
self.assertEqual(p.returncode, 0)
9594

95+
def test_close_stdin(self):
96+
user_input = dedent('''
97+
import os
98+
print("before close")
99+
os.close(0)
100+
''')
101+
process = spawn_repl()
102+
output = process.communicate(user_input)[0]
103+
self.assertEqual(process.returncode, 0)
104+
self.assertIn('before close', output)
105+
96106

97107
if __name__ == "__main__":
98108
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Fix GIL usage in :c:func:`PyOS_Readline`: lock the GIL to set an exception.
1+
Fix GIL usage in :c:func:`PyOS_Readline`: lock the GIL to set an exception
2+
and pass the Python thread state when checking if there is a pending signal.

Modules/signalmodule.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -1779,10 +1779,11 @@ PyOS_FiniInterrupts(void)
17791779
finisignal();
17801780
}
17811781

1782+
1783+
// The caller doesn't have to hold the GIL
17821784
int
1783-
PyOS_InterruptOccurred(void)
1785+
_PyOS_InterruptOccurred(PyThreadState *tstate)
17841786
{
1785-
PyThreadState *tstate = _PyThreadState_GET();
17861787
_Py_EnsureTstateNotNULL(tstate);
17871788
if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
17881789
return 0;
@@ -1797,6 +1798,15 @@ PyOS_InterruptOccurred(void)
17971798
}
17981799

17991800

1801+
// The caller must to hold the GIL
1802+
int
1803+
PyOS_InterruptOccurred(void)
1804+
{
1805+
PyThreadState *tstate = _PyThreadState_GET();
1806+
return _PyOS_InterruptOccurred(tstate);
1807+
}
1808+
1809+
18001810
#ifdef HAVE_FORK
18011811
static void
18021812
_clear_pending_signals(void)

Parser/myreadline.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,23 @@ static PyThread_type_lock _PyOS_ReadlineLock = NULL;
2424
int (*PyOS_InputHook)(void) = NULL;
2525

2626
/* This function restarts a fgets() after an EINTR error occurred
27-
except if PyOS_InterruptOccurred() returns true. */
27+
except if _PyOS_InterruptOccurred() returns true. */
2828

2929
static int
3030
my_fgets(PyThreadState* tstate, char *buf, int len, FILE *fp)
3131
{
3232
#ifdef MS_WINDOWS
33-
HANDLE hInterruptEvent;
33+
HANDLE handle;
34+
_Py_BEGIN_SUPPRESS_IPH
35+
handle = (HANDLE)_get_osfhandle(fileno(fp));
36+
_Py_END_SUPPRESS_IPH
37+
38+
/* bpo-40826: fgets(fp) does crash if fileno(fp) is closed */
39+
if (handle == INVALID_HANDLE_VALUE) {
40+
return -1; /* EOF */
41+
}
3442
#endif
43+
3544
while (1) {
3645
if (PyOS_InputHook != NULL) {
3746
(void)(PyOS_InputHook)();
@@ -60,7 +69,7 @@ my_fgets(PyThreadState* tstate, char *buf, int len, FILE *fp)
6069
through to check for EOF.
6170
*/
6271
if (GetLastError()==ERROR_OPERATION_ABORTED) {
63-
hInterruptEvent = _PyOS_SigintEvent();
72+
HANDLE hInterruptEvent = _PyOS_SigintEvent();
6473
switch (WaitForSingleObjectEx(hInterruptEvent, 10, FALSE)) {
6574
case WAIT_OBJECT_0:
6675
ResetEvent(hInterruptEvent);
@@ -90,7 +99,7 @@ my_fgets(PyThreadState* tstate, char *buf, int len, FILE *fp)
9099
}
91100
#endif
92101

93-
if (PyOS_InterruptOccurred()) {
102+
if (_PyOS_InterruptOccurred(tstate)) {
94103
return 1; /* Interrupt */
95104
}
96105
return -2; /* Error */

0 commit comments

Comments
 (0)