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 nedbat
Recipients Mark.Shannon, nedbat
Date 2021-07-13.11:29:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1626175762.98.0.836185243325.issue44621@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2021-07-13 11:29:23nedbatsetrecipients: + nedbat, Mark.Shannon
2021-07-13 11:29:22nedbatsetmessageid: <1626175762.98.0.836185243325.issue44621@roundup.psfhosted.org>
2021-07-13 11:29:22nedbatlinkissue44621 messages
2021-07-13 11:29:22nedbatcreate