-
-
Notifications
You must be signed in to change notification settings - Fork 439
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
Expose CTracer's trace function #789
Comments
Why not use |
Tried that, but it did not work. Looking at it again, it appears to be due to it setting itself up again as the trace function itself. import sys
origtrace = sys.gettrace()
def w(*args):
print("trace", args)
print("calling", origtrace)
origtrace(*args)
print(sys.gettrace())
# sys.settrace(w)
print("called")
return w
def f():
pass
f()
sys.settrace(w)
f()
f()
|
So, I think it would be nice if the |
Hmm, what am I doing wrong in my tests that are designed to check that |
You should assert that calling the trace function does not change the existing one.. def w(*args):
assert sys.gettrace() == w
origtrace(*args)
assert sys.gettrace() == w import sys
origtrace = sys.gettrace()
def w(*args):
print("trace", args)
assert sys.gettrace() == w
origtrace(*args)
assert sys.gettrace() == w
return w
def f():
pass
f()
sys.settrace(w)
f()
f() |
Just for clarification: I was using |
Looks like this gets done here: coveragepy/coverage/ctracer/tracer.c Lines 1015 to 1017 in 820b255
|
You're ahead of me here... I'm not sure what the original problem was. You've proposed a solution, but can you show me what you were originally attempting that went wrong? The lines you found in tracer.c were designed to make getting and setting the trace function work better, which sounds like what you are doing, but you say I should get rid of those lines. Let's start from the beginning so I can understand the whole situation. |
I am not saying to get rid of them - I don't know if they are necessary, but only meant that this is what's causing what I am seeing. So, when coverage is active, I want to monkeypatch The idea is to have coverage also for code while/after |
I have not tried it yet, but the workaround to install my custom settrace function again after calling CTracer's trace function would likely work (only for the "call" case anyway it seems). |
Related issue: #729 |
Additionally, I think the call to the trace function should return itself, so that it is idendical (via "is"). Some more info: import sys
t1 = sys.gettrace()
t2 = sys.gettrace()
assert t1 is t2
sys.settrace(t1)
assert t1 is t2
def w(*args):
print(args)
ret = t1(*args)
assert ret is t1, (ret, t1)
sys.settrace(w) Results in:
Edit: ok, "is" does not work with normal functions set via |
I am experimenting with monkeypatching
sys.settrace
to always call coverage.py's trace function, i.e. afterpdb.set_trace
, and especially with it usingsys.settrace(None)
etc.This works good already using the PyTracer, but for the CTracer it seems that its
trace
function needs to be made available.I've tried the following quickly, but it crashes, i.e. it likely needs changes to the handling of args - maybe an intermediate
CTracer_trace_public
C function?The text was updated successfully, but these errors were encountered: