[BUG] Cannot inspect cythonized generator functions as such (`co_flags` don't have `CO_GENERATOR`)
See original GitHub issueDescribe the bug Generator functions lose inspection after compiling
To Reproduce Code to reproduce the behaviour:
def func():
yield 1
when uncompiled:
In [1]: import inspect
In [2]: inspect.isgenerator(func())
Out[2]: True
In [3]: inspect.isgeneratorfunction(func)
Out[3]: True
In [4]: func.__code__.co_flags
Out[4]: 16777315
after being cythonized
In [3]: inspect.isgenerator(func())
Out[3]: True
In [4]: inspect.isgeneratorfunction(func)
Out[4]: False
In [5]: func.__code__.co_flags
Out[5]: 67
In [6]: type(func())
Out[6]: <class '_cython_3_0_0a10.generator'>
Expected behavior
Im not sure if this type of inspection is possible with cython_function_or_method
, but I was hoping to be able to tell if a given function is a generatorfunction or not
Environment (please complete the following information):
- OS: tested on mac and linux
- Python version tested on 3.9 and 3.10
- Cython version [e.g. 3_0_0a10]
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
inspect.iscoroutine only works for "native" coros - Google Groups
I know this is documented in PEP 492 but I'm struggling to understand why the inspect module has several test functions (iscoroutine, iscoroutinefunction, ......
Read more >How to test if Cython property is generator? - Stack Overflow
Why doesn't the usual way work? First, as you already probably know, there is no difference between inspect.isgeneratorfunction(.
Read more >Issue 30071: Duck-typing inspect.isfunction() - Python tracker
I'm not sure that the CPython inspect module should care about Cython objects. I don't think that Cython functions should count as Python ......
Read more >Untitled
Doctor who 50th anniversary 3d blu ray, Biology 10.1 vocabulary, How the american flag was made, Get 500 loan, Iftikhar thakur best performance...
Read more >Inspect live objects — Python 3.11.1 documentation
Source code: Lib/inspect.py The inspect module provides several useful functions to help get information about live objects such as modules, classes, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I looked through the usages of
CO_COROUTINE
in CPython (main) and they look safe, except for this one, which I don’t quite understand yet:https://github.com/python/cpython/blob/50b9a7762f06335277d9962edc8d39498601a4e4/Objects/frameobject.c#L208-L211
All other usages only occur for Python generator and coroutine objects, not for other objects that look like coroutines.
Thanks for looking through that.
I wouldn’t rely on that. Cython’s code objects aren’t as blank as they may seem, we emulate quite a bit of Python functionality in them for introspection. A test seems needed, as you wrote.