|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (C) 2025 Rocky Bernstein <[email protected]> |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +import ctypes |
| 17 | +import threading |
| 18 | + |
| 19 | +import trepan.processor.cmdproc as trepan_cmdproc |
| 20 | +from mathics.core.interrupt import AbortInterrupt |
| 21 | +from trepan.processor.command.base_cmd import DebuggerCommand |
| 22 | + |
| 23 | +class AbortCommand(DebuggerCommand): |
| 24 | + """**abort** |
| 25 | +
|
| 26 | + Abort the computation being interrupted set the final computation to $Aborted. |
| 27 | +
|
| 28 | + Similar to Mathics3 command Abort[] |
| 29 | +
|
| 30 | +
|
| 31 | + See also: |
| 32 | + --------- |
| 33 | +
|
| 34 | + See `continue` `exit` or `kill` for more forceful termination commands. |
| 35 | +
|
| 36 | + `run` and `restart` restart the debugged program.""" |
| 37 | + |
| 38 | + aliases = ("a", "abort") |
| 39 | + category = "support" |
| 40 | + max_args = 0 |
| 41 | + short_help = "Abort computation" |
| 42 | + |
| 43 | + DebuggerCommand.setup(locals(), category="support", max_args=0) |
| 44 | + |
| 45 | + def nothread_quit(self, arg): |
| 46 | + """abort command when there's just one thread.""" |
| 47 | + |
| 48 | + self.debugger.core.stop() |
| 49 | + self.debugger.core.execution_status = "Abort command" |
| 50 | + raise AbortInterrupt |
| 51 | + |
| 52 | + def threaded_quit(self, arg): |
| 53 | + """abort command when several threads are involved.""" |
| 54 | + threading_list = threading.enumerate() |
| 55 | + mythread = threading.current_thread() |
| 56 | + for t in threading_list: |
| 57 | + if t != mythread: |
| 58 | + ctype_async_raise(t, AbortInterrupt) |
| 59 | + pass |
| 60 | + pass |
| 61 | + raise AbortInterrupt |
| 62 | + |
| 63 | + def run(self, args): |
| 64 | + threading_list = threading.enumerate() |
| 65 | + if ( |
| 66 | + len(threading_list) == 1 or self.debugger.from_ipython |
| 67 | + ) and threading_list[0].name == "MainThread": |
| 68 | + # We are in a main thread and either there is one thread or |
| 69 | + # we or are in ipython, so that's safe to quit. |
| 70 | + return self.nothread_quit(args) |
| 71 | + else: |
| 72 | + return self.threaded_quit(args) |
| 73 | + return |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +trepan_cmdproc.PASSTHROUGH_EXCEPTIONS.add(AbortInterrupt) |
| 78 | +def setup(debugger, instance): |
| 79 | + """ |
| 80 | + Setup we need to do in order to make the Mathics3 Debugger code in ``instance`` work in the |
| 81 | + trepan3k debugger object ``debugger`` |
| 82 | + """ |
| 83 | + # Make sure we hook into debugger interface |
| 84 | + print("mabort setup") |
| 85 | + instance.debugger.intf = debugger.intf |
| 86 | + trepan_cmdproc.PASSTHROUGH_EXCEPTIONS.add(AbortInterrupt) |
| 87 | + |
| 88 | +# Demo it |
| 89 | +if __name__ == "__main__": |
| 90 | + from trepan.processor.command import mock as Mmock |
| 91 | + |
| 92 | + dbgr, cmd = Mmock.dbg_setup() |
| 93 | + command = AbortCommand(cmd) |
| 94 | + for cmdline in [ |
| 95 | + "abort", |
| 96 | + ]: |
| 97 | + args = cmdline.split() |
| 98 | + cmd_argstr = cmdline[len(args[0]) :].lstrip() |
| 99 | + cmd.cmd_argstr = cmd_argstr |
| 100 | + command.run(args) |
| 101 | + pass |
| 102 | + pass |
0 commit comments