File tree Expand file tree Collapse file tree 2 files changed +14
-13
lines changed Expand file tree Collapse file tree 2 files changed +14
-13
lines changed Original file line number Diff line number Diff line change 28
28
import signal
29
29
import struct
30
30
import termios
31
+ import threading
31
32
import time
32
33
import types
33
34
import platform
@@ -390,15 +391,15 @@ def restore(self):
390
391
os .write (self .output_fd , b"\033 [?7h" )
391
392
392
393
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
402
403
del self .old_sigwinch
403
404
404
405
def push_char (self , char : int | bytes ) -> None :
Original file line number Diff line number Diff line change @@ -326,14 +326,14 @@ def test_restore_in_thread(self, _os_write):
326
326
def thread_target ():
327
327
try :
328
328
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 )
332
331
thread = threading .Thread (target = thread_target )
333
332
thread .start ()
334
333
thread .join ()
334
+ # gh-139391: should not raise any exception when called from non-main thread
335
335
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" )
337
337
338
338
339
339
@unittest .skipIf (sys .platform == "win32" , "No Unix console on Windows" )
You can’t perform that action at this time.
0 commit comments