2424from pygments import highlight
2525
2626from mathicsscript .asymptote import asymptote_version
27+ from mathicsscript .interrupt import setup_signal_handler
2728from mathicsscript .settings import definitions
2829from mathicsscript .termshell import ShellEscapeException , mma_lexer
2930from mathicsscript .termshell_gnu import TerminalShellGNUReadline
30- from mathicsscript .termshell_prompt import (
31- TerminalShellCommon ,
32- TerminalShellPromptToolKit ,
33- )
31+ from mathicsscript .termshell import TerminalShellCommon
32+ from mathicsscript .termshell_prompt import TerminalShellPromptToolKit
3433from mathicsscript .version import __version__
3534
3635try :
@@ -123,7 +122,7 @@ def load_settings(shell):
123122 continue
124123 evaluation .evaluate (query )
125124 except KeyboardInterrupt :
126- print ("\n KeyboardInterrupt" )
125+ shell . errmsg ("\n KeyboardInterrupt" )
127126 return True
128127
129128
@@ -145,16 +144,17 @@ def interactive_eval_loop(
145144 shell : TerminalShellCommon ,
146145 unicode ,
147146 prompt ,
148- matplotlib : bool ,
149- asymptote : bool ,
150147 strict_wl_output : bool ,
151148):
149+ setup_signal_handler ()
150+
152151 def identity (x : Any ) -> Any :
153152 return x
154153
155154 def fmt_fun (query : Any ) -> Any :
156155 return highlight (str (query ), mma_lexer , shell .terminal_formatter )
157156
157+ shell .fmt_fn = fmt_fun
158158 while True :
159159 try :
160160 if have_readline and shell .using_readline :
@@ -173,6 +173,11 @@ def fmt_fun(query: Any) -> Any:
173173 fmt = fmt_fun
174174
175175 evaluation = Evaluation (shell .definitions , output = TerminalOutput (shell ))
176+
177+ # Store shell into the evaluation so that an interrupt handler
178+ # has access to this
179+ evaluation .shell = shell
180+
176181 query , source_code = evaluation .parse_feeder_returning_code (shell )
177182 if mathics_core .PRE_EVALUATION_HOOK is not None :
178183 mathics_core .PRE_EVALUATION_HOOK (query , evaluation )
@@ -214,7 +219,7 @@ def fmt_fun(query: Any) -> Any:
214219 try :
215220 print (open (source_code [2 :], "r" ).read ())
216221 except Exception :
217- print (str (sys .exc_info ()[1 ]))
222+ shell . errmsg (str (sys .exc_info ()[1 ]))
218223 else :
219224 subprocess .run (source_code [1 :], shell = True )
220225
@@ -224,13 +229,13 @@ def fmt_fun(query: Any) -> Any:
224229 # shell.definitions.increment_line(1)
225230
226231 except KeyboardInterrupt :
227- print ("\n KeyboardInterrupt" )
232+ shell . errmsg ("\n KeyboardInterrupt" )
228233 except EOFError :
229234 if prompt :
230- print ("\n \n Goodbye!\n " )
235+ shell . errmsg ("\n \n Goodbye!\n " )
231236 break
232237 except SystemExit :
233- print ("\n \n Goodbye!\n " )
238+ shell . errmsg ("\n \n Goodbye!\n " )
234239 # raise to pass the error code on, e.g. Quit[1]
235240 raise
236241 finally :
@@ -526,9 +531,7 @@ def main(
526531 )
527532
528533 definitions .set_line_no (0 )
529- interactive_eval_loop (
530- shell , charset , prompt , asymptote , matplotlib , strict_wl_output
531- )
534+ interactive_eval_loop (shell , charset , prompt , strict_wl_output )
532535 return exit_rc
533536
534537
0 commit comments