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)
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index 5c83678..864a6e7 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -1326,6 +1326,7 @@
return 0;
fail:
+ PyConfig_Clear(&config);
Py_ExitStatusException(status);
}
@@ -1584,6 +1585,46 @@
}
+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 @@
{"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},