Skip to content

Commit 63eaf34

Browse files
committed
Better information when exec current frame
1 parent 6e02fdf commit 63eaf34

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

trepan/lib/stack.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ def format_function_name(frame, style: str) -> Tuple[Optional[str], Optional[str
120120
Pick out the function name from ``frame`` and return both the name
121121
and the name styled according to ``style``
122122
"""
123-
if is_eval_or_exec_stmt(frame):
123+
if (exec_type := is_eval_or_exec_stmt(frame)):
124124
funcname = get_call_function_name(frame)
125+
if funcname is None:
126+
funcname = exec_type
125127
elif frame.f_code.co_name:
126128
funcname = frame.f_code.co_name
127129
else:

trepan/processor/cmdfns.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2013, 2015, 2017-2018, 2020-2021, 2023 Rocky
3+
# Copyright (C) 2013, 2015, 2017-2018, 2020-2021, 2023-2024 Rocky
44
# Bernstein <rocky@gnu.org>
55
#
66
# This program is free software: you can redistribute it and/or modify
@@ -38,13 +38,16 @@ def source_tempfile_remap(prefix, text, tempdir=None):
3838

3939
def deparse_fn(code):
4040
try:
41+
if PYTHON_VERSION_TRIPLE >= (3, 9):
42+
# Don't have decompiler here yet.
43+
return None
4144
if (3, 7) <= PYTHON_VERSION_TRIPLE < (3, 9):
4245
from decompyle3.semantics.linemap import (
4346
code_deparse_with_fragments_and_map as deparse_code,
4447
)
4548
else:
4649
from uncompyle6.semantics.linemap import (
47-
deparse_code_with_fragments_and_map as deparse_code,
50+
code_deparse_with_fragments_and_map as deparse_code,
4851
)
4952

5053
except ImportError:
@@ -246,4 +249,9 @@ def msg(m):
246249
print(want_different_line("s", False))
247250
print(want_different_line("s", True))
248251
print(want_different_line("s", True))
252+
253+
my_frame = inspect.currentframe()
254+
print("=" * 30)
255+
print(deparse_fn(my_frame.f_code).text)
256+
249257
pass

trepan/processor/print.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,25 @@ def print_source_location_info(
116116
print_fn(mess)
117117
return
118118

119-
120119
def print_location(proc_obj):
121120
"""Show where we are. GUI's and front-end interfaces often
122121
use this to update displays. So it is helpful to make sure
123122
we give at least some place that's located in a file.
124123
"""
124+
125+
def prefix_for_filename(deparsed_text: str) -> str:
126+
"""
127+
Return a reasonable filename-friendly string from some
128+
deparsed text. We do this so that the user gets some idea of
129+
what the string (source code text) is contained in the file.
130+
131+
"""
132+
lines = deparsed_text.split("\n")
133+
# FIXME Rather than blindly take the first line,
134+
# check if it is blank and if so use other lines.
135+
first_text_line = lines[0]
136+
return proc_obj._saferepr(first_text_line)[1:-1][:10]
137+
125138
i_stack = proc_obj.curindex
126139
if i_stack is None or proc_obj.stack is None:
127140
return False
@@ -176,11 +189,8 @@ def print_location(proc_obj):
176189
if deparsed:
177190
# Create a nice prefix for the temporary file to write.
178191
# Use the exec type and first line of the deparsed text.
179-
first_text_line = deparsed.text.split("\n")[0]
180-
# Strip of quotes added by repr and up the first 10 characters
181-
# of result.
182-
leading_code = proc_obj._saferepr(first_text_line)[1:-1][:10]
183-
prefix = f"{eval_kind}-{leading_code}-"
192+
leading_code_str = prefix_for_filename(deparsed.text)
193+
prefix = f"{eval_kind}-{leading_code_str}-"
184194

185195
remapped_file = cmdfns.source_tempfile_remap(
186196
prefix,
@@ -192,6 +202,11 @@ def print_location(proc_obj):
192202
filename = remapped_file
193203
else:
194204
eval_kind = is_eval_or_exec_stmt(frame)
205+
deparsed = deparse_fn(frame.f_code)
206+
if deparsed is not None:
207+
source_text = deparsed.text
208+
# else:
209+
# print("Can't deparse", frame.f_code)
195210
if source_text is None and eval_kind:
196211
source_text = f"{eval_kind}(...)"
197212
pass
@@ -251,7 +266,7 @@ def print_location(proc_obj):
251266
# FIXME:
252267
if source_text:
253268
lines = source_text.split("\n")
254-
temp_name = "string-"
269+
temp_name = "string-" + prefix_for_filename(source_text)
255270
else:
256271
# try with good ol linecache and consider fixing pyficache
257272
lines = linecache.getlines(filename)

0 commit comments

Comments
 (0)