Skip to content

Commit 4eb2f40

Browse files
gh-92913: Clarify changes to PyInitConfig.module_search_paths[_set] fields (GH-92980)
(cherry picked from commit 403d16f) Co-authored-by: Steve Dower <[email protected]>
1 parent 57d7ddd commit 4eb2f40

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

Doc/c-api/init_config.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,11 @@ Example setting the program name::
12891289
}
12901290
12911291
More complete example modifying the default configuration, read the
1292-
configuration, and then override some parameters::
1292+
configuration, and then override some parameters. Note that since
1293+
3.11, many parameters are not calculated until initialization, and
1294+
so values cannot be read from the configuration structure. Any values
1295+
set before initialize is called will be left unchanged by
1296+
initialization::
12931297
12941298
PyStatus init_python(const char *program_name)
12951299
{
@@ -1314,7 +1318,15 @@ configuration, and then override some parameters::
13141318
goto done;
13151319
}
13161320
1317-
/* Append our custom search path to sys.path */
1321+
/* Specify sys.path explicitly */
1322+
/* To calculate the default and then modify, finish initialization and
1323+
then use PySys_GetObject("path") to get the list. */
1324+
condig.module_search_paths_set = 1
1325+
status = PyWideStringList_Append(&config.module_search_paths,
1326+
L"/path/to/stdlib");
1327+
if (PyStatus_Exception(status)) {
1328+
goto done;
1329+
}
13181330
status = PyWideStringList_Append(&config.module_search_paths,
13191331
L"/path/to/more/modules");
13201332
if (PyStatus_Exception(status)) {
@@ -1417,8 +1429,8 @@ It is possible to completely ignore the function calculating the default
14171429
path configuration by setting explicitly all path configuration output
14181430
fields listed above. A string is considered as set even if it is non-empty.
14191431
``module_search_paths`` is considered as set if
1420-
``module_search_paths_set`` is set to ``1``. In this case, path
1421-
configuration input fields are ignored as well.
1432+
``module_search_paths_set`` is set to ``1``. In this case,
1433+
``module_search_paths`` will be used without modification.
14221434
14231435
Set :c:member:`~PyConfig.pathconfig_warnings` to ``0`` to suppress warnings when
14241436
calculating the path configuration (Unix only, Windows does not log any warning).

Doc/whatsnew/3.11.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ Other CPython Implementation Changes
403403
instead of prepending them.
404404
(Contributed by Bastian Neuburger in :issue:`44934`.)
405405

406+
* The :c:member:`PyConfig.module_search_paths_set` field must now be set to 1 for
407+
initialization to use :c:member:`PyConfig.module_search_paths` to initialize
408+
:data:`sys.path`. Otherwise, initialization will recalculate the path and replace
409+
any values added to ``module_search_paths``.
410+
406411

407412
New Modules
408413
===========
@@ -1861,6 +1866,16 @@ Porting to Python 3.11
18611866
* Distributors are encouraged to build Python with the optimized Blake2
18621867
library `libb2`_.
18631868

1869+
* The :c:member:`PyConfig.module_search_paths_set` field must now be set to 1 for
1870+
initialization to use :c:member:`PyConfig.module_search_paths` to initialize
1871+
:data:`sys.path`. Otherwise, initialization will recalculate the path and replace
1872+
any values added to ``module_search_paths``.
1873+
1874+
* :c:func:`PyConfig_Read` no longer calculates the initial search path, and will not
1875+
fill any values into :c:member:`PyConfig.module_search_paths`. To calculate default
1876+
paths and then modify them, finish initialization and use :c:func:`PySys_GetObject`
1877+
to retrieve :data:`sys.path` as a Python list object and modify it directly.
1878+
18641879

18651880
Deprecated
18661881
----------

Lib/test/_test_embed_set_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ def test_path(self):
236236
module_search_paths=['a', 'b', 'c'])
237237
self.assertEqual(sys.path, ['a', 'b', 'c'])
238238

239-
# Leave sys.path unchanged if module_search_paths_set=0
239+
# sys.path is reset if module_search_paths_set=0
240240
self.set_config(module_search_paths_set=0,
241241
module_search_paths=['new_path'])
242-
self.assertEqual(sys.path, ['a', 'b', 'c'])
242+
self.assertNotEqual(sys.path, ['a', 'b', 'c'])
243+
self.assertNotEqual(sys.path, ['new_path'])
243244

244245
def test_argv(self):
245246
self.set_config(parse_argv=0,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ensures changes to :c:member:`PyConfig.module_search_paths` are ignored
2+
unless :c:member:`PyConfig.module_search_paths_set` is set

Modules/getpath.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def search_up(prefix, *landmarks, test=isfile):
228228
use_environment = config.get('use_environment', 1)
229229

230230
pythonpath = config.get('module_search_paths')
231+
pythonpath_was_set = config.get('module_search_paths_set')
231232

232233
real_executable_dir = None
233234
stdlib_dir = None
@@ -626,8 +627,8 @@ def search_up(prefix, *landmarks, test=isfile):
626627
config['module_search_paths'] = py_setpath.split(DELIM)
627628
config['module_search_paths_set'] = 1
628629

629-
elif not pythonpath:
630-
# If pythonpath was already set, we leave it alone.
630+
elif not pythonpath_was_set:
631+
# If pythonpath was already explicitly set or calculated, we leave it alone.
631632
# This won't matter in normal use, but if an embedded host is trying to
632633
# recalculate paths while running then we do not want to change it.
633634
pythonpath = []

0 commit comments

Comments
 (0)