settrace doesn't trace builtin functions

skunkwerk skunkwerk at gmail.com
Sun Jun 30 22:11:55 EDT 2013


Hi,
  I've been using the settrace function to write a tracer for my program, which is working great except that it doesn't seem to work for built-in functions, like open('filename.txt').  This doesn't seem to be documented, so I'm not sure if I'm doing something wrong or that's the expected behavior.

If settrace's behavior in this regard is fixed, is there any way to trace calls to open()?
I don't want to use Linux's strace, as it'll run for whole program (not just the part I want) 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:

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?

thank you,
imran



More information about the Python-list mailing list