2020from mathics .core .interrupt import AbortInterrupt
2121from trepan .processor .command .base_cmd import DebuggerCommand
2222
23+
24+ def ctype_async_raise (thread_obj , exception ):
25+ found = False
26+ target_tid = 0
27+ for tid , tobj in threading ._active .items ():
28+ if tobj is thread_obj :
29+ found = True
30+ target_tid = tid
31+ break
32+
33+ if not found :
34+ raise ValueError ("Invalid thread object" )
35+
36+ ret = ctypes .pythonapi .PyThreadState_SetAsyncExc (
37+ target_tid , ctypes .py_object (exception )
38+ )
39+ # ref: http://docs.python.org/c-api/init.html#PyThreadState_SetAsyncExc
40+ if ret == 0 :
41+ raise AbortInterrupt
42+ elif ret > 1 :
43+ # Huh? Why would we notify more than one threads?
44+ # Because we punch a hole into C level interpreter.
45+ # So it is better to clean up the mess.
46+ ctypes .pythonapi .PyThreadState_SetAsyncExc (target_tid , 0 )
47+ raise SystemError ("PyThreadState_SetAsyncExc failed" )
48+
49+
2350class AbortCommand (DebuggerCommand ):
2451 """**abort**
2552
@@ -62,9 +89,9 @@ def threaded_quit(self, arg):
6289
6390 def run (self , args ):
6491 threading_list = threading .enumerate ()
65- if (
66- len ( threading_list ) == 1 or self . debugger . from_ipython
67- ) and threading_list [ 0 ].name == "MainThread" :
92+ if (len ( threading_list ) == 1 or self . debugger . from_ipython ) and threading_list [
93+ 0
94+ ].name == "MainThread" :
6895 # We are in a main thread and either there is one thread or
6996 # we or are in ipython, so that's safe to quit.
7097 return self .nothread_quit (args )
@@ -73,17 +100,18 @@ def run(self, args):
73100 return
74101
75102
76-
77103trepan_cmdproc .PASSTHROUGH_EXCEPTIONS .add (AbortInterrupt )
104+
105+
78106def setup (debugger , instance ):
79107 """
80108 Setup we need to do in order to make the Mathics3 Debugger code in ``instance`` work in the
81109 trepan3k debugger object ``debugger``
82110 """
83111 # Make sure we hook into debugger interface
84- print ("mabort setup" )
85112 instance .debugger .intf = debugger .intf
86- trepan_cmdproc .PASSTHROUGH_EXCEPTIONS .add (AbortInterrupt )
113+ # trepan_cmdproc.PASSTHROUGH_EXCEPTIONS.add(AbortInterrupt)
114+
87115
88116# Demo it
89117if __name__ == "__main__" :
0 commit comments