Skip to content

Commit

Permalink
bpo-42260: Fix _PyConfig_Read() if compute_path_config=0 (pythonGH-23220
Browse files Browse the repository at this point in the history
)

Fix _PyConfig_Read() if compute_path_config=0: use values set by
Py_SetPath(), Py_SetPythonHome() and Py_SetProgramName(). Add
compute_path_config parameter to _PyConfig_InitPathConfig().

The following functions now return NULL if called before
Py_Initialize():

* Py_GetExecPrefix()
* Py_GetPath()
* Py_GetPrefix()
* Py_GetProgramFullPath()
* Py_GetProgramName()
* Py_GetPythonHome()

These functions no longer automatically computes the Python Path
Configuration. Moreover, Py_SetPath() no longer computes
program_full_path.
  • Loading branch information
vstinner committed Nov 10, 2020
1 parent 1e996c3 commit ace3f9a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 95 deletions.
36 changes: 36 additions & 0 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ Process-wide parameters
The returned string points into static storage; the caller should not modify its
value.

This function should not be called before :c:func:`Py_Initialize`, otherwise
it returns ``NULL``.

.. versionchanged:: 3.10
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.


.. c:function:: wchar_t* Py_GetPrefix()
Expand All @@ -389,6 +395,12 @@ Process-wide parameters
script at build time. The value is available to Python code as ``sys.prefix``.
It is only useful on Unix. See also the next function.

This function should not be called before :c:func:`Py_Initialize`, otherwise
it returns ``NULL``.

.. versionchanged:: 3.10
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.


.. c:function:: wchar_t* Py_GetExecPrefix()
Expand Down Expand Up @@ -424,6 +436,12 @@ Process-wide parameters
while having :file:`/usr/local/plat` be a different filesystem for each
platform.

This function should not be called before :c:func:`Py_Initialize`, otherwise
it returns ``NULL``.

.. versionchanged:: 3.10
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.


.. c:function:: wchar_t* Py_GetProgramFullPath()
Expand All @@ -437,6 +455,12 @@ Process-wide parameters
static storage; the caller should not modify its value. The value is available
to Python code as ``sys.executable``.

This function should not be called before :c:func:`Py_Initialize`, otherwise
it returns ``NULL``.

.. versionchanged:: 3.10
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.


.. c:function:: wchar_t* Py_GetPath()
Expand All @@ -455,8 +479,14 @@ Process-wide parameters
can be (and usually is) modified later to change the search path for loading
modules.

This function should not be called before :c:func:`Py_Initialize`, otherwise
it returns ``NULL``.

.. XXX should give the exact rules
.. versionchanged:: 3.10
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.


.. c:function:: void Py_SetPath(const wchar_t *)
Expand Down Expand Up @@ -638,6 +668,12 @@ Process-wide parameters
:c:func:`Py_SetPythonHome`, or the value of the :envvar:`PYTHONHOME`
environment variable if it is set.
This function should not be called before :c:func:`Py_Initialize`, otherwise
it returns ``NULL``.
.. versionchanged:: 3.10
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
.. _threads:
Expand Down
38 changes: 21 additions & 17 deletions Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ There are two kinds of configuration:
environments variables are ignored, the LC_CTYPE locale is left unchanged and
no signal handler is registred.

The :c:func:`Py_RunMain` function can be used to write a customized Python
program.

See also :ref:`Initialization, Finalization, and Threads <initialization>`.

.. seealso::
:pep:`587` "Python Initialization Configuration".


Example
=======

Expand Down Expand Up @@ -532,7 +536,7 @@ PyConfig
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` output.
Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: wchar_t* base_executable
Expand All @@ -544,15 +548,15 @@ PyConfig
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` output.
Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: wchar_t* base_prefix
:data:`sys.base_prefix`.
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` output.
Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: int buffered_stdio
Expand Down Expand Up @@ -634,7 +638,7 @@ PyConfig
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` output.
Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: wchar_t* executable
Expand All @@ -643,7 +647,7 @@ PyConfig
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` output.
Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: int faulthandler
Expand Down Expand Up @@ -726,7 +730,7 @@ PyConfig
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` input.
Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. c:member:: int import_time
Expand Down Expand Up @@ -817,7 +821,7 @@ PyConfig
Default: value of the ``PLATLIBDIR`` macro which is set at configure time
by ``--with-platlibdir`` (default: ``"lib"``).
Part of the :ref:`Path Configuration <init-path-config>` input.
Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. versionadded:: 3.9
Expand All @@ -830,22 +834,22 @@ PyConfig
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` input.
Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. c:member:: PyWideStringList module_search_paths
.. c:member:: int module_search_paths_set
Module search paths: :data:`sys.path`.
If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the
function calculating the :ref:`Path Configuration <init-path-config>`
function calculating the :ref:`Python Path Configuration <init-path-config>`
overrides the :c:member:`~PyConfig.module_search_paths` and sets
:c:member:`~PyConfig.module_search_paths_set` to ``1``.
Default: empty list (``module_search_paths``) and ``0``
(``module_search_paths_set``).
Part of the :ref:`Path Configuration <init-path-config>` output.
Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: int optimization_level
Expand Down Expand Up @@ -911,15 +915,15 @@ PyConfig
.. c:member:: int pathconfig_warnings
On Unix, if non-zero, calculating the :ref:`Path Configuration
On Unix, if non-zero, calculating the :ref:`Python Path Configuration
<init-path-config>` can log warnings into ``stderr``. If equals to 0,
suppress these warnings.
It has no effect on Windows.
Default: ``1`` in Python mode, ``0`` in isolated mode.
Part of the :ref:`Path Configuration <init-path-config>` input.
Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. c:member:: wchar_t* prefix
Expand All @@ -928,7 +932,7 @@ PyConfig
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` output.
Part of the :ref:`Python Path Configuration <init-path-config>` output.
.. c:member:: wchar_t* program_name
Expand All @@ -946,7 +950,7 @@ PyConfig
Default: ``NULL``.
Part of the :ref:`Path Configuration <init-path-config>` input.
Part of the :ref:`Python Path Configuration <init-path-config>` input.
.. c:member:: wchar_t* pycache_prefix
Expand Down Expand Up @@ -1262,7 +1266,7 @@ and user site directory. The C standard streams (ex: ``stdout``) and the
LC_CTYPE locale are left unchanged. Signal handlers are not installed.
Configuration files are still used with this configuration. Set the
:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
:ref:`Python Path Configuration <init-path-config>` ("output fields") to ignore these
configuration files and avoid the function computing the default path
configuration.
Expand All @@ -1287,8 +1291,8 @@ and :ref:`Python UTF-8 Mode <utf8-mode>`
.. _init-path-config:
Path Configuration
==================
Python Path Configuration
=========================
:c:type:`PyConfig` contains multiple fields for the path configuration:
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ Porting to Python 3.10
``unicodedata.ucnhash_CAPI`` has been moved to the internal C API.
(Contributed by Victor Stinner in :issue:`42157`.)

* :c:func:`Py_GetPath`, :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and
:c:func:`Py_GetProgramName` functions now return ``NULL`` if called before
:c:func:`Py_Initialize` (before Python is initialized). Use the new
:ref:`Python Initialization Configuration API <init-config>` to get the
:ref:`Python Path Configuration. <init-path-config>`.
(Contributed by Victor Stinner in :issue:`42260`.)

Deprecated
----------

Expand Down
4 changes: 3 additions & 1 deletion Include/internal/pycore_initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ PyAPI_FUNC(void) _PyConfig_InitCompatConfig(PyConfig *config);
extern PyStatus _PyConfig_Copy(
PyConfig *config,
const PyConfig *config2);
extern PyStatus _PyConfig_InitPathConfig(PyConfig *config);
extern PyStatus _PyConfig_InitPathConfig(
PyConfig *config,
int compute_path_config);
extern PyStatus _PyConfig_Read(PyConfig *config, int compute_path_config);
extern PyStatus _PyConfig_Write(const PyConfig *config,
struct pyruntimestate *runtime);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:c:func:`Py_GetPath`, :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and
:c:func:`Py_GetProgramName` functions now return ``NULL`` if called before
:c:func:`Py_Initialize` (before Python is initialized). Use the new
:ref:`Python Initialization Configuration API <init-config>` to get the
:ref:`Python Path Configuration. <init-path-config>`. Patch by Victor
Stinner.
4 changes: 2 additions & 2 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -2069,8 +2069,8 @@ config_read(PyConfig *config, int compute_path_config)
}
}

if (compute_path_config && config->_install_importlib) {
status = _PyConfig_InitPathConfig(config);
if (config->_install_importlib) {
status = _PyConfig_InitPathConfig(config, compute_path_config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
Expand Down
Loading

0 comments on commit ace3f9a

Please sign in to comment.