question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Cannot inspect cythonized generator functions as such (`co_flags` don't have `CO_GENERATOR`)

See original GitHub issue

Describe 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:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
scodercommented, Jul 14, 2022

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.

0reactions
scodercommented, Jul 15, 2022

That code’s for checking if it’s legal to set the frame)s f_lineno. While tracing you can do that to jump execution around, but you can’t do it into/out of loops, exception blocks and some other things.

Thanks for looking through that.

Since Cython always has a blank co_code, I think it’ll always fail here in the setter so it’s not a problem. Maybe there should be a test for this though.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found