Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand print context instead of climbing up the stack #10

Open
besfahbod opened this issue Feb 5, 2019 · 1 comment
Open

Expand print context instead of climbing up the stack #10

besfahbod opened this issue Feb 5, 2019 · 1 comment

Comments

@besfahbod
Copy link
Contributor

At the moment, the dbg implementation walks up the stack to find a line with "dbg" in it:

pydbg/pydbg.py

Lines 27 to 29 in 4d787cd

for i in reversed(inspect.stack()):
ctx = i.code_context[0]
if "dbg" in ctx:

Considering the fact that the expected use-case of this function is to put it literally in the code (and not pass around as a callback), I think it's safe to limit the stack lookup to only one level.

If we do that, we can then improve the context lookup to walk back on the line-of-code (to some maximum of like, 10) and print those out as the context for the call.

What do you think?

@tylerwince
Copy link
Owner

Are you thinking we do something like:

ctx = inspect.stack()[~1].code_context[0]
print(
    f"[{i.filename}:{i.lineno}] {ctx[ctx.find('(') + 1 : ctx.rfind(')')]} = {exp!r}"
)

so we don't have to iterate over the entire stack list to reverse it? You are likely right that we can only look up 1 level in the stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants