Skip to content

Commit 85b571d

Browse files
yihong0618picnixz
andcommitted
fix: address comments better way fix
Signed-off-by: yihong0618 <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent c0bb775 commit 85b571d

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

Lib/_pyrepl/unix_console.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import signal
2929
import struct
3030
import termios
31+
import threading
3132
import time
3233
import types
3334
import platform
@@ -390,15 +391,15 @@ def restore(self):
390391
os.write(self.output_fd, b"\033[?7h")
391392

392393
if hasattr(self, "old_sigwinch"):
393-
# Only restore signal handler if we're in the main thread
394-
# signal.signal() only works in the main thread of the main interpreter
395-
try:
396-
signal.signal(signal.SIGWINCH, self.old_sigwinch)
397-
except ValueError:
398-
# This can happen when called from a non-main thread
399-
# (e.g., asyncio REPL). In this case, we skip signal restoration
400-
# to avoid the "signal only works in main thread" error.
401-
pass
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
402403
del self.old_sigwinch
403404

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

Lib/test/test_pyrepl/test_unix_console.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,14 @@ def test_restore_in_thread(self, _os_write):
326326
def thread_target():
327327
try:
328328
console.restore()
329-
except ValueError as e:
330-
if "signal only works in main thread" in str(e):
331-
exception_caught.append(e)
329+
except Exception as e:
330+
exception_caught.append(e)
332331
thread = threading.Thread(target=thread_target)
333332
thread.start()
334333
thread.join()
334+
# gh-139391: should not raise any exception when called from non-main thread
335335
self.assertEqual(len(exception_caught), 0,
336-
"restore() should not raise ValueError in non-main thread")
336+
"restore() should not raise any exception in non-main thread")
337337

338338

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

0 commit comments

Comments
 (0)