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

Restore previous f_trace after snoop is done #214

Open
alexmojaki opened this issue Mar 13, 2021 · 1 comment
Open

Restore previous f_trace after snoop is done #214

alexmojaki opened this issue Mar 13, 2021 · 1 comment

Comments

@alexmojaki
Copy link
Collaborator

alexmojaki commented Mar 13, 2021

See alexmojaki/snoop#34

Basically you need to do this:

https://github.com/alexmojaki/snoop/blob/437956c661184ab458a3f60fcf97c81c31ea2db1/snoop/tracer.py#L213-L217

In particular you need calling_frame.f_trace = previous_trace in addition to the existing sys.settrace(previous_trace).

This is obviously good for restoring the other tracer properly, which I confirmed with the PyCharm debugger. But I stumbled across this because pp stopped working after with snoop: exited. It turns out that if you don't reset f_trace then the frame line number becomes wrong. Here's a demo:

import inspect
import pysnooper

with pysnooper.snoop():
    pass

print(inspect.currentframe().f_trace)
print(inspect.currentframe().f_lineno)  # wrong lineno
1/0  # wrong lineno in traceback

In particular note the weird traceback:

Traceback (most recent call last):
  File "...", line 5, in <module>
    pass
ZeroDivisionError: division by zero

Here's a pure python version of the demo:

import inspect
import sys

frame = inspect.currentframe()

frame.f_trace = lambda *_: print('tracing', frame.f_lineno) or frame.f_trace
sys.settrace(frame.f_trace)

# frame.f_trace = None  # uncomment to fix
sys.settrace(None)

print('after trace', frame.f_lineno)  # wrong lineno
1/0  # wrong lineno in traceback

This is a bug which is apparently fixed in 3.10: https://bugs.python.org/issue42823

@cool-RR
Copy link
Owner

cool-RR commented Mar 13, 2021

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