Use a critical section to serialize adding builtins to globals#4921
Merged
davidhewitt merged 4 commits intoPyO3:mainfrom Feb 19, 2025
Merged
Use a critical section to serialize adding builtins to globals#4921davidhewitt merged 4 commits intoPyO3:mainfrom
davidhewitt merged 4 commits intoPyO3:mainfrom
Conversation
This was referenced Feb 18, 2025
Closed
Contributor
Author
|
I ran |
|
Works perfectly in our setup as well and looks good to me. Thank you! |
davidhewitt
reviewed
Feb 19, 2025
| // - https://github.com/python/cpython/pull/24564 (the same fix in CPython 3.10) | ||
| // - https://github.com/PyO3/pyo3/issues/3370 | ||
| let builtins_s = crate::intern!(self, "__builtins__").as_ptr(); | ||
| let has_builtins = unsafe { ffi::PyDict_Contains(globals.as_ptr(), builtins_s) }; |
Member
There was a problem hiding this comment.
While we're at it, perhaps let's just use safe APIs here?
Suggested change
| let has_builtins = unsafe { ffi::PyDict_Contains(globals.as_ptr(), builtins_s) }; | |
| let has_builtins = globals.contains(builtins)?; |
There was once a slight perf advantage where the raw FFI would have avoided a useless reference count incref / decref on the key, but since IntoPyObject we can now avoid that cost completely. So the safe API should be zero cost and shorter, safer code.
... and similar below.
7 tasks
davidhewitt
approved these changes
Feb 19, 2025
mgorny
pushed a commit
to mgorny/pyo3
that referenced
this pull request
Feb 19, 2025
…4921) * add test that panics because __builtins__ isn't available * use a critical section to serialize adding __builtins__ to __globals__ * add release note * use safe APIs instead of PyDict_Contains
davidhewitt
pushed a commit
that referenced
this pull request
Feb 20, 2025
* add test that panics because __builtins__ isn't available * use a critical section to serialize adding __builtins__ to __globals__ * add release note * use safe APIs instead of PyDict_Contains
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4913
Because
PyDictis a concurrent hash table, it's safe to first check if the dict has a key without holding a critical section then acquire the critical section and fill the dict.Also adds a test based on the reproducer that I verified fails on
main.