bpo-36763: Implement PyWideStringList_Insert() of PEP 587 (GH-15423) · python/cpython@a6427cb · GitHub
Skip to content

Commit a6427cb

Browse files
bpo-36763: Implement PyWideStringList_Insert() of PEP 587 (GH-15423)
(cherry picked from commit 3842f29) Co-authored-by: Victor Stinner <vstinner@redhat.com>
1 parent fe64ba6 commit a6427cb

5 files changed

Lines changed: 51 additions & 13 deletions

File tree

Doc/c-api/init_config.rst

Lines changed: 6 additions & 2 deletions

Include/cpython/initconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ typedef struct {
3737

3838
PyAPI_FUNC(PyStatus) PyWideStringList_Append(PyWideStringList *list,
3939
const wchar_t *item);
40+
PyAPI_FUNC(PyStatus) PyWideStringList_Insert(PyWideStringList *list,
41+
Py_ssize_t index,
42+
const wchar_t *item);
4043

4144

4245
/* --- PyPreConfig ----------------------------------------------- */

Lib/test/test_embed.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def _get_expected_config(self, env):
500500
self.fail(f"fail to decode stdout: {stdout!r}")
501501

502502
def get_expected_config(self, expected_preconfig, expected, env, api,
503-
add_path=None):
503+
modify_path_cb=None):
504504
cls = self.__class__
505505
if cls.EXPECTED_CONFIG is None:
506506
cls.EXPECTED_CONFIG = self._get_expected_config(env)
@@ -556,8 +556,9 @@ def get_expected_config(self, expected_preconfig, expected, env, api,
556556
prepend_path = expected['pythonpath_env']
557557
if prepend_path is not None:
558558
expected['module_search_paths'] = [prepend_path, *expected['module_search_paths']]
559-
if add_path is not None:
560-
expected['module_search_paths'] = [*expected['module_search_paths'], add_path]
559+
if modify_path_cb is not None:
560+
expected['module_search_paths'] = expected['module_search_paths'].copy()
561+
modify_path_cb(expected['module_search_paths'])
561562

562563
for key in self.COPY_PRE_CONFIG:
563564
if key not in expected_preconfig:
@@ -602,7 +603,7 @@ def check_global_config(self, configs):
602603
self.assertEqual(configs['global_config'], expected)
603604

604605
def check_all_configs(self, testname, expected_config=None,
605-
expected_preconfig=None, add_path=None, stderr=None,
606+
expected_preconfig=None, modify_path_cb=None, stderr=None,
606607
*, api):
607608
env = remove_python_envvars()
608609

@@ -628,7 +629,7 @@ def check_all_configs(self, testname, expected_config=None,
628629

629630
self.get_expected_config(expected_preconfig,
630631
expected_config, env,
631-
api, add_path)
632+
api, modify_path_cb)
632633

633634
out, err = self.run_embedded_interpreter(testname, env=env)
634635
if stderr is None and not expected_config['verbose']:
@@ -893,9 +894,12 @@ def test_init_read_set(self):
893894
'program_name': './init_read_set',
894895
'executable': 'my_executable',
895896
}
897+
def modify_path(path):
898+
path.insert(1, "test_path_insert1")
899+
path.append("test_path_append")
896900
self.check_all_configs("test_init_read_set", config,
897901
api=API_PYTHON,
898-
add_path="init_read_set_path")
902+
modify_path_cb=modify_path)
899903

900904
def test_init_run_main(self):
901905
code = ('import _testinternalcapi, json; '

Programs/_testembed.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,8 +1350,14 @@ static int test_init_read_set(void)
13501350
goto fail;
13511351
}
13521352

1353+
status = PyWideStringList_Insert(&config.module_search_paths,
1354+
1, L"test_path_insert1");
1355+
if (PyStatus_Exception(status)) {
1356+
goto fail;
1357+
}
1358+
13531359
status = PyWideStringList_Append(&config.module_search_paths,
1354-
L"init_read_set_path");
1360+
L"test_path_append");
13551361
if (PyStatus_Exception(status)) {
13561362
goto fail;
13571363
}

Python/initconfig.c

Lines changed: 25 additions & 4 deletions

0 commit comments

Comments
 (0)