Skip to content

sys.monitoring PY_YIELD leaks internal async_generator_wrapped_value object #129013

@jaltmayerpizzorno

Description

@jaltmayerpizzorno

Bug report

Bug description:

Rather than the actual value, sys.monitoring's PY_YIELD event passes in to the callback the internal async_generator_wrapped_value.
I imagine this is a bug... it's inconsistent with what it does for non-async generators.

import sys
import types
import typing
import asyncio

async def gen():
    yield 42

async def main():
    async for _ in gen():
        pass

def handle_yield(code: types.CodeType, offset: int, value: object) -> typing.Any:
    if code.co_filename == __file__:
        print(f"yield name={code.co_qualname} {type(value)=}")

sysmon = sys.monitoring
sysmon.use_tool_id(4, "r")
sysmon.register_callback(4, sysmon.events.PY_YIELD, handle_yield)
sysmon.set_events(4, sysmon.events.PY_YIELD)

asyncio.run(main())

This prints out:

yield name=gen type(value)=<class 'async_generator_wrapped_value'>

CPython versions tested on:

3.12, 3.13, 3.14

Operating systems tested on:

macOS, Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions