Skip to content

Commit

Permalink
Break out format_stack_entry() ...
Browse files Browse the repository at this point in the history
to make it a little more comprehensible and to make it
amenable to change in the Mathics3 debugger.
  • Loading branch information
rocky committed Sep 27, 2024
1 parent c1ee563 commit 464e630
Showing 1 changed file with 85 additions and 36 deletions.
121 changes: 85 additions & 36 deletions trepan/lib/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import os.path as osp
import re
from reprlib import repr
from typing import Optional
from typing import Optional, Tuple

import xdis
from xdis import IS_PYPY, get_opcode
Expand Down Expand Up @@ -113,23 +113,23 @@ def deparse_source_from_code(code):
return source_text


def format_stack_entry(
dbg_obj, frame_lineno, lprefix=": ", include_location=True, style="none"
) -> str:
"""Format and return a stack entry gdb-style.
Note: lprefix is not used. It is kept for compatibility.
def format_function_name(frame, style: str) -> Tuple[str, str]:
"""
Pick out the function name from ``frame`` and return both the name
and the name styled according to ``style``
"""
frame, lineno = frame_lineno
filename = frame2file(dbg_obj.core, frame)

s = ""
if frame.f_code.co_name:
funcname = frame.f_code.co_name
else:
funcname = "<lambda>"
pass
s = format_token(Function, funcname, style=style)
return funcname, format_token(Function, funcname, style=style)


def format_function_and_parameters(frame, debugger, style: str) -> Tuple[bool, str]:
""" """

funcname, s = format_function_name(frame, style)
args, varargs, varkw, local_vars = inspect.getargvalues(frame)
if "<module>" == funcname and (
[],
Expand Down Expand Up @@ -159,18 +159,34 @@ def format_stack_entry(
except Exception:
pass
else:
maxargstrsize = dbg_obj.settings["maxargstrsize"]
maxargstrsize = debugger.settings["maxargstrsize"]
if len(params) >= maxargstrsize:
params = f"{params[0:maxargstrsize]}...)"
pass
s += params
pass

# Note: ddd can't handle wrapped stack entries (yet).
# The 35 is hoaky though. FIXME.
if len(s) >= 35:
s += "\n "
return is_module, s


def format_return_and_location(
frame,
line_number: int,
debugger,
is_module: bool,
include_location: bool,
style: str,
) -> str:
"""
Format the return value if frame is at a return, and the location
of frame if `include_location` is True; `is_module` indicates whether
frame is a module or not.
A formatted string is returned; `style` gives the formatting
style used.
"""
filename = frame2file(debugger.core, frame)
s = ""
if "__return__" in frame.f_locals:
rv = frame.f_locals["__return__"]
s += "->"
Expand Down Expand Up @@ -206,8 +222,31 @@ def format_stack_entry(
filename = f"'{filename}'"
s += " %s at line %s" % (
format_token(Filename, filename, style=style),
format_token(LineNumber, "%r" % lineno, style=style),
format_token(LineNumber, str(line_number), style=style),
)

return s


def format_stack_entry(
dbg_obj, frame_lineno, lprefix=": ", include_location=True, style="none"
) -> str:
"""Format and return a stack entry gdb-style.
Note: lprefix is not used. It is kept for compatibility.
"""
frame, line_number = frame_lineno

is_module, s = format_function_and_parameters(frame, dbg_obj, style)
args, varargs, varkw, local_vars = inspect.getargvalues(frame)

# Note: ddd can't handle wrapped stack entries (yet).
# The 35 is hoaky though. FIXME.
if len(s) >= 35:
s += "\n "

s += format_return_and_location(
frame, line_number, dbg_obj, is_module, include_location, style
)
return s


Expand Down Expand Up @@ -259,17 +298,18 @@ def check_path_with_frame(frame, path):
return True, None


def is_exec_stmt(frame):
def is_exec_stmt(frame) -> bool:
"""Return True if we are looking at an exec statement"""
return hasattr(frame, "f_back") and get_call_function_name(frame) == "exec"


opc = get_opcode(PYTHON_VERSION_TRIPLE, IS_PYPY)


def get_call_function_name(frame):
def get_call_function_name(frame) -> Optional[str]:
"""If f_back is looking at a call function, return
the name for it. Otherwise, return None"""

f_back = frame.f_back
if not f_back:
return None
Expand Down Expand Up @@ -457,27 +497,34 @@ def __init__(self):
pass

frame = inspect.currentframe()
print(frame2filesize(frame))
# print(frame2filesize(frame))
pyc_file = osp.join(
osp.dirname(__file__), "__pycache__", osp.basename(__file__)[:-3] + ".pyc"
)
print(pyc_file, getsourcefile(pyc_file))
# print(pyc_file, getsourcefile(pyc_file))

m = MockDebugger()

# For testing print_stack_entry()
# import inspect
# import trepan
# from trepan.api import debug;
# dd = trepan.debugger.Trepan()
# my_frame = inspect.currentframe()
# dd.core.processor.stack = [(my_frame, 100)]
# dd.core.processor.curframe = my_frame
# debug()
from trepan.debugger import Trepan

dd = Trepan()
my_frame = inspect.currentframe()
dd.core.processor.stack = [(my_frame, 100)]
dd.core.processor.curframe = my_frame
# print_stack_entry(dd.core.processor, 0, "fruity")

# print(format_stack_entry(m, (frame, 10,)))
# print(format_stack_entry(m, (frame, 10,), color="dark"))
print(
format_stack_entry(
m,
(
frame,
10,
),
style="tango",
)
)
# print("frame count: %d" % count_frames(frame))
# print("frame count: %d" % count_frames(frame.f_back))
# print("frame count: %d" % count_frames(frame, 1))
Expand All @@ -488,13 +535,15 @@ def __init__(self):
# def sqr(x):
# x * x

# def fn(x):
# frame = inspect.currentframe()
# print(get_call_function_name(frame))
# return
def fn(x):
frame = inspect.currentframe()
print(format_stack_entry(dd, (frame, frame.f_code.co_firstlineno + 2)))
print(get_call_function_name(frame))
return

# print("=" * 30)
# eval("fn(5)")
print("=" * 30)
fn(5)
eval("fn(5)")
# print("=" * 30)
# print(print_obj("fn", fn))
# print("=" * 30)
Expand Down

0 comments on commit 464e630

Please sign in to comment.