I've been using the sys.settrace function to write a tracer for my program, which is working great except that it doesn't seem to get called for built-in functions, like open('filename.txt'). This behavior doesn't seem to be documented, so I'm not sure if I'm doing something wrong or if this is the expected behavior. I am using Doug Hellmann's "trace_calls_and_returns" code from here as my tracing function.
If I can't do this with settrace, is there any way to trace calls to open()? I don't want to use Linux's strace, as it'll run for the whole program (not just the part I want to trace) and won't show my python line numbers/file names, etc. The other option I considered was monkey-patching the open function through a wrapper, like:
import traceback, __builtin__
def wrapped_open(*arg,**kw):
print 'open called'
traceback.print_stack()
f = __builtin__.open(*arg,**kw)
return f
open = wrapped_open
but that seemed very brittle to me.
Could someone suggest a better way of doing this?
You won't be able to trace compiled code imported into python, and open()
is not pure python, it's a C binding from CPython part of the Python distribution.
So the only way you'll have to trace within open()
is to go for strace
or an equivalent, but in any ways, get beyond what python can introspect of himself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With