python / python/cpython

no sys.monitoring branches shown for `async for`

未關閉
#123,076 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先,在 CPython main 上重現所提供的 async-for 範例,並將分支輸出與 PR 122564 引入的行為進行比較。追蹤 sys.monitoring 的分支資料和涉及的 co_locations;完成標準是 async-for 分支會被回報,且 if 分支不再被標記為超出作用域。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
testing-qa, tooling
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。