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 摘要。