Re: Howto MACRO in python ?

Discussion in 'Python' started by Christian Tismer, Aug 12, 2003.

  1. Richie Hindle wrote:

    ....

    > Anyone know how I could retrieve function attributes from within a trace
    > function? It would be neater.


    Do you really mean function attributes, or just
    local variables? The latter are easily obtainable
    through the frame's f_locals.

    If you are just after the locals, you don't need to read on.

    When a frame is executed, the function is no longer
    available, just the source filename (from the code
    object), the name of the function and the frame's f_lineno.

    Guessing the function object from this info is not
    guaranteed to work, but will probably work if your
    code is static (not created dynamically, compiled
    and thrown away after starting it).
    With some luck, you can use the filename and the
    function name, import the according module and
    get the function object by the name.
    Then you can use function attributes.

    cheers - chris

    --
    Christian Tismer :^) <mailto:>
    Mission Impossible 5oftware : Have a break! Take a ride on Python's
    Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
    14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
    work +49 30 89 09 53 34 home +49 30 802 86 56 pager +49 173 24 18 776
    PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
    whom do you want to sponsor today? http://www.stackless.com/
    Christian Tismer, Aug 12, 2003
    #1
    1. Advertising

  2. On Tue, 12 Aug 2003 20:39:39 +0200, Christian Tismer <> wrote:

    >Richie Hindle wrote:
    >
    >...
    >
    >> Anyone know how I could retrieve function attributes from within a trace
    >> function? It would be neater.

    >
    >Do you really mean function attributes, or just
    >local variables? The latter are easily obtainable
    >through the frame's f_locals.
    >
    >If you are just after the locals, you don't need to read on.
    >
    >When a frame is executed, the function is no longer
    >available, just the source filename (from the code
    >object), the name of the function and the frame's f_lineno.
    >
    >Guessing the function object from this info is not
    >guaranteed to work, but will probably work if your
    >code is static (not created dynamically, compiled
    >and thrown away after starting it).
    >With some luck, you can use the filename and the
    >function name, import the according module and
    >get the function object by the name.
    >Then you can use function attributes.
    >

    maybe there ought to be a magic __self__ in the local namespace of a function?

    Or better yet (?;-) if a user-defined function were a subclass of type function
    instead of an instance, and a frame were an activation-instance of this function subclass,
    then frame.__class__.__dict__ would be the function __dict__, where you could find attributes.

    Binding of locals to passed args would be an __init__ function of the activation-instance,
    automatically followed by running the code (or maybe not, in the case of generators),
    and presumably a function (being a frame-instance-making class) could be customized more
    flexibly than we can now do with functions, including metaclass stuff, so you could e.g.
    define pre-bound local slot variables (just handwaving ;-). def would be syntactic sugar.

    ;-)

    Regards,
    Bengt Richter
    Bengt Richter, Aug 12, 2003
    #2
    1. Advertising

  3. Christian Tismer

    Sean Ross Guest

    "Bengt Richter" <> wrote in message
    news:bhbhsm$2vv$0@216.39.172.122...
    > maybe there ought to be a magic __self__ in the local namespace of a

    function?

    I've played with trying to get something like that, but I haven't got
    anything reliable[1]. So far I have a very simple function that retrieves
    the calling function object:

    import inspect

    class ThisResolutionError(Exception): pass

    def this():
    fname = inspect.currentframe(1).f_code.co_name
    frameno = 2
    func = None
    while func is None:
    try:
    func = inspect.currentframe(frameno).f_locals.get(fname, None)
    frameno += 1
    except ValueError:
    # reached end of call stack
    raise ThisResolutionError, "could not resolve %s" % fname
    return func


    So, when it works (which is not always), you can do things like:

    def f():
    "f's doc string"
    __self__ = this()
    print __self__.__name__
    print __self__.__doc__
    print __self__.__dict__

    def g():
    __self__ = this()
    print "called nested function %s" % __self__.__name__
    g()


    f.f_attr = "function attribute"
    f()

    -------------------output ----------------------
    f
    f's doc string
    {'f_attr': 'function attribute'}
    called nested function g



    I would be interested to see something more general - something that could
    be called inside of any namespace (at the module, class, method, or function
    level) which you could then (atleast) query and (possibly) modify. Why would
    I want to do that? Fun, mostly. I like the idea of being able to query any
    object while being inside the object.

    +1 __self__

    Sean


    [1] this() will not work in methods, and there can be issues if you put it
    in nested functions

    #
    # you can do this (if you want)
    #
    def f():
    __self__ = this()
    def g():
    print "called nested function g"
    __self__.g = g

    f()
    f.g()

    ----------------------
    called nested function g

    #
    # but not this (the reason for which should be obvious)
    #
    def f():
    __self__ = this()
    def g():
    __self__ = this()
    print "called nested function %s" % __self__.__name__
    __self__.g = g

    f()
    f.g()

    -------------------------
    ThisResolutionError: could not resolve g
    Sean Ross, Aug 12, 2003
    #3
  4. Bengt Richter wrote:

    ....

    > maybe there ought to be a magic __self__ in the local namespace of a function?
    >
    > Or better yet (?;-) if a user-defined function were a subclass of type function
    > instead of an instance, and a frame were an activation-instance of this function subclass,
    > then frame.__class__.__dict__ would be the function __dict__, where you could find attributes.


    I have similar thoughts, here, maybe simpler:
    A frame is something like an instance of a function,
    the function is somehow instantiated to be executed.

    Unfortunately, on creation of a frame, the function
    is "unpacked", in the sense that certain information
    is used to parameterize the frame, but then the frame
    has only the code object left.
    This is probably so, because frames are created in
    different context, like in the executable part of
    a class, or when the code of a script is run.
    There is no function, but there is a frame with a code
    object.

    Maybe it makes sense to just add the function/method
    to the code object as a new slot, which is either filled
    or set to None?
    I guess that not to do this was a decision from the
    pre-GC time, where this would have been a really bad
    circular reference?

    Then we could grow a frame attribute like __callable__
    which grabs the callable from the code object.
    Maybe we should ask python-dev what they think.

    ciao - chris
    --
    Christian Tismer :^) <mailto:>
    Mission Impossible 5oftware : Have a break! Take a ride on Python's
    Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
    14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
    work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
    PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
    whom do you want to sponsor today? http://www.stackless.com/
    Christian Tismer, Aug 13, 2003
    #4
    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. Dead RAM
    Replies:
    20
    Views:
    1,093
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    565
    Jack Klein
    Sep 21, 2003
  3. Bill Loren

    Re: Howto MACRO in python ?

    Bill Loren, Aug 12, 2003, in forum: Python
    Replies:
    1
    Views:
    324
    Sean Ross
    Aug 12, 2003
  4. Bob Gailer

    Re: Howto MACRO in python ?

    Bob Gailer, Aug 12, 2003, in forum: Python
    Replies:
    0
    Views:
    866
    Bob Gailer
    Aug 12, 2003
  5. Richard Meister

    Howto check value of constants in a macro

    Richard Meister, Nov 22, 2005, in forum: C Programming
    Replies:
    11
    Views:
    450
    Michael Mair
    Nov 23, 2005
Loading...

Share This Page