3535# we see below. So for now, we'll live with the code duplication.
3636
3737import sys
38+ from typing import Callable , Optional
3839
3940from trepan .debugger import Trepan , debugger_obj
4041from trepan .post_mortem import post_mortem_excepthook , uncaught_exception
@@ -48,11 +49,11 @@ def debugger_on_post_mortem():
4849
4950def run_eval (
5051 expression ,
51- debug_opts = None ,
52- start_opts = None ,
53- globals_ = None ,
54- locals_ = None ,
55- tb_fn = None ,
52+ debug_opts : Optional [ dict ] = None ,
53+ start_opts : Optional [ dict ] = None ,
54+ globals_ : Optional [ dict ] = None ,
55+ locals_ : Optional [ dict ] = None ,
56+ tb_fn : Optional [ Callable ] = None ,
5657):
5758 """Evaluate the expression (given as a string) under debugger
5859 control starting with the statement after the place that
@@ -79,7 +80,13 @@ def run_eval(
7980 return
8081
8182
82- def run_call (func , * args , ** kwds ):
83+ def run_call (
84+ func : Callable ,
85+ * args ,
86+ debug_opts : Optional [dict ] = None ,
87+ start_opts : Optional [dict ] = None ,
88+ ** kwds ,
89+ ):
8390 """Call the function (a function or method object, not a string)
8491 with the given arguments starting with the statement after
8592 the place that this appears in your program.
@@ -88,7 +95,7 @@ def run_call(func, *args, **kwds):
8895 returned. The debugger prompt appears as soon as the function is
8996 entered."""
9097
91- dbg = Trepan ()
98+ dbg = Trepan (opts = debug_opts )
9299 try :
93100 return dbg .run_call (func , * args , ** kwds )
94101 except Exception :
@@ -278,26 +285,41 @@ def stop(opts=None):
278285if __name__ == "__main__" :
279286 import tracer
280287
281- def foo (n ):
282- y = n
283- for i in range (n ):
284- print (i )
285- pass
286- return y
288+ def plus5 (n : int ) -> int :
289+ return n + 5
287290
291+ from trepan .inout .stringarray import StringArrayInput , StringArrayOutput
288292 from trepan .lib .default import DEBUGGER_SETTINGS
289293
290294 settings = dict (DEBUGGER_SETTINGS )
291295 settings .update ({"trace" : True , "printset" : tracer .ALL_EVENTS })
292- debug_opts = {"step_ignore" : - 1 , "settings" : settings }
296+ debugger_input = StringArrayInput ()
297+ debugger_output = StringArrayOutput ()
298+ debug_opts = {
299+ "step_ignore" : - 1 ,
300+ "settings" : settings ,
301+ "input" : debugger_input ,
302+ "output" : debugger_output ,
303+ }
293304 print ('Issuing: run_eval("1+2")' )
294- print (run_eval ("1+2" , debug_opts = debug_opts ))
295- print ('Issuing: run_exec("x=1; y=2")' )
296- run_exec ("x=1; y=2" , debug_opts = debug_opts )
297- print ("Issuing: run_call(foo, debug_opts, None, 2)" )
305+ run_eval ("1+2" , debug_opts = debug_opts )
306+ print (debugger_output .output )
307+ # print('Issuing: run_exec("x=1; y=2")')
308+ # run_exec("x=1; y=2", debug_opts=debug_opts)
309+ debugger_input = StringArrayInput (["step" , "list" , "continue" ])
310+ debugger_output = StringArrayOutput ()
311+ debug_opts = {
312+ "step_ignore" : - 1 ,
313+ "settings" : settings ,
314+ "input" : debugger_input ,
315+ "output" : debugger_output ,
316+ }
298317 if len (sys .argv ) > 1 :
299- print (f"Issuing interactive: run_call(foo, { debug_opts } , None, { sys .argv [1 ]} )" )
300- run_call (foo , debug_opts , int (sys .argv [1 ]))
318+ print (
319+ f"Issuing interactive: run_call(plus5, { int (sys .argv [1 ])} , debug_opts={ debug_opts } )"
320+ )
321+ run_call (plus5 , int (sys .argv [1 ]), debug_opts = debug_opts )
301322 else :
302- run_call (foo , debug_opts , 2 )
323+ print (f"Issuing interactive: run_call(plus5, 2, debug_opts={ debug_opts } )" )
324+ run_call (plus5 , 2 , debug_opts = debug_opts )
303325 pass
0 commit comments