New submission from Ned Batchelder <n...@nedbatchelder.com>:

Python 3.9 traces this code incorrectly.  Note: 3.8 and 3.10 are correct, only 
3.9 gets it wrong.

-----------------------------------
import linecache, sys

def trace(frame, event, arg):
    # The weird globals here is to avoid a NameError on shutdown...
    if frame.f_code.co_filename == globals().get("__file__"):
        lineno = frame.f_lineno
        print("{} {}: {}".format(event[:4], lineno, linecache.getline(__file__, 
lineno).rstrip()))
    return trace

import asyncio

async def async_gen():
    yield 13

async def async_test():
    global a
    a = 17
    async for i in async_gen():
        print(i + 19)
    else:
        a = 21

print(sys.version)
sys.settrace(trace)

asyncio.run(async_test())
assert a == 21
----------------------------------

The starred line shows a trace of a statement that is not executed:

  3.9.6 (default, Jul 13 2021, 07:21:14)
  [Clang 12.0.0 (clang-1200.0.32.29)]
  call 15: async def async_test():
  line 17:     a = 17
  line 18:     async for i in async_gen():
  call 12: async def async_gen():
  line 13:     yield 13
  retu 13:     yield 13
  exce 18:     async for i in async_gen():
  line 19:         print(i + 19)
  32
  line 18:     async for i in async_gen():
  call 13:     yield 13
  retu 13:     yield 13
  exce 18:     async for i in async_gen():
* line 19:         print(i + 19)
  line 21:         a = 21
  retu 21:         a = 21

----------
components: Interpreter Core
keywords: 3.9regression
messages: 397393
nosy: Mark.Shannon, nedbat
priority: normal
severity: normal
status: open
title: Python 3.9 traces async for/else incorrectly
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44621>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to