On Fri, Jan 24, 2014 at 4:28 PM, Tobias Oberstein
<[email protected]> wrote:
> Am 25.01.2014 01:13, schrieb Guido van Rossum:
>
>> On Fri, Jan 24, 2014 at 2:37 PM, Tobias Oberstein
>> <[email protected]> wrote:
>>>
>>> I have noticed that `inspect.getargspec` won't return useful information
>>> on
>>> functions decorated with `@asyncio.coroutine`.
>>> (Sidenote: same with Twisted's `@inlineCallbacks`)
>>
>>
>> I presume you are talking about Trollius? It works for Tulip:
>
>
> Nope, Python 3.4.0b1 .. I get this:
>
> @asyncio.coroutine
> def foo1(a, b = 3):
>    yield from asyncio.sleep(1)
>    return a + b
>
> @asyncio.coroutine
> def foo2(a, b = 3):
>    return a + b
>
> print(inspect.getargspec(foo1))
> print(inspect.getargspec(foo2))
>
> $ python3 test2.py
> ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=(3,))
> ArgSpec(args=[], varargs='args', keywords='kw', defaults=None)
>
> ==
>
> But yes, it indeed does work if there is a yield in the function (hence the
> function actually is a coroutine). But it does not work if there is a
> decorator, but the function is plain. Strange. Is this expected behavior?

When there's no yield[from] in the decorated function, the decorator
makes one up:

    if inspect.isgeneratorfunction(func):
        coro = func
    else:
        @functools.wraps(func)
        def coro(*args, **kw):
            res = func(*args, **kw)
            if isinstance(res, futures.Future) or inspect.isgenerator(res):
        res = yield from res
            return res

You're seeing the signature of the wrapper function (coro).

You should read the source code for inspect.py (and for wraps in
functools.py) to follow exactly how come signature() does the right
thing but getargspec() doesn't; I think it is because signature()
calls unwrap() but getargspec() doesn't. If you'd like to see that
fixed, file a bug on the Python bug tracker (bugs.python.org) -- I
suspect there are backwards compatibility concerns that prevent us
from just calling unwrap() in getargspec().

It's possible that you can also fix this by doing something extra in
the @coroutine decorator code shown above but I don't immediately see
it.

>>> However, `inspect.signature` from
>>>
>>> http://www.python.org/dev/peps/pep-0362/
>>>
>>>   _does_ work (means: returns the actual function signature).
>>>
>>> Will this be made available on Python 2, or are there fundamental reasons
>>> this won't work out?
>>
>>
>> If it requires modifications to the inspect module it won't be
>> backported (new feature time is over). If it can be accomplished by
>
>
> ;( I see.
>
>
>> tweaking Trollius' @coroutine implementation, it might be done, if
>> someone who would know how to do this cares enough. Peresonally I
>> don't know and I don't care. :-(
>>
>



-- 
--Guido van Rossum (python.org/~guido)

Reply via email to