-
Notifications
You must be signed in to change notification settings - Fork 207
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
[Recommendation]: Don't mess around with builtins, that's what modules are for :) #96
Comments
Thanks for for reaching out @Yhg1s and @rwgk. Indeed, it would be nice to do this in a better way! Just for context, what is this good for? nanobind and pybind11 maintain some internal data structures about the bindings. Some complex binding projects are split up across multiple extension modules, and in that case those internal data structures must be shared. So this ugly capsule in It's actually kind of undesirable that this is even visible at all. The best solution would be if there is some "secret" dictionary only accessible by CPython (but not pure Python) code, where such things can be stashed. The problem I see with @Yhg1s, do you have any suggestions? |
For what it's worth, users can also delete things from builtins (with predictably catastrophic effects):
|
@Yhg1s, @JelleZijlstra: What about the Py_LIMITED_API function (FWIW the documentation of this function also mentions |
(By the way: @rwgk -- this may be an interesting coordinated switch to also do in pybind11 along with the internals ABI bump) |
There's nothing that is usable by an extension module, provided by Python, that isn't also usable by other modules. That includes builtins. (The builtins you're currently setting are even easier to access than the separate module I suggested, since you don't even need to import anything :) If you want some way to control who can change things, you will need to add access control yourself. A separate module with accessor functions may be what you're looking for there, but how you're going to prevent people from using it incorrectly, I don't know. |
Is it possible to access the dictionary |
I was looking for where this API came from originally, and it seems to serve a related purpose in the |
Previously, nanobind stored its internal data structures in the ``builtins`` dictionary, which was rightfully criticised in issue #96. This tentative commit instead uses the interpreter state dictionary exposed via ``PyInterpreterState_GetDict()``.
|
Previously, nanobind stored its internal data structures in the ``builtins`` dictionary, which was rightfully criticised in issue #96. This tentative commit instead uses the interpreter state dictionary exposed via ``PyInterpreterState_GetDict()``.
|
Okay, that makes sense. I am not trying to protect against such behavior. I moved storage of the internal capsule from The previous location (
For posterity, some answers to the question "what could possibly go wrong?":
|
From a performance perspective (ignoring code quality issues), it is fine to add values to
Python 3.11 is fairly forgiving of changing |
Problem description
The title is by @Yhg1s, I'm just the messenger here.
We happened to stumble over this in connection with pybind11, then I looked in nanobind and see that's also adding capsules to
builtins
:nanobind/src/nb_internals.cpp
Line 379 in 61393ad
Thomas wrote:
Reproducible example code
No response
The text was updated successfully, but these errors were encountered: