getting from code object to class/function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Skip Montanaro

    getting from code object to class/function

    The trace module allows you to print all functions which were called at
    least once during a program's run. I just checked in a change to also track
    caller/callee relationships and display then at program exit.

    Both types of output suffer from the fact that all the trace function gets
    is the current frame. From there it can find the code object and then the
    source filename and function name. There is, however, no direct way back
    from the code object to (for example) the class and method which "own" that
    object. I didn't see anything obvious in the inspect module that would
    allow me to worm around this problem.

    A not perfect solution would seem to be: if the first element of the code's
    co_varnames attribute is "self" and the object in question is an instance,
    work back to the class and grab the class name (which is only available
    indirectly via str()), then use it to embellish the function name found in
    the code object.

    I'm open to other suggestions.

    Thx,

    Skip

  • Michael Hudson

    #2
    Re: getting from code object to class/function

    Skip Montanaro <skip@pobox.com > writes:
    [color=blue]
    > The trace module allows you to print all functions which were called at
    > least once during a program's run. I just checked in a change to also track
    > caller/callee relationships and display then at program exit.
    >
    > Both types of output suffer from the fact that all the trace function gets
    > is the current frame. From there it can find the code object and then the
    > source filename and function name. There is, however, no direct way back
    > from the code object to (for example) the class and method which "own" that
    > object. I didn't see anything obvious in the inspect module that would
    > allow me to worm around this problem.[/color]

    gc.get_referrer s?

    It's impossible in general, of course, because you can using the 'new'
    module construct multiple functions that refer to the same code
    object.

    Cheers,
    mwh

    --
    [1] If you're lost in the woods, just bury some fibre in the ground
    carrying data. Fairly soon a JCB will be along to cut it for you
    - follow the JCB back to civilsation/hitch a lift.
    -- Simon Burr, cam.misc

    Comment

    Working...