Skip to content

Commit

Permalink
Improve "list" error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Nov 11, 2024
1 parent 43223e1 commit f8c0105
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
14 changes: 9 additions & 5 deletions trepan/interfaces/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -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"""
Expand Down
7 changes: 6 additions & 1 deletion trepan/processor/cmdlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions trepan/processor/cmdproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions trepan/processor/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion trepan/processor/parse/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f8c0105

Please sign in to comment.