top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

settrace doesn't trace builtin functions in python

0 votes

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'
 f =*arg,**kw)
 return f
open = wrapped_open

but that seemed very brittle to me. Could someone suggest a better way of doing this?

posted Jul 1, 2013 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

Similar Questions
0 votes

I have this innocent and simple code:

from collections import deque
exhaust_iter = deque(maxlen=0).extend
exhaust_iter.__doc__ = "Exhaust an iterator efficiently without caching any of its yielded values."

Obviously it does not work. Is there a way to get it to work simply and without creating a new scope (which would be a rather inefficient a way to set documentation, and would hamper introspection)?

How about dropping the "simply" requirement?

+1 vote

I am confused about how various built-in functions are called. Some are called with dot notation


and some are called like 'normal'


How do you know/remember which way to call them?

+3 votes

Is the byteorder (or endianness) of the functions in the audioop module somewhere specified or does anyone know how it behaves on different systems?

On my little-endian system it matches the system's endianness:

>>> import sys, audioop
>>> sys.byteorder
>>> audioop.lin2lin(b'xff', 1, 2)
0 votes

I'm writing a custom profiler that uses sys.settrace. I was wondering if there was any way of tracing the assignments of variables inside a function as its executed, without looking at locals() at every single line and comparing them to see if anything has changed.

Sort of like xdebug's collect_assignments parameter in PHP.

+1 vote

The following two functions return the same result:


But they may be computed in different ways. The first choice can accommodate non-integer powers and so it would logically proceed by taking a logarithm, multiplying by the power (in this case, 2), and then taking the anti-logarithm. But for a trivial value for the power like 2, this is clearly a wasteful choice. Just multiply x by itself, and skip the expensive log and anti-log steps.

My question is, what do Python interpreters do with power operators where the power is a small constant, like 2? Do they know to take the shortcut?

Useful Links with Similar Problem
Contact Us
+91 9880187415
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.