python / python/cpython

no sys.monitoring branches shown for `async for`

Open
#123,076 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

With the code from PR 122564, no branches are indicated for an async for. In the example below, both branches for the if also unexpectedly present (through co_locations) as "going out of scope":

import asyncio

async def g():
    yield 1
    yield 2

x = 0
async def fun():
    global x

    async for v in g():  # line 11
        if v > 0:
            x += v
    x += 3
    print(x)

asyncio.run(fun())

Results:

Disassembly of <code object fun at 0x103480870, file "/Users/juan/project/slipcover/ex11.py", line 8>:
   8           0       RETURN_GENERATOR
               2       POP_TOP
        L1:    4       RESUME                   0

  11           6       LOAD_GLOBAL              1 (g + NULL)
              16       CALL                     0
              24       GET_AITER
        L2:   26       GET_ANEXT
              28       LOAD_CONST               0 (None)
        L3:   30       SEND                     3 (to L6)
        L4:   34       YIELD_VALUE              1
        L5:   36       RESUME                   3
              38       JUMP_BACKWARD_NO_INTERRUPT 5 (to L3)
        L6:   40       END_SEND
        L7:   42       STORE_FAST               0 (v)

  12          44       LOAD_FAST                0 (v)
              46       LOAD_CONST               1 (0)
              48       COMPARE_OP             148 (bool(>))
        L8:   52       POP_JUMP_IF_TRUE         2 (to L9)
              56       JUMP_BACKWARD           17 (to L2)
        L9:   60       NOT_TAKEN

  13          62       LOAD_GLOBAL              2 (x)
              72       LOAD_FAST                0 (v)
              74       BINARY_OP               13 (+=)
              78       STORE_GLOBAL             1 (x)
              80       JUMP_BACKWARD           29 (to L2)

  11   L10:   84       CLEANUP_THROW
       L11:   86       JUMP_BACKWARD_NO_INTERRUPT 24 (to L6)
       L12:   88       END_ASYNC_FOR

  14          90       LOAD_GLOBAL              2 (x)
             100       LOAD_CONST               2 (3)
             102       BINARY_OP               13 (+=)
             106       STORE_GLOBAL             1 (x)

  15         108       LOAD_GLOBAL              5 (print + NULL)
             118       LOAD_GLOBAL              2 (x)
             128       CALL                     1
             136       POP_TOP
             138       RETURN_CONST             0 (None)

  --   L13:  140       CALL_INTRINSIC_1         3 (INTRINSIC_STOPITERATION_ERROR)
             142       RERAISE                  1
ExceptionTable:
  L1 to L2 -> L13 [0] lasti
  L2 to L4 -> L12 [1]
  L4 to L5 -> L10 [3]
  L5 to L7 -> L12 [1]
  L7 to L8 -> L13 [0] lasti
  L9 to L10 -> L13 [0] lasti
  L10 to L11 -> L12 [1]
  L12 to L13 -> L13 [0] lasti

'fun' branches:
    ex11.py 12:11-12:16 "v > 0" (fun@52) -> 12:11-12:16 "v > 0" (fun@58) [if/while -> out]
    ex11.py 12:11-12:16 "v > 0" (fun@52) -> 12:11-12:16 "v > 0" (fun@60) [if/while -> out]

@markshannon @nedbat

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied async-for example on CPython main and compare the branch output with the behavior introduced by PR 122564. Trace the sys.monitoring branch data and co_locations involved; done means async-for branches are reported and the if branches are no longer marked as going out of scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.