-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Allow efficient read access to PyConfig options #99872
Comments
So far, nobody asked for a public API for that. The latest discussion was in issue #93103 which there was no concrete proposed API. The thing is that most values are only used to initialize Python module attributes, and then the config is no longer update. A good example is An older discussion was issue #78100 "Expose _PyCoreConfig structure to Python". The issue was fixed by using PYTHONHASHSEED environment variable to solve his issue, exposing PyConfig wouldn't help for this issue.
I added
Why not using |
By the way, |
I understand that it was deprecated in the context of subinterpreters and local configuration. It seems reasonable to go through the thread state instead to read the config value. If I was to read it from something as complex as I also second the interest of reading the up-to-date configuration, not just something that was initially passed into Python's setup and was then subject to dynamic changes. But it seems to me that It seems really funny that during the time when PEP 587 decided to make all public global configuration variables obsolete, no-one came and said "wait a minute, they used to be public and might still be of interest to someone – shouldn't we provide users with a way to read the current interpreter configuration?"
I think I'll use them in CPython >= 3.9 then at least. If Py3.12 gains an official way to read the config, I'll switch to that. |
…he value from the interpreter's current PyConfig. See python/cpython#99872
I just noticed that they are not a complete replacement, because it was previously possible (and reasonable) to read the global variable without holding the GIL. This is no longer possible when going through the |
@gpshead @ericsnowcurrently @corona10: Do you have an option on which API should be added? PEP 587 makes the PyConfig structure public, so maybe Note: "read-only" in C is not well defined with types. There are ways to modify some configuration parameters using
PyConfig is a C API, but most people use Python with the Python API. In Python, reading PyConfig is an API to initialize Python. As I explained, then you should read the "current" configuration (without PyConfig), since many configuration options can be changed, and so PyConfig becomes outdated. I think it was @zooba who proposed recently to even remove
What's your use case? Which configuration variable do you want to read without holding the GIL? To avoid inconsistencies, maybe functions like I'm talking about this problem:
|
I'm not sure if that is even true. If you consider the code that runs on user machines, then a lot of that is in extension modules that use the C-API. ISTM that Python's C-API is just as important as its Python APIs.
Cython reads the Now, as you suggested, the concept of "read-only" is not well established in C, which makes it unclear whether I would guess that other candidates are Generally speaking, there should be one place of truth for these values. That used to be the global variables, but now |
Currently, it's no longer possible to change this value at runtime. I suggest you to read the value at startup (ex: when an extension is loaded, in its "init" function) and stores a copy of the value to avoid any GIL or complicated function calls. Before PyConfig (Python 3.7), it wasn't possible to set |
The most up to date configuration is in the Python API, where it always has been. Most of this is the To make the canonical source of configuration be a different structure would be a massive internal restructure, and probably a performance regression for users in Python. It's doable, but it's a ton of work. The point of the |
I agree, use the If you do find some config value that is modified at runtime and you regularly need to get the current version of that efficiently via a public C API, we can consider a specific API for that. Most of PyConfig is read only. All of it probably should be, so finding things that do change at runtime is worthy of us considering if we're even handling them internally in the right manner. |
…he value from the interpreter's current PyConfig. See python/cpython#99872
…the old Py_OptimizeFlag. The flag was never meant to be modifiable and thus can be read once at module import time. See python/cpython#99872 (comment)
* Work around the deprecation of Py_OptimizeFlag in Py3.12 by reading the value from the interpreter's current PyConfig. See python/cpython#99872 * Avoid access to PyConfig without holding the GIL when trying to read the old Py_OptimizeFlag. The flag was never meant to be modifiable and thus can be read once at module import time. See python/cpython#99872 (comment)
* Work around the deprecation of Py_OptimizeFlag in Py3.12 by reading the value from the interpreter's current PyConfig. See python/cpython#99872 * Avoid access to PyConfig without holding the GIL when trying to read the old Py_OptimizeFlag. The flag was never meant to be modifiable and thus can be read once at module import time. See python/cpython#99872 (comment)
Update: right now, Cython 3.0 fails to build on Python 3.13 since I removed private _PyInterpreterState_GetConfig() and Cython uses it: see issue #107076. We should provide an efficient replacement for this Cython code: _PyInterpreterState_GetConfig(__Pyx_PyThreadState_Current->interp)->optimization_level By the way, I'm not fully comfortable with Parser/pegen.c and Parser/tokenizer.c which use a private API: #ifdef Py_DEBUG
p->debug = _Py_GetConfig()->parser_debug;
#endif |
Documentation
The documentation seems to be geared towards embedding scenarios and modifying the configuration, and I couldn't find an example for simply reading a PyConfig field.
PEP 587 seems similarly unhelpful.
Looking through the CPython sources, it appears that what I want to do is this:
I'm pretty sure that this is not the official API way to do it, but it's unclear to me what the right way is, given that
PyInterpreterState
is an opaque struct and the previously simple access toPy_OptimizeFlag
has been deprecated as of Py3.12.Also, it's unclear what way of access should be used in the limited API.
The text was updated successfully, but these errors were encountered: