[issue29944] Argumentless super() fails in classes constructed with type()

Dan Snider report at bugs.python.org
Sun Apr 9 00:02:27 EDT 2017


Dan Snider added the comment:

Just wanted to add that I found this when I was trying to find a way to decorate all methods in a class without using a metaclass or modifying __getattr__.


Using Josh's workaround, here's a simple demonstration that (at least at first glance) prints a message when a method is called and when it is finished:

def mydec(*, enter=True, exit=True):
    def make_closure(__class__):
        return (lambda: super).__closure__
    def outer(cls):
        def wrapper(method):
            @functools.wraps(method)
            def inner(*args, **kwargs):
                if enter:
                    print('entering', method.__qualname__)
                r = method(*args, **kwargs)
                if exit:
                    print('exiting', method.__qualname__)
                return method(*args, **kwargs)
            return inner    
        for name, value in cls.__dict__.items():
            if isinstance(value, types.FunctionType):
                method = types.FunctionType(getattr(cls, name).__code__,
                                            globals(),
                                            closure=make_closure(cls))
                setattr(cls, name, wrapper(method))
        return cls
    return outer

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29944>
_______________________________________


More information about the Python-bugs-list mailing list