summaryrefslogtreecommitdiffstats
path: root/Programs/_testembed.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2021-06-23 15:47:33 (GMT)
committerGitHub <noreply@github.com>2021-06-23 15:47:33 (GMT)
commit5ed7827b1620f5b3729bc9767158d24a33863fa9 (patch)
treeb319c77a89064b6f2fab1345a6c3fb1c3f3bf072 /Programs/_testembed.c
parentfcde2c6a8c99a56576b25733d5cc60bce6d51f46 (diff)
downloadcpython-5ed7827b1620f5b3729bc9767158d24a33863fa9.zip
cpython-5ed7827b1620f5b3729bc9767158d24a33863fa9.tar.gz
cpython-5ed7827b1620f5b3729bc9767158d24a33863fa9.tar.bz2
bpo-44441: _PyImport_Fini2() resets PyImport_Inittab (GH-26874) (GH-26878)
Py_RunMain() now resets PyImport_Inittab to its initial value at exit. It must be possible to call PyImport_AppendInittab() or PyImport_ExtendInittab() at each Python initialization. (cherry picked from commit 489699ca05bed5cfd10e847d8580840812b476cd)
Diffstat (limited to 'Programs/_testembed.c')
-rw-r--r--Programs/_testembed.c58
1 files changed, 55 insertions, 3 deletions
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index 3bf0c37..3933b86 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -22,6 +22,9 @@
/* Use path starting with "./" avoids a search along the PATH */
#define PROGRAM_NAME L"./_testembed"
+#define INIT_LOOPS 16
+
+
static void _testembed_Py_Initialize(void)
{
Py_SetProgramName(PROGRAM_NAME);
@@ -54,9 +57,8 @@ static int test_repeated_init_and_subinterpreters(void)
{
PyThreadState *mainstate, *substate;
PyGILState_STATE gilstate;
- int i, j;
- for (i=0; i<15; i++) {
+ for (int i=1; i <= INIT_LOOPS; i++) {
printf("--- Pass %d ---\n", i);
_testembed_Py_Initialize();
mainstate = PyThreadState_Get();
@@ -67,7 +69,7 @@ static int test_repeated_init_and_subinterpreters(void)
print_subinterp();
PyThreadState_Swap(NULL);
- for (j=0; j<3; j++) {
+ for (int j=0; j<3; j++) {
substate = Py_NewInterpreter();
print_subinterp();
Py_EndInterpreter(substate);
@@ -83,6 +85,20 @@ static int test_repeated_init_and_subinterpreters(void)
return 0;
}
+#define EMBEDDED_EXT_NAME "embedded_ext"
+
+static PyModuleDef embedded_ext = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = EMBEDDED_EXT_NAME,
+ .m_size = 0,
+};
+
+static PyObject*
+PyInit_embedded_ext(void)
+{
+ return PyModule_Create(&embedded_ext);
+}
+
/*****************************************************
* Test forcing a particular IO encoding
*****************************************************/
@@ -1641,6 +1657,39 @@ static int test_get_argc_argv(void)
}
+static int test_repeated_init_and_inittab(void)
+{
+ // bpo-44441: Py_RunMain() must reset PyImport_Inittab at exit.
+ // It must be possible to call PyImport_AppendInittab() or
+ // PyImport_ExtendInittab() before each Python initialization.
+ for (int i=1; i <= INIT_LOOPS; i++) {
+ printf("--- Pass %d ---\n", i);
+
+ // Call PyImport_AppendInittab() at each iteration
+ if (PyImport_AppendInittab(EMBEDDED_EXT_NAME,
+ &PyInit_embedded_ext) != 0) {
+ fprintf(stderr, "PyImport_AppendInittab() failed\n");
+ return 1;
+ }
+
+ // Initialize Python
+ wchar_t* argv[] = {PROGRAM_NAME, L"-c", L"pass"};
+ PyConfig config;
+ PyConfig_InitPythonConfig(&config);
+ config.isolated = 1;
+ config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
+ init_from_config_clear(&config);
+
+ // Py_RunMain() calls _PyImport_Fini2() which resets PyImport_Inittab
+ int exitcode = Py_RunMain();
+ if (exitcode != 0) {
+ return exitcode;
+ }
+ }
+ return 0;
+}
+
+
/* *********************************************************
* List of test cases and the function that implements it.
*
@@ -1660,8 +1709,10 @@ struct TestCase
};
static struct TestCase TestCases[] = {
+ // Python initialization
{"test_forced_io_encoding", test_forced_io_encoding},
{"test_repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters},
+ {"test_repeated_init_and_inittab", test_repeated_init_and_inittab},
{"test_pre_initialization_api", test_pre_initialization_api},
{"test_pre_initialization_sys_options", test_pre_initialization_sys_options},
{"test_bpo20891", test_bpo20891},
@@ -1700,6 +1751,7 @@ static struct TestCase TestCases[] = {
{"test_run_main", test_run_main},
{"test_get_argc_argv", test_get_argc_argv},
+ // Audit
{"test_open_code_hook", test_open_code_hook},
{"test_audit", test_audit},
{"test_audit_subinterpreter", test_audit_subinterpreter},