Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-70764: inspect.getclosurevars now identifies global variables with LOAD_GLOBAL#120143

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@blhsing
Copy link
Contributor

@blhsingblhsing commentedJun 6, 2024
edited by bedevere-appbot
Loading

inspect.getclosurevars now identifies global variables withLOAD_GLOBAL

Currentlyinspect.getclosurevars identifies a global variable by testing if a name inco_names exists in the function's global namespace, but this approach can result in incorrectly classifying an attribute name as a global variable when the name exists both as an attriute and as a global variable.

This PR fixes the issue by using theLOAD_GLOBAL andLOAD_ATTR instructions instead to identify global names and unbound names, respectively.

@carljm
Copy link
Member

Thanks for the contribution!

This could makegetclosurevars much slower if run on a very large function, because bytecode size can scale much faster than the set of names used.

In particular, if a large function has lots of references to builtins (even the same builtin used many times) we will repeatedly raise and catch an exception, possibly many times for the same builtin name. This repeated exception raising could at least be mitigated by keeping a set of seen global names, and not re-looking-up an already-seen name (or just avoiding re-looking-up names already present inbuiltin_vars).

That said, I'm not sure I see any other implementation strategy for fixing this function; we just don't preserve enough context from compilation on the code object, except in the bytecode itself.

Would like to see if any other reviewer has better ideas here, before merging this.

@blhsing
Copy link
ContributorAuthor

blhsing commentedJun 7, 2024
edited
Loading

This could makegetclosurevars much slower if run on a very large function, because bytecode size can scale much faster than the set of names used.

In particular, if a large function has lots of references to builtins (even the same builtin used many times) we will repeatedly raise and catch an exception, possibly many times for the same builtin name. This repeated exception raising could at least be mitigated by keeping a set of seen global names, and not re-looking-up an already-seen name (or just avoiding re-looking-up names already present inbuiltin_vars).

That said, I'm not sure I see any other implementation strategy for fixing this function; we just don't preserve enough context from compilation on the code object, except in the bytecode itself.

Would like to see if any other reviewer has better ideas here, before merging this.

Thanks for the review! I totally agree with your assessments. I've updated the approach so that it collects all global names in a set first before looking them up in global and builtin namespaces. And yeah unfortunately I don't see a way not to scan through all the bytecodes if we are to aim for accuracy.

Copy link
Member

@carljmcarljm left a comment

Choose a reason for hiding this comment

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

This will still be a slowdown, but not raising a lot of exceptions should help a lot, andinspect methods are often slow; correctness seems more important. And no better idea for how to make this method correct has surfaced; I don't think there is one. Going to go ahead and rebase this to make sure no other changes have impacted it in the last months, and then merge if signal looks good. Thanks again for the contribution!

blhsing reacted with thumbs up emoji
@carljmcarljm merged commit83ba8c2 intopython:mainNov 5, 2024
36 checks passed
@carljmcarljm added the needs backport to 3.13bugs and security fixes labelNov 5, 2024
@miss-islington-app
Copy link

Thanks@blhsing for the PR, and@carljm for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@carljmcarljm added the needs backport to 3.12only security fixes labelNov 5, 2024
@miss-islington-app
Copy link

Thanks@blhsing for the PR, and@carljm for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestNov 5, 2024
…s with LOAD_GLOBAL (pythonGH-120143)(cherry picked from commit83ba8c2)Co-authored-by: blhsing <blhsing@gmail.com>
@bedevere-app
Copy link

GH-126459 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelNov 5, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestNov 5, 2024
…s with LOAD_GLOBAL (pythonGH-120143)(cherry picked from commit83ba8c2)Co-authored-by: blhsing <blhsing@gmail.com>
@bedevere-app
Copy link

GH-126460 is a backport of this pull request to the3.12 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.12only security fixes labelNov 5, 2024
carljm pushed a commit that referenced this pull requestNov 6, 2024
…es with LOAD_GLOBAL (GH-120143) (#126460)gh-70764: inspect.getclosurevars now identifies global variables with LOAD_GLOBAL (GH-120143)(cherry picked from commit83ba8c2)Co-authored-by: blhsing <blhsing@gmail.com>
carljm pushed a commit that referenced this pull requestNov 6, 2024
…es with LOAD_GLOBAL (GH-120143) (#126459)gh-70764: inspect.getclosurevars now identifies global variables with LOAD_GLOBAL (GH-120143)(cherry picked from commit83ba8c2)Co-authored-by: blhsing <blhsing@gmail.com>
picnixz pushed a commit to picnixz/cpython that referenced this pull requestDec 8, 2024
ebonnal pushed a commit to ebonnal/cpython that referenced this pull requestJan 12, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@carljmcarljmcarljm approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@blhsing@carljm

[8]ページ先頭

©2009-2025 Movatter.jp