getting from code object to class/function

Discussion in 'Python' started by Skip Montanaro, Apr 7, 2004.

  1. 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
    Skip Montanaro, Apr 7, 2004
    #1
    1. Advertising

  2. Skip Montanaro <> writes:

    > 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.


    gc.get_referrers?

    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
    Michael Hudson, Apr 7, 2004
    #2
    1. Advertising

  3. Skip Montanaro <> writes:

    > >> 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.

    >
    > mwh> gc.get_referrers?
    >
    > Interesting idea.


    I'm not sure it'll work though: I don't think code objects participate
    in GC, so I don't know if function's traverse function call the
    callback on the code object.

    That said, I've just tried it, and it does work :)

    Cheers,
    mwh

    --
    We've had a lot of problems going from glibc 2.0 to glibc 2.1.
    People claim binary compatibility. Except for functions they
    don't like. -- Peter Van Eynde, comp.lang.lisp
    Michael Hudson, Apr 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    9
    Views:
    566
    justanotherguy
    Dec 3, 2004
  3. Petri Savolainen
    Replies:
    0
    Views:
    439
    Petri Savolainen
    Jul 1, 2003
  4. Sergio Correia
    Replies:
    7
    Views:
    285
    Ben Finney
    Sep 18, 2007
  5. DanielJohnson
    Replies:
    7
    Views:
    2,007
    James Kanze
    Jan 17, 2009
Loading...

Share This Page