Skip to content

Commit 2c3d101

Browse files
committed
Improve "info line" and "info lines"
Add * in "info" output when we mean an offset. Uses revised pyficache line_info information which always stores code object and offset.
1 parent 078d911 commit 2c3d101

5 files changed

Lines changed: 25 additions & 23 deletions

File tree

trepan/lib/breakpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(
7272
if (code_info := code_position_cache.get(code)) is not None:
7373
if position == -1:
7474
# Figure out the code offset from the line number.
75-
if linecache_info is not None and (tup := linecache_info.lineno_info.get(line_number)):
75+
if linecache_info is not None and (tup := linecache_info.line_info.get(line_number)):
7676
self.offset = tup[0]
7777
# When an offset value is Python code, then column information is stored in the parent.
7878
# FIXME: -1 and 1 might not be right when we have a line with some code and a

trepan/lib/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def canonic_filename(self, frame: Optional[FrameType]) -> str:
199199
if "<string>" == filename:
200200
if (new_filename := pyficache.main.code2tempfile.get(frame.f_code)):
201201
filename = new_filename
202-
return self.canonic(frame.f_code.co_filename)
202+
return self.canonic(filename)
203203

204204
def filename(self, filename=None) -> Optional[str]:
205205
"""Return filename or the basename of that depending on the

trepan/processor/command/info_subcmd/line.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
import columnize
1819
import inspect
1920
import os.path as osp
2021
import re
@@ -90,15 +91,16 @@ def lineinfo(self, arg):
9091

9192
def run(self, args):
9293
"""Current line number in source file"""
93-
if not self.proc.curframe:
94-
self.errmsg("No line number information available.")
95-
return
96-
9794
# info line <loc>
9895
if len(args) == 0:
96+
if not self.proc.curframe:
97+
self.errmsg("Frame is needed when no line number is given.")
98+
return
99+
99100
# No line number. Use current frame line number
100101
line_number = inspect.getlineno(self.proc.curframe)
101102
filename = self.core.canonic_filename(self.proc.curframe)
103+
102104
elif len(args) == 1:
103105
# lineinfo returns (item, file, lineno) or (None,)
104106
line_number, filename = self.lineinfo(args[2:])
@@ -123,25 +125,21 @@ def run(self, args):
123125
return
124126
msg1 = 'Line %d of "%s"' % (line_number, self.core.filename(filename),)
125127
line_info = linecache_info.line_info
126-
if line_info:
127-
msg2 = "is at offset(s) %s" % ", ".join(linecache_info.line_numbers[line_number])
128-
self.msg(wrapped_lines(msg1, msg2, self.settings["width"]))
128+
line_number_offsets = line_info.get(line_number)
129+
if line_number_offsets:
130+
offset_data = [f"{code.co_name}:*{offset}" for code, offset in line_number_offsets]
131+
if len(offset_data) == 1:
132+
msg2 = f"is at bytecode offset {offset_data[0]}"
133+
self.msg(wrapped_lines(msg1, msg2, self.settings["width"]))
134+
else:
135+
msg2 = "is at bytecode offsets:"
136+
self.msg(wrapped_lines(msg1, msg2, self.settings["width"]))
137+
self.msg(columnize.columnize(offset_data, colsep=", ", ljust=False, lineprefix=" "))
129138
else:
130139
self.errmsg(
131140
"No line information for line %d of %s"
132141
% (line_number, self.core.filename(filename))
133142
)
134-
if line_info and len(line_info) > 1:
135-
self.msg(
136-
wrapped_lines(
137-
"There are multiple line offsets for line number.",
138-
"Other line offsets: %s"
139-
% ", ".join(
140-
["%s of %s" % (li.offsets[0], li.name) for li in line_info[1:]]
141-
),
142-
self.settings["width"],
143-
)
144-
)
145143
return False
146144

147145
pass

trepan/processor/command/info_subcmd/lines.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2020, 2024 Rocky Bernstein <rocky@gnu.org>
2+
# Copyright (C) 2020, 2024, 2026 Rocky Bernstein <rocky@gnu.org>
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
@@ -21,6 +21,7 @@
2121
# Our local modules
2222
from trepan.processor.command.base_subcmd import DebuggerSubcommand
2323
from trepan.misc import pretty_modfunc_name
24+
# from pyficache import get_linecache_info
2425
from pyficache import cache_code_lines
2526

2627

@@ -96,13 +97,16 @@ def run(self, args):
9697
return
9798

9899
# No line number. Use current frame line number
99-
filename = curframe.f_code.co_filename
100+
filename = self.core.canonic_filename(self.proc.curframe)
100101
file_info = cache_code_lines(
101102
filename, toplevel_only=False, include_offsets=True
102103
)
103104
if file_info:
104105
self.section(f"Line: fn, offset for table for {filename}")
105106
lines = []
107+
# linecache_info = get_linecache_info(filename)
108+
# line_info = linecache_info.line_info
109+
106110
for line_number, line_info in file_info.line_numbers.items():
107111
if not name or any(li.name == name for li in line_info):
108112
lines.append(

trepan/processor/command/reload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def run(self, args):
8989
subcommand_module = importlib.import_module(subcmd.__module__)
9090
importlib.reload(subcommand_module)
9191
classnames = [
92-
tup[0] for tup in inspect.getmembers(subcommand_module, inspect.isclass)
92+
tup[0] for tup in inspect.getmembers(subcommand_module, inspect.isclass) if str(tup[0]) != "DebuggerSubcommand"
9393
]
9494
if len(classnames) == 1:
9595
try:

0 commit comments

Comments
 (0)