← 返回首页
gh-74929: Make containment checks more efficient in `FrameLocalsProxy` by gaogaotiantian · Pull Request #118624 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

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

Provide feedback

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

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

gh-74929: Make containment checks more efficient in FrameLocalsProxy#118624

Merged
ncoghlan merged 1 commit into
python:mainfrom
gaogaotiantian:frameproxy-contain
May 6, 2024
Merged

gh-74929: Make containment checks more efficient in FrameLocalsProxy#118624
ncoghlan merged 1 commit into
python:mainfrom
gaogaotiantian:frameproxy-contain

Conversation

gaogaotiantian commented May 6, 2024
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Currently the more efficient contains function is not used when someone does key in frame.f_locals because that defaults to sq_contains and it's not properly hooked. The function is only called when using frame.f_locals.__contains__(key).

This hooks sq_contains so the in operation is much more efficient.

Copy link
Copy Markdown
Member

Is this a correctness issue or merely a performance issue?

Copy link
Copy Markdown
Member Author

This is merely a performance issue. Without this change, the CONTAINS_OP will treat the proxy as a normal iterable and check the keys one by one. The iter() method worked fine so the result is correct. A list of keys will be generated and iterated.

ncoghlan changed the title gh-74929: Make contains work properly for FrameLocalsProxy gh-74929: Make containment checks more efficient in FrameLocalsProxy May 6, 2024

ncoghlan left a comment
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

LGTM! (PR title tweaked to better reflect its purpose)

ncoghlan merged commit afbe5bf into python:main May 6, 2024
gaogaotiantian deleted the frameproxy-contain branch May 6, 2024 17:28
SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
…sProxy` (python#118624) Properly implement the `sq_contains` slot for frame locals proxy containment checks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Footer

© 2026 GitHub, Inc.