Skip to content

Commit bcd67d3

Browse files
committed
fix: apply the suggestion
Signed-off-by: yihong0618 <[email protected]>
1 parent c454f63 commit bcd67d3

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

Lib/_pyrepl/unix_console.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,14 @@ def restore(self):
391391
os.write(self.output_fd, b"\033[?7h")
392392

393393
if hasattr(self, "old_sigwinch"):
394-
if os.name != 'nt':
395-
try:
396-
signal.signal(signal.SIGWINCH, self.old_sigwinch)
397-
except ValueError as e:
398-
# We can silence the ValueError if signal.signal() raised it
399-
# from a non-main thread on a non-Windows platform. Otherwise,
400-
# we need to re-raise it as its cause could be different.
401-
if threading.current_thread() is threading.main_thread():
402-
raise e
394+
try:
395+
signal.signal(signal.SIGWINCH, self.old_sigwinch)
396+
except ValueError as e:
397+
# We can silence the ValueError if signal.signal() raised it
398+
# from a non-main thread on a non-Windows platform. Otherwise,
399+
# we need to re-raise it as its cause could be different.
400+
if threading.current_thread() is threading.main_thread():
401+
raise e
403402
del self.old_sigwinch
404403

405404
def push_char(self, char: int | bytes) -> None:

Lib/test/test_pyrepl/test_unix_console.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import signal
55
import subprocess
66
import sys
7+
import threading
78
import unittest
89
from functools import partial
910
from test.support import os_helper, force_not_colorized_test_class
10-
from test.support import script_helper
11+
from test.support import script_helper, threading_helper
1112

1213
from unittest import TestCase
1314
from unittest.mock import MagicMock, call, patch, ANY, Mock
@@ -317,23 +318,16 @@ def test_restore_with_invalid_environ_on_macos(self, _os_write):
317318
console.prepare() # needed to call restore()
318319
console.restore() # this should succeed
319320

321+
@threading_helper.reap_threads
322+
@threading_helper.requires_working_threading()
320323
def test_restore_in_thread(self, _os_write):
321-
# for gh-139391
322-
import threading
324+
# gh-139391: ensure that console.restore() silently suppresses
325+
# exceptions when calling signal.signal() from a non-main thread.
323326
console = unix_console([])
324327
console.old_sigwinch = signal.SIG_DFL
325-
exception_caught = []
326-
def thread_target():
327-
try:
328-
console.restore()
329-
except Exception as e:
330-
exception_caught.append(e)
331-
thread = threading.Thread(target=thread_target)
328+
thread = threading.Thread(target=console.restore)
332329
thread.start()
333-
thread.join()
334-
# gh-139391: should not raise any exception when called from non-main thread
335-
self.assertEqual(len(exception_caught), 0,
336-
"restore() should not raise any exception in non-main thread")
330+
thread.join() # this should not raise
337331

338332

339333
@unittest.skipIf(sys.platform == "win32", "No Unix console on Windows")

0 commit comments

Comments
 (0)