Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented May 1, 2019

  • 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

* 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
@vstinner
Copy link
Member Author

vstinner commented May 1, 2019

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 :-)

@vstinner
Copy link
Member Author

vstinner commented May 1, 2019

@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

@vstinner
Copy link
Member Author

vstinner commented May 1, 2019

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):
Copy link
Member

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.

Copy link
Member Author

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)``:
Copy link
Member

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?

Copy link
Member Author

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
Copy link
Member

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)``
Copy link
Member

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 :)

Copy link
Member Author

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".

@vstinner
Copy link
Member Author

vstinner commented May 2, 2019

cc @Yhg1s

vstinner added 7 commits May 1, 2019 23:24
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
@vstinner vstinner merged commit 8763930 into python:master May 2, 2019
@vstinner vstinner deleted the init_api_v2 branch May 2, 2019 19:14
ncoghlan pushed a commit to ncoghlan/peps that referenced this pull request May 7, 2019
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