| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,8 +72,12 @@ PyWideStringList | |||
| 72 | 72 | ||
| 73 | 73 | .. c:function:: PyStatus PyWideStringList_Insert(PyWideStringList *list, Py_ssize_t index, const wchar_t *item) | |
| 74 | 74 | ||
| 75 | - Insert *item* into *list* at *index*. If *index* is greater than *list* | ||
| 76 | - length, just append *item* to *list*. | ||
| 75 | + Insert *item* into *list* at *index*. | ||
| 76 | + | ||
| 77 | + If *index* is greater than or equal to *list* length, append *item* to | ||
| 78 | + *list*. | ||
| 79 | + | ||
| 80 | + *index* must be greater than or equal to 0. | ||
| 77 | 81 | ||
| 78 | 82 | Python must be preinitialized to call this function. | |
| 79 | 83 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,6 +37,9 @@ typedef struct { | |||
| 37 | 37 | ||
| 38 | 38 | PyAPI_FUNC(PyStatus) PyWideStringList_Append(PyWideStringList *list, | |
| 39 | 39 | const wchar_t *item); | |
| 40 | + PyAPI_FUNC(PyStatus) PyWideStringList_Insert(PyWideStringList *list, | ||
| 41 | + Py_ssize_t index, | ||
| 42 | + const wchar_t *item); | ||
| 40 | 43 | ||
| 41 | 44 | ||
| 42 | 45 | /* --- PyPreConfig ----------------------------------------------- */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -500,7 +500,7 @@ def _get_expected_config(self, env): | |||
| 500 | 500 | self.fail(f"fail to decode stdout: {stdout!r}") | |
| 501 | 501 | ||
| 502 | 502 | def get_expected_config(self, expected_preconfig, expected, env, api, | |
| 503 | - add_path=None): | ||
| 503 | + modify_path_cb=None): | ||
| 504 | 504 | cls = self.__class__ | |
| 505 | 505 | if cls.EXPECTED_CONFIG is None: | |
| 506 | 506 | cls.EXPECTED_CONFIG = self._get_expected_config(env) | |
@@ -556,8 +556,9 @@ def get_expected_config(self, expected_preconfig, expected, env, api, | |||
| 556 | 556 | prepend_path = expected['pythonpath_env'] | |
| 557 | 557 | if prepend_path is not None: | |
| 558 | 558 | 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']) | ||
| 561 | 562 | ||
| 562 | 563 | for key in self.COPY_PRE_CONFIG: | |
| 563 | 564 | if key not in expected_preconfig: | |
@@ -602,7 +603,7 @@ def check_global_config(self, configs): | |||
| 602 | 603 | self.assertEqual(configs['global_config'], expected) | |
| 603 | 604 | ||
| 604 | 605 | 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, | ||
| 606 | 607 | *, api): | |
| 607 | 608 | env = remove_python_envvars() | |
| 608 | 609 | ||
@@ -628,7 +629,7 @@ def check_all_configs(self, testname, expected_config=None, | |||
| 628 | 629 | ||
| 629 | 630 | self.get_expected_config(expected_preconfig, | |
| 630 | 631 | expected_config, env, | |
| 631 | - api, add_path) | ||
| 632 | + api, modify_path_cb) | ||
| 632 | 633 | ||
| 633 | 634 | out, err = self.run_embedded_interpreter(testname, env=env) | |
| 634 | 635 | if stderr is None and not expected_config['verbose']: | |
@@ -894,9 +895,12 @@ def test_init_read_set(self): | |||
| 894 | 895 | 'program_name': './init_read_set', | |
| 895 | 896 | 'executable': 'my_executable', | |
| 896 | 897 | } | |
| 898 | + def modify_path(path): | ||
| 899 | + path.insert(1, "test_path_insert1") | ||
| 900 | + path.append("test_path_append") | ||
| 897 | 901 | self.check_all_configs("test_init_read_set", config, | |
| 898 | 902 | api=API_PYTHON, | |
| 899 | - add_path="init_read_set_path") | ||
| 903 | + modify_path_cb=modify_path) | ||
| 900 | 904 | ||
| 901 | 905 | def test_init_run_main(self): | |
| 902 | 906 | code = ('import _testinternalcapi, json; ' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1350,8 +1350,14 @@ static int test_init_read_set(void) | |||
| 1350 | 1350 | goto fail; | |
| 1351 | 1351 | } | |
| 1352 | 1352 | ||
| 1353 | + status = PyWideStringList_Insert(&config.module_search_paths, | ||
| 1354 | + 1, L"test_path_insert1"); | ||
| 1355 | + if (PyStatus_Exception(status)) { | ||
| 1356 | + goto fail; | ||
| 1357 | + } | ||
| 1358 | + | ||
| 1353 | 1359 | status = PyWideStringList_Append(&config.module_search_paths, | |
| 1354 | - L"init_read_set_path"); | ||
| 1360 | + L"test_path_append"); | ||
| 1355 | 1361 | if (PyStatus_Exception(status)) { | |
| 1356 | 1362 | goto fail; | |
| 1357 | 1363 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -297,32 +297,53 @@ _PyWideStringList_Copy(PyWideStringList *list, const PyWideStringList *list2) | |||
| 297 | 297 | ||
| 298 | 298 | ||
| 299 | 299 | PyStatus | |
| 300 | - PyWideStringList_Append(PyWideStringList *list, const wchar_t *item) | ||
| 300 | + PyWideStringList_Insert(PyWideStringList *list, | ||
| 301 | + Py_ssize_t index, const wchar_t *item) | ||
| 301 | 302 | { | |
| 302 | - if (list->length == PY_SSIZE_T_MAX) { | ||
| 303 | + Py_ssize_t len = list->length; | ||
| 304 | + if (len == PY_SSIZE_T_MAX) { | ||
| 303 | 305 | /* length+1 would overflow */ | |
| 304 | 306 | return _PyStatus_NO_MEMORY(); | |
| 305 | 307 | } | |
| 308 | + if (index < 0) { | ||
| 309 | + return _PyStatus_ERR("PyWideStringList_Insert index must be >= 0"); | ||
| 310 | + } | ||
| 311 | + if (index > len) { | ||
| 312 | + index = len; | ||
| 313 | + } | ||
| 306 | 314 | ||
| 307 | 315 | wchar_t *item2 = _PyMem_RawWcsdup(item); | |
| 308 | 316 | if (item2 == NULL) { | |
| 309 | 317 | return _PyStatus_NO_MEMORY(); | |
| 310 | 318 | } | |
| 311 | 319 | ||
| 312 | - size_t size = (list->length + 1) * sizeof(list->items[0]); | ||
| 320 | + size_t size = (len + 1) * sizeof(list->items[0]); | ||
| 313 | 321 | wchar_t **items2 = (wchar_t **)PyMem_RawRealloc(list->items, size); | |
| 314 | 322 | if (items2 == NULL) { | |
| 315 | 323 | PyMem_RawFree(item2); | |
| 316 | 324 | return _PyStatus_NO_MEMORY(); | |
| 317 | 325 | } | |
| 318 | 326 | ||
| 319 | - items2[list->length] = item2; | ||
| 327 | + if (index < len) { | ||
| 328 | + memmove(&items2[index + 1], | ||
| 329 | + &items2[index], | ||
| 330 | + (len - index) * sizeof(items2[0])); | ||
| 331 | + } | ||
| 332 | + | ||
| 333 | + items2[index] = item2; | ||
| 320 | 334 | list->items = items2; | |
| 321 | 335 | list->length++; | |
| 322 | 336 | return _PyStatus_OK(); | |
| 323 | 337 | } | |
| 324 | 338 | ||
| 325 | 339 | ||
| 340 | + PyStatus | ||
| 341 | + PyWideStringList_Append(PyWideStringList *list, const wchar_t *item) | ||
| 342 | + { | ||
| 343 | + return PyWideStringList_Insert(list, list->length, item); | ||
| 344 | + } | ||
| 345 | + | ||
| 346 | + | ||
| 326 | 347 | PyStatus | |
| 327 | 348 | _PyWideStringList_Extend(PyWideStringList *list, const PyWideStringList *list2) | |
| 328 | 349 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments