Finding defining class in a decorator

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

    #1

    Finding defining class in a decorator

    We have a tracing decorator that automatically logs enter/exits to/from
    functions and methods and it also figures out by itself the function
    call arguments values and the class or module the function/method is
    defined on. Finding the name of the class where the method we just
    entered was defined in is a bit tricky.

    Here's a snippet of the test code:

    class Base:
    @tracelevel(1)
    def foomethod(self, x, y=5, **kwds):
    return x+y

    class TClass(Base):
    @tracelevel(1)
    def foomethod(self, x, y=1, **kwds):
    return Base.foomethod( self, x, **kwds) + x + y

    t = TClass()
    t.foomethod(4, d=1)
    return

    The output format is irrelevant at this point but it should be
    something like:

    TClass:foometho d ...
    Base:foomethod ...

    correctly showing the name of the class where the entered method was
    defined on.

    We are also using the proposed decorator function in the future
    functools module, which looks like this:

    def _update_wrapper (decorated, func, deco_func):
    # Support naive introspection
    decorated.__mod ule__ = func.__module__
    decorated.__nam e__ = func.__name__
    decorated.__doc __ = func.__doc__
    decorated.__dic t__.update(func .__dict__)
    decorated.__dec orator__ = deco_func
    decorated.__dec orates__ = func

    def decorator(deco_ func):
    """Wrap a function as an introspection friendly decorator
    function"""
    def wrapper(func):
    decorated = deco_func(func)
    if decorated is func:
    return func
    _update_wrapper (decorated, func, deco_func)
    return decorated
    # Manually make this decorator introspection friendly
    _update_wrapper (wrapper, deco_func, functools_decor ator)
    return wrapper

    In our decorator, the part that figures out the name of the class where
    the wrapped method was defined in has to start with the assumption that
    the first argument is self and then find the defining class from it.
    This code fragment is in the wrapper function of the decorator:

    if numargs > 0:
    # at definition time, class methods are not methods
    # yet because the class doesn't exist when the
    # decorators get called and thus, we have to figure
    # out classname at runtime via self
    #
    # assume first arg is self, see if f.__name__ is there
    # as a method and if so, then grab it's class name
    #
    self = args[0]
    if type(self) == types.InstanceT ype:
    # getattr will find the method anywhere in the
    # class tree so start from the top
    bases = list(inspect.ge tmro(self.__cla ss__))
    bases.reverse()
    for c in bases:
    # f was given to us in the deco_func
    meth = getattr(c, f.__name__, None)

    # we found a method with that name, which
    # it's probably this same wrapper function
    # we used to wrap the original method with.

    ofunc = getattr(meth, '__decorates__' , False)

    if ofunc and ofunc.func_code == f.func_code:
    # got it
    clsname = meth.im_class._ _name__
    break

    Is there a way to do this without the __decorates__ attribute?

    --
    Luis P Caamano
    Atlanta, GA, USA

  • Ziga Seilnacht

    #2
    Re: Finding defining class in a decorator

    lcaamano wrote:[color=blue]
    > We have a tracing decorator that automatically logs enter/exits to/from
    > functions and methods and it also figures out by itself the function
    > call arguments values and the class or module the function/method is
    > defined on. Finding the name of the class where the method we just
    > entered was defined in is a bit tricky.[/color]

    [snipped]

    You might find this helpful:

    import sys

    def tracer(func):
    """
    A decorator that prints the name of the class from which it was
    called.

    The name is determined at class creation time. This works
    only in CPython, since it relies on the sys._getframe()
    function. The assumption is that it can only be called
    from a class statement. The name of the class is deduced
    from the code object name.
    """
    classframe = sys._getframe(1 )
    print classframe.f_co de.co_name
    return func


    if __name__ == '__main__':

    # this should print Test1

    class Test1(object):

    @tracer
    def spam(self):
    pass

    # this should print Test2

    class Test2(Test1):

    @tracer
    def spam(self):
    pass

    [color=blue]
    > --
    > Luis P Caamano
    > Atlanta, GA, USA[/color]

    Hope this helps,
    Ziga

    Comment

    • lcaamano

      #3
      Re: Finding defining class in a decorator

      Nice. I had to call _getframe(2) to account for the wrapper but the
      idea seems to work OK, that is, save the name of the decorator's
      original caller at definition time while creating the wrapper function.
      Much better than doing that at runtime.

      Thanks

      --
      Luis P Caamano
      Atlanta, GA USA

      Comment

      Working...