Author SzieberthAdam
Recipients SzieberthAdam
Date 2014-04-12.11:14:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1397301247.66.0.634427842107.issue21205@psf.upfronthosting.co.za>
In-reply-to
Content
I faced this particular issue by writing decorators for asyncio coroutines. 

I even posted a question to SO: http://stackoverflow.com/questions/23015626/how-to-decorate-an-asyncio-coroutine-to-retain-its-name

However, since that I realized the problem is more general. I believe that a generator object should inherit its function's __name__ attribute instead of ignoring it. Example:


################################################################
import functools

def decorated(genfunc):
    @functools.wraps(genfunc)
    def wrapper(*args, **kargs):
        print('inside wrapper')
        for x in genfunc(*args, **kargs):
            yield 'decorated: {!r}'.format(x)
    print('outside wrapper')
    print('wrapper.__name__: {!r}'.format(wrapper.__name__))
    return wrapper

@decorated
def simple_genfunc():
    """Testdoc."""
    yield from range(10)

if __name__ == '__main__':
    print('simple_genfunc.__name__: {!r}'.format(
        simple_genfunc.__name__))
    print('simple_genfunc().__name__: {!r}'.format(
        simple_genfunc().__name__))
################################################################

And its result:

Z:\>python -i genfuncdec.py
outside wrapper
wrapper.__name__: 'simple_genfunc'
simple_genfunc.__name__: 'simple_genfunc'
simple_genfunc().__name__: 'wrapper'
>>> simple_genfunc
<function simple_genfunc at 0x00C30420>
>>> simple_genfunc.__wrapped__
<function simple_genfunc at 0x00C304B0>
>>> simple_genfunc()
<generator object wrapper at 0x00C0EE18>
>>> simple_genfunc.__wrapped__()
<generator object simple_genfunc at 0x00C0ED28>

As you can see the generator object's __name__ remained the hardcoded one.
History
Date User Action Args
2014-04-12 11:14:07SzieberthAdamsetrecipients: + SzieberthAdam
2014-04-12 11:14:07SzieberthAdamsetmessageid: <1397301247.66.0.634427842107.issue21205@psf.upfronthosting.co.za>
2014-04-12 11:14:07SzieberthAdamlinkissue21205 messages
2014-04-12 11:14:06SzieberthAdamcreate