3636
3737# types
3838Command = commands .Command
39- if False :
40- from .types import Callback , SimpleContextManager , KeySpec , CommandName
39+ from .types import Callback , SimpleContextManager , KeySpec , CommandName
4140
4241
4342def disp_str (buffer : str ) -> tuple [str , list [int ]]:
@@ -247,6 +246,7 @@ class Reader:
247246 lxy : tuple [int , int ] = field (init = False )
248247 scheduled_commands : list [str ] = field (default_factory = list )
249248 can_colorize : bool = False
249+ threading_hook : Callback | None = None
250250
251251 ## cached metadata to speed up screen refreshes
252252 @dataclass
@@ -722,6 +722,24 @@ def do_cmd(self, cmd: tuple[str, list[str]]) -> None:
722722 self .console .finish ()
723723 self .finish ()
724724
725+ def run_hooks (self ) -> None :
726+ threading_hook = self .threading_hook
727+ if threading_hook is None and 'threading' in sys .modules :
728+ from ._threading_handler import install_threading_hook
729+ install_threading_hook (self )
730+ if threading_hook is not None :
731+ try :
732+ threading_hook ()
733+ except Exception :
734+ pass
735+
736+ input_hook = self .console .input_hook
737+ if input_hook :
738+ try :
739+ input_hook ()
740+ except Exception :
741+ pass
742+
725743 def handle1 (self , block : bool = True ) -> bool :
726744 """Handle a single event. Wait as long as it takes if block
727745 is true (the default), otherwise return False if no event is
@@ -732,16 +750,13 @@ def handle1(self, block: bool = True) -> bool:
732750 self .dirty = True
733751
734752 while True :
735- input_hook = self .console .input_hook
736- if input_hook :
737- input_hook ()
738- # We use the same timeout as in readline.c: 100ms
739- while not self .console .wait (100 ):
740- input_hook ()
741- event = self .console .get_event (block = False )
742- else :
743- event = self .console .get_event (block )
744- if not event : # can only happen if we're not blocking
753+ # We use the same timeout as in readline.c: 100ms
754+ self .run_hooks ()
755+ self .console .wait (100 )
756+ event = self .console .get_event (block = False )
757+ if not event :
758+ if block :
759+ continue
745760 return False
746761
747762 translate = True
@@ -763,8 +778,7 @@ def handle1(self, block: bool = True) -> bool:
763778 if cmd is None :
764779 if block :
765780 continue
766- else :
767- return False
781+ return False
768782
769783 self .do_cmd (cmd )
770784 return True
0 commit comments