Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspect.isgenerator is broken #997

Open
smurfix opened this issue Apr 10, 2025 · 2 comments · May be fixed by #998
Open

inspect.isgenerator is broken #997

smurfix opened this issue Apr 10, 2025 · 2 comments · May be fixed by #998

Comments

@smurfix
Copy link
Contributor

smurfix commented Apr 10, 2025

This doesn't quite match what I'd expect.

MicroPython v1.24.0-400.g160c2931f.dirty on 2025-04-10; linux [GCC 14.2.0] version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> async def fn():
...     pass
...
>>> x=fn()
>>> inspect.isgeneratorfunction(x)
False  ## correct
>>> inspect.isgenerator(x)
False  ## wrong
>>> x
<generator object 'fn' at 7f5b2a9534a0>
>>> inspect.isgeneratorfunction(fn)
True  ## correct
>>> inspect.isgenerator(fn)
True  ## wrong
>>> 
@smurfix
Copy link
Contributor Author

smurfix commented Apr 10, 2025

Fix:

def isgenerator(obj):
    return isinstance(obj, type((lambda: (yield))()))

Note the additional parentheses.

dpgeorge added a commit to dpgeorge/micropython-lib that referenced this issue Apr 11, 2025
Also optimise both `isgenerator()` and `isgeneratorfunction()` so they use
the same lambda, and don't have to create it each time they are called.

Fixes issue micropython#997.

Signed-off-by: Damien George <[email protected]>
@dpgeorge dpgeorge linked a pull request Apr 11, 2025 that will close this issue
@dpgeorge
Copy link
Member

Yes, you're quite right, it's missing those parenthesis.

See #998 for a fix.

@dpgeorge dpgeorge linked a pull request Apr 11, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants