-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
PEP 587: Init API v2 #1018
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
PEP 587: Init API v2 #1018
Conversation
* Add "PyConfig with dynamic memory allocations" section * Add functions: * PyWideStringList_Append(list, item) * PyConfig_SetString(config_str, str) * PyConfig_SetWideString(config_str, str) * PyConfig_SetWideStringFromString(config_str, str) * PyConfig_Read(config) * PyConfig_SetArgv(config, argc, argv) * PyConfig_SetWideArgv(config, argc, argv) * PyConfig_Clear(config) * Py_INIT_IS_ERROR(err) * Py_INIT_IS_EXIT(err) * Remove Py_INIT_USER_ERR(MSG) function * On Windows, PyInitError.exitcode type is now "unsigned int" and Py_ExitInitError() now calls ExitProcess(exitcode). * PyInitError: add private _type field. The "prefix" field becomes a private "_func" field. * Rename PyWideCharList to PyWideStringList
|
I'm working on an implementation of this API enhancement: https://bugs.python.org/issue36763 I should rework "Memory allocations and Py_DecodeLocale()" section, but I'm late for the Language Summit :-) |
|
@zooba: This PEP change basically implements what you requested, the ability to modify configuration after reading it. The code already existed, but in the first version of my PEP, I tried hard to make the API as simple as possible using "static PyConfig". You explained that that as an user, my API was too limited. So I changed the API to use "dynamc" PyConfig by default which requires to call PyConfig_Clear() when done, but add a separated section for "static PyConfig" which is the exception, rather than the default. cc @ncoghlan |
|
Another problem that I had is that PyConfig_SetWideString() and PyConfig_SetArgv() must pre-initialize Python to ensure that encodings are properly configured. PyConfig_SetArgv() uses the config argument to pre-initialize Python. PyConfig_SetWideString() doesn't pass the configuration to pre-initialize Python which can lead to mojibake if the configuration contains command line arguments which pick a different encoding. I'm not 100% sure of what happens exactly. Rather than implicitly pre-initializing Python, maybe these 2 methods should fail if Python is not pre-initialized explicitly, because of the corner cases. |
pep-0587.rst
Outdated
| usually rendered as ``prefix: msg``. | ||
| * ``user_err`` (int): if non-zero, the error is caused by the user | ||
| configuration, otherwise it's an internal Python error. | ||
| * ``exitcode`` (``unsigned int`` on Windows, ``int`` otherwise): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like having this distinction. If there are real problems with using unsigned int everywhere, I'd rather use int on Windows as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Unix, exit() takes an int. So I would prefer to use "int". In practice, currently Python only returns positive exit code (something like 0 <= exitcode <= 10, I'm not super of the upper bound).
pep-0587.rst
Outdated
|
|
||
| ``PyConfig`` methods: | ||
|
|
||
| * ``PyInitError PyConfig_SetString(char **config_str, const char *str)``: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These all take a PyConfig object/pointer as well, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usage is: PyConfig_SetString(&config->ATTR, "value") where ATTR is a PyConfig field. The method doesn't take a pointer to the PyConfig structure.
| * ``PyInitError PyConfig_Read(_PyCoreConfig *config)``: | ||
| Read all Python configuration | ||
| * ``void PyConfig_Clear(_PyCoreConfig *config)``: | ||
| Release memory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a lot more thoughts about this part of the API, but I like the direction. Let's chat :)
pep-0587.rst
Outdated
| * ``PyWideStringList_Append(list, item)`` | ||
| * ``PyConfig_SetString(config_str, str)`` | ||
| * ``PyConfig_SetWideString(config_str, str)`` | ||
| * ``PyConfig_SetWideStringFromString(config_str, str)`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder... can we define "PreInitialize" as "enough initialization to allocate str, tuple, list, dict objects" - which doesn't necessarily support arbitrary encodings, to be clear - but then we could use regular C APIs for defining the configuration and simplify this API (possibly even down to an eval equivalent if people really want to just do that ;) ).
Again, let's chat :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If possible, I would prefer to postpone this feature for Python 3.9. IMHO the code isn't ready, and this topic wasn't discussed much so far. At least, the PEP 587 design should allow to extend the API to support this use case later. Like: add a new field to PyConfig and new methods to "finish" the "initialization".
|
cc @Yhg1s |
PyConfig: Rename _check_hash_pycs_mode to check_hash_pycs_mode.
* Rename PyConfig_SetWideString to PyConfig_SetString * Rename PyConfig_SetWideStringFromString to PyConfig_DecodeString * Document -X command line options * Document PyPreConfig and PyConfig field types
Update open questions
Add "PyConfig with dynamic memory allocations" section
Add functions:
Remove Py_INIT_USER_ERR(MSG) function
On Windows, PyInitError.exitcode type is now "unsigned int"
and Py_ExitInitError() now calls ExitProcess(exitcode).
PyInitError: add private _type field. The "prefix" field becomes a
private "_func" field.
Rename PyWideCharList to PyWideStringList