classification
Title: asyncio.ensure_future() AttributeError with “async def” coroutines
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, martin.panter, python-dev, vstinner, yselivanov
Priority: normal Keywords:

Created on 2015-06-24 08:35 by martin.panter, last changed 2015-06-24 14:57 by yselivanov. This issue is now closed.

Messages (4)
msg245727 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-06-24 08:35
While trying to port the example at <https://docs.python.org/3.5/library/asyncio-task.html#example-future-with-run-until-complete> to use “async def”, I discovered the ensure_future() function does not like the coroutine field name changes introduced in Issue 24400:

>>> asyncio.ensure_future(slow_operation(future))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/proj/python/cpython/Lib/asyncio/futures.py", line 196, in __repr__
    info = self._repr_info()
  File "/home/proj/python/cpython/Lib/asyncio/tasks.py", line 105, in _repr_info
    coro = coroutines._format_coroutine(self._coro)
  File "/home/proj/python/cpython/Lib/asyncio/coroutines.py", line 242, in _format_coroutine
    filename = coro.gi_code.co_filename
AttributeError: 'coroutine' object has no attribute 'gi_code'

I understand the gi_ names all changed to cr_, so I guess this code has to adjust as well.
msg245728 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-06-24 08:47
It's not the first time that a bug in found in _format_coroutine(). We need more unit tests!

Previous bug: https://github.com/python/asyncio/issues/222
msg245748 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-06-24 14:55
New changeset 8f4e738cb07f by Yury Selivanov in branch '3.5':
Issue #24495, #24400: Test asyncio.Task.repr in debug mode
https://hg.python.org/cpython/rev/8f4e738cb07f
msg245750 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-06-24 14:57
Should be fixed now. Thanks for discovering this, Martin!

Victor, I'll make a PR on github/asyncio with some refactoring or CoroWrapper etc.  The code became too cumbersome, and has to be properly refactored.  At least we should have one wrapper class for generator based coroutines, and another one for 'async def' ones.
History
Date User Action Args
2015-06-24 14:57:31yselivanovsetstatus: open -> closed
resolution: fixed
messages: + msg245750

stage: needs patch -> resolved
2015-06-24 14:55:48python-devsetnosy: + python-dev
messages: + msg245748
2015-06-24 08:47:29vstinnersetmessages: + msg245728
2015-06-24 08:35:27martin.pantercreate