Skip to content

Commit 8533b95

Browse files
committed
Better handling of pyasm files
pyasm lines have location information and newlines. So don't add these.
1 parent cd85cb5 commit 8533b95

2 files changed

Lines changed: 40 additions & 19 deletions

File tree

trepan/processor/command/list.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def run(self, args):
9494
if filename is None:
9595
return
9696
filename = pyficache.unmap_file(pyficache.resolve_name_to_path(filename))
97+
is_pyasm = filename.endswith(".pyasm")
9798

9899
# We now have range information. Do the listing.
99100
max_line = pyficache.size(filename)
@@ -147,6 +148,11 @@ def run(self, args):
147148
self.msg("[EOF]")
148149
break
149150
else:
151+
if is_pyasm:
152+
self.msg_nocr(line)
153+
proc.list_lineno = lineno
154+
continue
155+
150156
line = line.rstrip("\n")
151157
s = proc._saferepr(lineno).rjust(3)
152158
if len(s) < 5:
@@ -174,6 +180,7 @@ def run(self, args):
174180
else:
175181
s += a_pad
176182
pass
183+
177184
self.msg(s + "\t" + line)
178185
proc.list_lineno = lineno
179186
pass
@@ -203,8 +210,8 @@ def doit(cmd, args):
203210
print("--" * 10)
204211
# doit(lcmd, ['list'])
205212

206-
doit(lcmd, ['list', __file__ + ':5'])
207-
print('--' * 10)
213+
doit(lcmd, ["list", __file__ + ":5"])
214+
print("--" * 10)
208215

209216
# doit(lcmd, ['list', 'os:10'])
210217
# print('--' * 10)

trepan/processor/print.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2024 Rocky Bernstein <rocky@gnu.org>
3+
# Copyright (C) 2024-2025 Rocky Bernstein <rocky@gnu.org>
44
#
55
# This program is free software: you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License as published by
@@ -19,24 +19,17 @@
1919
import os.path as osp
2020
import re
2121
import sys
22-
from inspect import ismodule, currentframe
22+
from inspect import currentframe, ismodule
2323
from tempfile import NamedTemporaryFile
2424
from types import CodeType
2525

2626
import pyficache
2727

28+
from trepan.lib.format import Filename, Hex, LineNumber, Symbol, format_token # Opcode,
2829
from trepan.lib.stack import check_path_with_frame, frame2file, is_eval_or_exec_stmt
2930
from trepan.processor import cmdfns
3031
from trepan.processor.cmdfns import deparse_fn
3132

32-
from trepan.lib.format import ( # Opcode,
33-
Filename,
34-
Hex,
35-
LineNumber,
36-
Symbol,
37-
format_token,
38-
)
39-
4033
try:
4134
from trepan.lib.deparse import deparse_and_cache
4235

@@ -53,7 +46,9 @@ def format_code(code_object: CodeType, style) -> str:
5346
Format according to "style" a Python code object. The
5447
formatted string is returned.
5548
"""
56-
formatted_line = format_token(LineNumber, str(code_object.co_firstlineno), style=style)
49+
formatted_line = format_token(
50+
LineNumber, str(code_object.co_firstlineno), style=style
51+
)
5752
formatted_id = format_token(Hex, hex(id(code_object)), style=style)
5853
formatted_name = format_token(Symbol, code_object.co_name, style=style)
5954
formatted_filename = format_token(Filename, code_object.co_filename, style=style)
@@ -70,14 +65,16 @@ def format_frame(frame_object, style) -> str:
7065
"""
7166
formatted_line = format_token(LineNumber, str(frame_object.f_lineno), style=style)
7267
formatted_id = format_token(Hex, hex(id(frame_object)), style=style)
73-
formatted_filename = format_token(Filename, frame_object.f_code.co_filename, style=style)
68+
formatted_filename = format_token(
69+
Filename, frame_object.f_code.co_filename, style=style
70+
)
7471
return (
7572
f"<frame at {formatted_id} "
7673
f"file {formatted_filename}, line {formatted_line}>"
7774
)
7875

7976

80-
def print_source_line(msg, lineno, line, event_str=None):
77+
def print_source_line(msg, lineno, line, event_str=None, is_pyasm: bool = False):
8178
"""Print out a source line of text , e.g. the second
8279
line in:
8380
(/tmp.py:2): <module>
@@ -90,7 +87,10 @@ def print_source_line(msg, lineno, line, event_str=None):
9087

9188
# We don't use the filename normally. ipython and other applications
9289
# however might.
93-
return msg(f"{event_str} {lineno} {line}")
90+
if is_pyasm:
91+
return msg(f"{event_str}\n{line}")
92+
else:
93+
return msg(f"{event_str} {lineno} {line}")
9494

9595

9696
def print_source_location_info(
@@ -116,6 +116,7 @@ def print_source_location_info(
116116
print_fn(mess)
117117
return
118118

119+
119120
def print_location(proc_obj):
120121
"""Show where we are. GUI's and front-end interfaces often
121122
use this to update displays. So it is helpful to make sure
@@ -242,7 +243,15 @@ def prefix_for_filename(deparsed_text: str) -> str:
242243
opts["style"] = proc_obj.settings("style")
243244

244245
pyficache.update_cache(filename)
246+
247+
is_pyasm = filename.endswith(".pyasm")
248+
if is_pyasm:
249+
opts = opts.copy()
250+
opts["style"] = "plain"
251+
opts["output"] = "plain"
252+
245253
line = pyficache.getline(filename, lineno, opts)
254+
246255
if not line:
247256
if (
248257
not source_text
@@ -290,8 +299,6 @@ def prefix_for_filename(deparsed_text: str) -> str:
290299

291300
pass
292301

293-
if isinstance(proc_obj.curframe, int):
294-
breakpoint()
295302
line = linecache.getline(filename, lineno, proc_obj.curframe.f_globals)
296303
if not line:
297304
m = re.search("^<frozen (.*)>", filename)
@@ -343,7 +350,11 @@ def prefix_for_filename(deparsed_text: str) -> str:
343350
if line and len(line.strip()) != 0:
344351
if proc_obj.event:
345352
print_source_line(
346-
intf_obj.msg, lineno, line, proc_obj.event2short[proc_obj.event]
353+
intf_obj.msg,
354+
lineno,
355+
line,
356+
proc_obj.event2short[proc_obj.event],
357+
is_pyasm,
347358
)
348359
pass
349360
if "<string>" != filename:
@@ -364,11 +375,14 @@ def prefix_for_filename(deparsed_text: str) -> str:
364375
pass
365376
return True
366377

378+
367379
# Demo it
368380
if __name__ == "__main__":
381+
369382
def five():
370383
from trepan.processor.cmdproc import CommandProcessor
371384
from trepan.processor.command.mock import MockDebugger
385+
372386
d = MockDebugger()
373387
cmdproc = CommandProcessor(d.core)
374388
cmdproc.frame = currentframe()

0 commit comments

Comments
 (0)