Skip to content

types._GeneratorWrapper is no longer consistent with types.GeneratorType/types.CoroutineType#143552

@x42005e1f

Description

@x42005e1f

Bug report

Bug description:

types.coroutine() uses _GeneratorWrapper as a wrapper for generator iterators in cases where the callable object is not a user-defined function (but is, for example, a generator function compiled via Cython/Nuitka). However, the implementation has not been updated for 11 years and therefore does not provide certain properties, such as:

As a result, the objects returned by the new function do not look exactly like types.GeneratorType/types.CoroutineType, which in particular makes them incompatible with the inspect.getgeneratorstate() and inspect.getcoroutinestate() functions:

>>>frominspectimportgetcoroutinestate, getgeneratorstate>>>fromtypesimportcoroutine>>>defgenerator_function(): ... return ... yield# generator definition>>> @coroutine ... defcoroutine_function(): ... returngenerator_function() >>>gencoro=coroutine_function() >>>getgeneratorstate(gencoro) Traceback (mostrecentcalllast): AttributeError: '_GeneratorWrapper'objecthasnoattribute'gi_suspended'>>>getcoroutinestate(gencoro) Traceback (mostrecentcalllast): AttributeError: '_GeneratorWrapper'objecthasnoattribute'cr_suspended'

Interestingly, there was already an attempt to add *_suspended in #133373 (see #133372 (comment)), but the PR was closed in favor of removing the TODO comment, and this side issue was apparently overlooked.

Related: cython/cython#7448.

CPython versions tested on:

3.11, 3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions