Skip to content

Commit ace3f9a

Browse files
authored
bpo-42260: Fix _PyConfig_Read() if compute_path_config=0 (GH-23220)
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.
1 parent 1e996c3 commit ace3f9a

File tree

8 files changed

+93
-95
lines changed

8 files changed

+93
-95
lines changed

Doc/c-api/init.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ Process-wide parameters
376376
The returned string points into static storage; the caller should not modify its
377377
value.
378378

379+
This function should not be called before :c:func:`Py_Initialize`, otherwise
380+
it returns ``NULL``.
381+
382+
.. versionchanged:: 3.10
383+
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
384+
379385

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

398+
This function should not be called before :c:func:`Py_Initialize`, otherwise
399+
it returns ``NULL``.
400+
401+
.. versionchanged:: 3.10
402+
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
403+
392404

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

439+
This function should not be called before :c:func:`Py_Initialize`, otherwise
440+
it returns ``NULL``.
441+
442+
.. versionchanged:: 3.10
443+
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
444+
427445

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

458+
This function should not be called before :c:func:`Py_Initialize`, otherwise
459+
it returns ``NULL``.
460+
461+
.. versionchanged:: 3.10
462+
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
463+
440464

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

482+
This function should not be called before :c:func:`Py_Initialize`, otherwise
483+
it returns ``NULL``.
484+
458485
.. XXX should give the exact rules
459486
487+
.. versionchanged:: 3.10
488+
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
489+
460490

461491
.. c:function:: void Py_SetPath(const wchar_t *)
462492
@@ -638,6 +668,12 @@ Process-wide parameters
638668
:c:func:`Py_SetPythonHome`, or the value of the :envvar:`PYTHONHOME`
639669
environment variable if it is set.
640670
671+
This function should not be called before :c:func:`Py_Initialize`, otherwise
672+
it returns ``NULL``.
673+
674+
.. versionchanged:: 3.10
675+
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
676+
641677
642678
.. _threads:
643679

Doc/c-api/init_config.rst

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ There are two kinds of configuration:
2424
environments variables are ignored, the LC_CTYPE locale is left unchanged and
2525
no signal handler is registred.
2626

27+
The :c:func:`Py_RunMain` function can be used to write a customized Python
28+
program.
29+
2730
See also :ref:`Initialization, Finalization, and Threads <initialization>`.
2831

2932
.. seealso::
3033
:pep:`587` "Python Initialization Configuration".
3134

35+
3236
Example
3337
=======
3438

@@ -532,7 +536,7 @@ PyConfig
532536
533537
Default: ``NULL``.
534538
535-
Part of the :ref:`Path Configuration <init-path-config>` output.
539+
Part of the :ref:`Python Path Configuration <init-path-config>` output.
536540
537541
.. c:member:: wchar_t* base_executable
538542
@@ -544,15 +548,15 @@ PyConfig
544548
545549
Default: ``NULL``.
546550
547-
Part of the :ref:`Path Configuration <init-path-config>` output.
551+
Part of the :ref:`Python Path Configuration <init-path-config>` output.
548552
549553
.. c:member:: wchar_t* base_prefix
550554
551555
:data:`sys.base_prefix`.
552556
553557
Default: ``NULL``.
554558
555-
Part of the :ref:`Path Configuration <init-path-config>` output.
559+
Part of the :ref:`Python Path Configuration <init-path-config>` output.
556560
557561
.. c:member:: int buffered_stdio
558562
@@ -634,7 +638,7 @@ PyConfig
634638
635639
Default: ``NULL``.
636640
637-
Part of the :ref:`Path Configuration <init-path-config>` output.
641+
Part of the :ref:`Python Path Configuration <init-path-config>` output.
638642
639643
.. c:member:: wchar_t* executable
640644
@@ -643,7 +647,7 @@ PyConfig
643647
644648
Default: ``NULL``.
645649
646-
Part of the :ref:`Path Configuration <init-path-config>` output.
650+
Part of the :ref:`Python Path Configuration <init-path-config>` output.
647651
648652
.. c:member:: int faulthandler
649653
@@ -726,7 +730,7 @@ PyConfig
726730
727731
Default: ``NULL``.
728732
729-
Part of the :ref:`Path Configuration <init-path-config>` input.
733+
Part of the :ref:`Python Path Configuration <init-path-config>` input.
730734
731735
.. c:member:: int import_time
732736
@@ -817,7 +821,7 @@ PyConfig
817821
Default: value of the ``PLATLIBDIR`` macro which is set at configure time
818822
by ``--with-platlibdir`` (default: ``"lib"``).
819823
820-
Part of the :ref:`Path Configuration <init-path-config>` input.
824+
Part of the :ref:`Python Path Configuration <init-path-config>` input.
821825
822826
.. versionadded:: 3.9
823827
@@ -830,22 +834,22 @@ PyConfig
830834
831835
Default: ``NULL``.
832836
833-
Part of the :ref:`Path Configuration <init-path-config>` input.
837+
Part of the :ref:`Python Path Configuration <init-path-config>` input.
834838
835839
.. c:member:: PyWideStringList module_search_paths
836840
.. c:member:: int module_search_paths_set
837841
838842
Module search paths: :data:`sys.path`.
839843
840844
If :c:member:`~PyConfig.module_search_paths_set` is equal to 0, the
841-
function calculating the :ref:`Path Configuration <init-path-config>`
845+
function calculating the :ref:`Python Path Configuration <init-path-config>`
842846
overrides the :c:member:`~PyConfig.module_search_paths` and sets
843847
:c:member:`~PyConfig.module_search_paths_set` to ``1``.
844848
845849
Default: empty list (``module_search_paths``) and ``0``
846850
(``module_search_paths_set``).
847851
848-
Part of the :ref:`Path Configuration <init-path-config>` output.
852+
Part of the :ref:`Python Path Configuration <init-path-config>` output.
849853
850854
.. c:member:: int optimization_level
851855
@@ -911,15 +915,15 @@ PyConfig
911915
912916
.. c:member:: int pathconfig_warnings
913917
914-
On Unix, if non-zero, calculating the :ref:`Path Configuration
918+
On Unix, if non-zero, calculating the :ref:`Python Path Configuration
915919
<init-path-config>` can log warnings into ``stderr``. If equals to 0,
916920
suppress these warnings.
917921
918922
It has no effect on Windows.
919923
920924
Default: ``1`` in Python mode, ``0`` in isolated mode.
921925
922-
Part of the :ref:`Path Configuration <init-path-config>` input.
926+
Part of the :ref:`Python Path Configuration <init-path-config>` input.
923927
924928
.. c:member:: wchar_t* prefix
925929
@@ -928,7 +932,7 @@ PyConfig
928932
929933
Default: ``NULL``.
930934
931-
Part of the :ref:`Path Configuration <init-path-config>` output.
935+
Part of the :ref:`Python Path Configuration <init-path-config>` output.
932936
933937
.. c:member:: wchar_t* program_name
934938
@@ -946,7 +950,7 @@ PyConfig
946950
947951
Default: ``NULL``.
948952
949-
Part of the :ref:`Path Configuration <init-path-config>` input.
953+
Part of the :ref:`Python Path Configuration <init-path-config>` input.
950954
951955
.. c:member:: wchar_t* pycache_prefix
952956
@@ -1262,7 +1266,7 @@ and user site directory. The C standard streams (ex: ``stdout``) and the
12621266
LC_CTYPE locale are left unchanged. Signal handlers are not installed.
12631267
12641268
Configuration files are still used with this configuration. Set the
1265-
:ref:`Path Configuration <init-path-config>` ("output fields") to ignore these
1269+
:ref:`Python Path Configuration <init-path-config>` ("output fields") to ignore these
12661270
configuration files and avoid the function computing the default path
12671271
configuration.
12681272
@@ -1287,8 +1291,8 @@ and :ref:`Python UTF-8 Mode <utf8-mode>`
12871291
12881292
.. _init-path-config:
12891293
1290-
Path Configuration
1291-
==================
1294+
Python Path Configuration
1295+
=========================
12921296
12931297
:c:type:`PyConfig` contains multiple fields for the path configuration:
12941298

Doc/whatsnew/3.10.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,14 @@ Porting to Python 3.10
493493
``unicodedata.ucnhash_CAPI`` has been moved to the internal C API.
494494
(Contributed by Victor Stinner in :issue:`42157`.)
495495

496+
* :c:func:`Py_GetPath`, :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
497+
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and
498+
:c:func:`Py_GetProgramName` functions now return ``NULL`` if called before
499+
:c:func:`Py_Initialize` (before Python is initialized). Use the new
500+
:ref:`Python Initialization Configuration API <init-config>` to get the
501+
:ref:`Python Path Configuration. <init-path-config>`.
502+
(Contributed by Victor Stinner in :issue:`42260`.)
503+
496504
Deprecated
497505
----------
498506

Include/internal/pycore_initconfig.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ PyAPI_FUNC(void) _PyConfig_InitCompatConfig(PyConfig *config);
151151
extern PyStatus _PyConfig_Copy(
152152
PyConfig *config,
153153
const PyConfig *config2);
154-
extern PyStatus _PyConfig_InitPathConfig(PyConfig *config);
154+
extern PyStatus _PyConfig_InitPathConfig(
155+
PyConfig *config,
156+
int compute_path_config);
155157
extern PyStatus _PyConfig_Read(PyConfig *config, int compute_path_config);
156158
extern PyStatus _PyConfig_Write(const PyConfig *config,
157159
struct pyruntimestate *runtime);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:c:func:`Py_GetPath`, :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
2+
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and
3+
:c:func:`Py_GetProgramName` functions now return ``NULL`` if called before
4+
:c:func:`Py_Initialize` (before Python is initialized). Use the new
5+
:ref:`Python Initialization Configuration API <init-config>` to get the
6+
:ref:`Python Path Configuration. <init-path-config>`. Patch by Victor
7+
Stinner.

Python/initconfig.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,8 +2069,8 @@ config_read(PyConfig *config, int compute_path_config)
20692069
}
20702070
}
20712071

2072-
if (compute_path_config && config->_install_importlib) {
2073-
status = _PyConfig_InitPathConfig(config);
2072+
if (config->_install_importlib) {
2073+
status = _PyConfig_InitPathConfig(config, compute_path_config);
20742074
if (_PyStatus_EXCEPTION(status)) {
20752075
return status;
20762076
}

0 commit comments

Comments
 (0)