This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients asvetlov, giampaolo.rodola, ncoghlan, njs, yselivanov
Date 2018-01-29.05:36:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1517204210.25.0.467229070634.issue32703@psf.upfronthosting.co.za>
In-reply-to
Content
Looking at the ceval code, I think Yury's theory is plausible, and we may also be leaving the interpreter's internal stack in a dubious state. Things then get cleaned up if you wrap the async with in a try/except or try/finally:

==============
>>> async def try_main():
...     try:
...         async with open_file():
...             pass
...     finally:
...         pass
... 
>>> try_main().send(None)
sys:1: RuntimeWarning: coroutine 'open_file' was never awaited
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in try_main
AttributeError: __aexit__
==============

Unfortunately for that theory, adding braces and "Py_DECREF(POP());" to the relevant error handling branch *doesn't* fix the problem.

I also found another way to provoke similar misbehaviour without async with:

==========
>>> async def open_file():
...     pass
... 
>>> open_file()
<coroutine object open_file at 0x7f92fe19c548>
>>> _
<coroutine object open_file at 0x7f92fe19c548>
>>> 1
__main__:1: RuntimeWarning: coroutine 'open_file' was never awaited
1
>>> open_file()
<coroutine object open_file at 0x7f92fe19c548>
>>> del _
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
>>> _
<coroutine object open_file at 0x7f92fe19c548>
>>> 1
1
>>> _
1
>>> 
==========
History
Date User Action Args
2018-01-29 05:36:50ncoghlansetrecipients: + ncoghlan, giampaolo.rodola, njs, asvetlov, yselivanov
2018-01-29 05:36:50ncoghlansetmessageid: <1517204210.25.0.467229070634.issue32703@psf.upfronthosting.co.za>
2018-01-29 05:36:50ncoghlanlinkissue32703 messages
2018-01-29 05:36:49ncoghlancreate