[Python-Dev] PEP 432 progress: Python initalization (original) (raw)

Victor Stinner victor.stinner at gmail.com
Thu Dec 14 10:16:15 EST 2017


Hi,

Serhiy Storchaka seems to be worried by the high numbers of commits in https://bugs.python.org/issue32030 "PEP 432: Rewrite Py_Main()", so let me explain the context of this work :-)

To prepare CPython to implement my UTF-8 Mode PEP (PEP 540), I worked on the implementation of Nick Coghlan's PEP 432:

PEP 432 -- Restructuring the CPython startup sequence https://www.python.org/dev/peps/pep-0432/

The startup sequence is a big pile of code made of multiple functions: main(), Py_Main(), Py_Initialize(), Py_Finalize()... and a lot of tiny "configuration" functions like Py_SetPath().

Over the years, many configuration options were added in the middle of the code. The priority of configuration options is not always correct between command line options, envrionment variables, configuration files (like "pyenv.cfg"), etc. For technical reasons, it's hard to impement properly the -E option (ignore PYTHON* environment variables).

For example, the new PYTHONCOERCECLOCALE environment variable (of PEP 538) doesn't handle properly -E (it ignores -E), because it was too complex to support -E. -- I'm working on fixing this.

Last weeks, I mostly worked on the Py_Main() function, Modules/getpath.c and PC/getpathp.c, to "refactor" the code:

My motivation was to write a correct implementation of the UTF-8 Mode (PEP 540).

Nick's motivation is to make CPython easily to embed. His plan for Python 3.8 is to give access to the new _PyCoreConfig and _PyMainInterpreterConfig structures to:

(I'm not sure that I understood correctly, so please read the PEP 432 ;-))

IMHO the most visible change of the PEP 432 is to split Python initialization in two parts:

The goal is to introduce the opportunity to configure Python between Core and Main.

The implementation is currently a work-in-progress. Nick will not have the bandwidth, neither do I, to update his PEP and finish the implementation, before Python 3.7. So this work remains private until at least Python 3.8.

Another part of the work is to enhance the documentation. You can for example now find an explicit list of C functions which can be called before Py_Initialize():

https://docs.python.org/dev/c-api/init.html#before-python-initialization

And also a list of functions that must not be called before Py_Initialize(), whereas you might want to call them :-)

Victor



More information about the Python-Dev mailing list