#36 Interaction with inspect

closed-wont-fix
nobody
5
2004-07-30
2004-06-17
Anonymous
No

>>> import inspect
>>> inspect.stack()
[(<frame object at 0xa119914>, '<stdin>', 1, '?', None,
None)]
>>> import psyco
>>> inspect.stack()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/tmp/python.4228/usr/lib/python2.3/inspect.py",
line 795, in stack
return getouterframes(sys._getframe(1), context)
File "/tmp/python.4228/usr/lib/python2.3/inspect.py",
line 776, in
getouterframes
framelist.append((frame,) + getframeinfo(frame,
context))
File "/tmp/python.4228/usr/lib/python2.3/inspect.py",
line 744, in
getframeinfo
raise TypeError('arg is not a frame or traceback
object')
TypeError: arg is not a frame or traceback object

Discussion

  • Armin Rigo
    Armin Rigo
    2004-06-17

    Logged In: YES
    user_id=4771

    Psyco returns custom frame objects emulating Python's, even when there is no Psyco-compiled code anywhere in the frame stack. This is because it is difficult to tell in advance if some frames will have to be emulated or not. So sys._getframe() returns instances of a custom class, which defeats inspect.isframe().

    I tend not to use the inspect module myself, as it is very dependent on this kind of low-level details; however, if it is really needed, I can provide a hack by which inspect.isframe() is patched dynamically to also accept Psyco's emulated frames.

     
  • George Sakkis
    George Sakkis
    2004-06-17

    Logged In: YES
    user_id=1065136

    Hi Armin,

    thanks for looking into this. I am using inspect for logging
    purposes (e.g. to get the names of functions in the stack), so
    I guess I could do without it, but I'm sure there are other
    programs that rely more on inspect. So, if you could make
    inspect and psyco work together, that would be very
    convenient.

     
  • Armin Rigo
    Armin Rigo
    2004-06-17

    Logged In: YES
    user_id=4771

    sys._getframe() is correctly emulated with Psyco, so you
    should be able to use sys._getframe(1) to get the parent
    frame, and then follow the f_back attributes to get to the
    previous ones. This is essentially what inspect does, but
    for some reason it first checks the type of the object
    returned by sys._getframe().

    Working around this limitation would be possible but it
    would be one more dirty hack (e.g. patching inspect
    dynamically when it is loaded). I think I'd rather
    recommend you to avoid the inspect module and its
    limitations if it is easy to do so.

     
  • Armin Rigo
    Armin Rigo
    2004-07-30

    • status: open --> closed-wont-fix