Skip to content

gh-137477: Fix inspect.getblock() for generator expressions #137488

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Aug 6, 2025

This fixes also inspect.getsourcelines() and inspect.getsource().

This fixes also inspect.getsourcelines() and inspect.getsource().
Lib/inspect.py Outdated
if token == "lambda":
self.islambda = True
self.started = True
else:
self.islambda = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain this part a bit to me? What's in the else case? Why is islambda set? I was a bit confused (without testing too much into it) when I read the code because I thought this is definitely not a lambda here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For def and class we search to the end of the block. For lambda and generator expressions we search to the end of the logical line. self.islambda is used to distinguish between these modes. We could rename it, but this will increase the size of the diff and may break user code that uses this private class, so I leave it for a time.

Although the code can be simplified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants