Hi,

If you embed Python in Python, I would like your opinion on
https://bugs.python.org/issue42260 issue which adds a way to configure
the Python initialization in Python.

I'm looking for feedback from people who embed Python and have issues
to configure Python as they wish.

Simple example (just to show how the API would look like) setting the
the bytes_warning option:
----------------
import _testinternalcapi
import sys

assert sys.flags.bytes_warning == 0

config = _testinternalcapi.get_config()  # Copy PyConfig
config['bytes_warning'] = 1  # regular Python dict
_testinternalcapi.set_config(config)  # Update PyConfig and sys

assert sys.flags.bytes_warning == 1
----------------

The example already works in the current master branch!


The PyConfig structure has 53 members (+ 4 private members). You can
configure many things: sys.argv, sys.path, stdout encoding, disable
user site directory, etc.


In Python 3.8, I implemented PEP 587 "Python Initialization
Configuration" which is the concrete implementation of the first half
of PEP 432 "Restructuring the CPython startup sequence". Python now
provides a C API to configure every option of its initialization:

   https://docs.python.org/dev/c-api/init_config.html

Nick Coghlan recently withdrew his PEP 432 since PEP 587 was rejected.
It made me sad :-( I tried to implement the second half of the PEP
432:

* [DONE] Introduce the most limited "core" initialization phase where
Python only has builtin types and builtin modules (implemented as a
provisional C API in the PEP 587, I fixed some issues recently). sys
module is only partially initialized (no sys.path, no sys.stdout).
Python should not access the file system in his phase.

* [DONE] Only compute the Python path configuration during the main
initialization, not during the core initialization.

* [DONE] Ability to configure the Python initialization in Python.

* [WIP] Rewrite Modules/getpath.c (1600 lines of C code) to
Lib/_getpath.py (700 lines of Python code)

See https://bugs.python.org/issue42260 for the work-in-progress.


In Python 3.9, it's already possible to configure PyConfig in C. The
idea here is to give the ability to do it in Python, but also to
reimplement the old Modules/getpath.c as modern Python code.

In my WIP PR 23169, I reimplemented getpath.c in two parts:

* _getpath Python module: logic to build sys.path
* _cgetpath C extension used by _getpath: minimum C code to expose
Py_DecodeLocale() function and some constants in Python

It will be easier to maintain and evolve _getpath.py, but also to
write a test suite (right now, getpath.c has almost no test, see
test_embed).


Later, the idea would be to let applications embedding Python to
execute their own code to configure Python initialization options
(PyConfig) in Python between the "core" initialization phase and the
"main" initialization (Python fully initialized). It should not be run
after the main initialization, since the configuration is used to
configure Python during the main initialization.

>From a practical point of view, executing Python code to initialize
Python requires freezing this code as a frozen module (as I did for
_getpath.py in my PR). I don't know if it's easy to do that in an
application embedding Python right now.

My remaining problem is that my PR changes PyConfig_Read(): it no
longer computes the Python Path Configuration, since it is now
implemented in Python. IMO it's an acceptable drawback compared to the
benefit of these new features.

Victor
-- 
Night gathers, and now my watch begins. It shall not end until my death.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/HQNFTXOCDD5ROIQTDXPVMA74LMCDZUKH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to