From f8c0105af7e5ba59b5f33fd68d83360788027a83 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 10 Nov 2024 21:49:58 -0500 Subject: [PATCH] Improve "list" error handling --- trepan/interfaces/user.py | 14 +++++++++----- trepan/processor/cmdlist.py | 7 ++++++- trepan/processor/cmdproc.py | 7 ++----- trepan/processor/location.py | 5 +++-- trepan/processor/parse/semantics.py | 3 ++- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/trepan/interfaces/user.py b/trepan/interfaces/user.py index b0a0344a..3838a766 100755 --- a/trepan/interfaces/user.py +++ b/trepan/interfaces/user.py @@ -18,6 +18,8 @@ """Interface when communicating with the user in the same process as the debugged program.""" import atexit +import os.path as osp +import pathlib from os import environ @@ -28,6 +30,10 @@ histfile = environ.get("TREPAN3KHISTFILE", default_configfile("history")) +# Create HISTFILE if it doesn't exist already +if not osp.isfile(histfile): + pathlib.Path(histfile).touch() + # is_pypy = '__pypy__' in sys.builtin_module_names DEFAULT_USER_SETTINGS = { @@ -45,7 +51,8 @@ write_history_file, ) except ImportError: - pass + def write_history_file(histfile: str): + return class UserInterface(TrepanInterface): """Interface when communicating with the user in the same @@ -86,10 +93,7 @@ def __init__(self, inp=None, out=None, opts={}): return def user_write_history_file(self): - try: - write_history_file(self.histfile) - except Exception: - pass + write_history_file(self.histfile) def close(self): """Closes both input and output""" diff --git a/trepan/processor/cmdlist.py b/trepan/processor/cmdlist.py index 20726fd4..0997dcbf 100644 --- a/trepan/processor/cmdlist.py +++ b/trepan/processor/cmdlist.py @@ -17,7 +17,7 @@ from trepan.processor.location import resolve_location from trepan.processor.parse.parser import LocationError from trepan.processor.parse.scanner import ScannerError -from trepan.processor.parse.semantics import Location, build_location, build_range +from trepan.processor.parse.semantics import Location, RangeError, build_location, build_range INVALID_PARSE_LIST = (None, None, None) @@ -63,6 +63,9 @@ def parse_list_cmd(proc, args, listsize=10): proc.errmsg(e.text) proc.errmsg(e.text_cursor) return INVALID_PARSE_LIST + except RangeError as e: + proc.errmsg(e.errmsg) + return INVALID_PARSE_LIST if list_range.first is None: # Last must have been given @@ -100,6 +103,8 @@ def parse_list_cmd(proc, args, listsize=10): last = first + int(last[1:]) elif not last: last = first + listsize + elif first is None: + return INVALID_PARSE_LIST elif last < first: # Treat as a count rather than an absolute location last = first + last diff --git a/trepan/processor/cmdproc.py b/trepan/processor/cmdproc.py index 0fa3baf8..94653993 100644 --- a/trepan/processor/cmdproc.py +++ b/trepan/processor/cmdproc.py @@ -197,11 +197,6 @@ def get_option_fn(key): self._populate_cmd_lists() - # Note: prompt_str's value set below isn't used. It is - # computed dynamically. The value is suggestive of what it - # looks like. - self.prompt_str = "(trepan3k) " - # Stop only if line/file is different from last time self.different_line = None @@ -234,6 +229,8 @@ def get_option_fn(key): initfile_list = get_option("initfile_list") for init_cmdfile in initfile_list: self.queue_startfile(init_cmdfile) + + self.set_prompt() return def _saferepr(self, str, maxwidth=None): diff --git a/trepan/processor/location.py b/trepan/processor/location.py index a3453c16..da1f3c0e 100644 --- a/trepan/processor/location.py +++ b/trepan/processor/location.py @@ -151,10 +151,11 @@ def resolve_location(proc, location) -> Optional[Location]: if lineinfo: offset = lineinfo[0].offsets[0] mod_func = lineinfo[0].name - else: - print(f"No offset found for {filename} {lineno}") elif location.line_number: + if curframe is None: + proc.errmsg("Current frame is not set") + return INVALID_LOCATION filename = frame2file(proc.core, curframe, canonic=False) lineno = location.line_number is_address = location.is_address diff --git a/trepan/processor/parse/semantics.py b/trepan/processor/parse/semantics.py index 94b6d3c5..f9a28b35 100644 --- a/trepan/processor/parse/semantics.py +++ b/trepan/processor/parse/semantics.py @@ -204,7 +204,8 @@ def n_range(self, range_node): Location(None, last_node.value, False, None, offset=None), None ) else: - assert last_node == "DIRECTION" + if last_node != "DIRECTION": + raise RangeError("Expecting a range direction at the end") self.result = ListRange(None, last_node.value) pass self.prune()