bpo-40910: Export Py_GetArgcArgv() function (GH-20721) (GH-20723)

Export explicitly the Py_GetArgcArgv() function to the C API and
document the function. Previously, it was exported implicitly which
no longer works since Python is built with -fvisibility=hidden.

* Add PyConfig._orig_argv member.
* Py_InitializeFromConfig() no longer calls _PyConfig_Write() twice.
* PyConfig_Read() no longer initializes Py_GetArgcArgv(): it is now
  _PyConfig_Write() responsibility.
* _PyConfig_Write() result type becomes PyStatus instead of void.
* Write an unit test on Py_GetArgcArgv().

(cherry picked from commit e81f6e687d)
This commit is contained in:
Victor Stinner 2020-06-08 18:44:50 +02:00 committed by GitHub
parent 1220a47079
commit dedaac040f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 131 additions and 22 deletions

View file

@ -1326,6 +1326,7 @@ static int test_init_read_set(void)
return 0;
fail:
PyConfig_Clear(&config);
Py_ExitStatusException(status);
}
@ -1584,6 +1585,46 @@ static int test_run_main(void)
}
static int test_get_argc_argv(void)
{
PyConfig config;
PyConfig_InitPythonConfig(&config);
wchar_t *argv[] = {L"python3", L"-c",
(L"import sys; "
L"print(f'Py_RunMain(): sys.argv={sys.argv}')"),
L"arg2"};
config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
config_set_string(&config, &config.program_name, L"./python3");
// Calling PyConfig_Read() twice must not change Py_GetArgcArgv() result.
// The second call is done by Py_InitializeFromConfig().
PyStatus status = PyConfig_Read(&config);
if (PyStatus_Exception(status)) {
PyConfig_Clear(&config);
Py_ExitStatusException(status);
}
init_from_config_clear(&config);
int get_argc;
wchar_t **get_argv;
Py_GetArgcArgv(&get_argc, &get_argv);
printf("argc: %i\n", get_argc);
assert(get_argc == Py_ARRAY_LENGTH(argv));
for (int i=0; i < get_argc; i++) {
printf("argv[%i]: %ls\n", i, get_argv[i]);
assert(wcscmp(get_argv[i], argv[i]) == 0);
}
Py_Finalize();
printf("\n");
printf("test ok\n");
return 0;
}
/* *********************************************************
* List of test cases and the function that implements it.
*
@ -1641,6 +1682,7 @@ static struct TestCase TestCases[] = {
{"test_init_setpythonhome", test_init_setpythonhome},
{"test_init_warnoptions", test_init_warnoptions},
{"test_run_main", test_run_main},
{"test_get_argc_argv", test_get_argc_argv},
{"test_open_code_hook", test_open_code_hook},
{"test_audit", test_audit},