diff options
author | Victor Stinner <vstinner@python.org> | 2020-06-08 18:44:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-08 18:44:50 +0200 |
commit | dedaac040fe5b0b88acdec072623bb39ec53687f (patch) | |
tree | a83f2e7bcb9653e17fe8ae3ca23e04ab4a6ff5d9 /Programs | |
parent | bpo-33187: Document 3.9 changes to xml.etree.ElementInclude.include (GH-20438) (diff) | |
download | cpython-dedaac040fe5b0b88acdec072623bb39ec53687f.tar.gz cpython-dedaac040fe5b0b88acdec072623bb39ec53687f.tar.bz2 cpython-dedaac040fe5b0b88acdec072623bb39ec53687f.zip |
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 e81f6e687d0f04a45f2389d0b43fafd6d8491624)
Diffstat (limited to 'Programs')
-rw-r--r-- | Programs/_testembed.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Programs/_testembed.c b/Programs/_testembed.c index 5c83678f650..864a6e7139a 100644 --- a/Programs/_testembed.c +++ b/Programs/_testembed.c @@ -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}, |