/*********************************************************
* Embedded interpreter tests that need a custom exe
*
- * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
+ * Executed via Lib/test/test_embed.py
*********************************************************/
// Use to display the usage
}
-static void _testembed_Py_Initialize(void)
+static void _testembed_Py_InitializeFromConfig(void)
{
PyConfig config;
_PyConfig_InitCompatConfig(&config);
init_from_config_clear(&config);
}
+static void _testembed_Py_Initialize(void)
+{
+ Py_SetProgramName(PROGRAM_NAME);
+ Py_Initialize();
+}
+
/*****************************************************
* Test repeated initialisation and subinterpreters
for (int i=1; i <= INIT_LOOPS; i++) {
printf("--- Pass %d ---\n", i);
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
mainstate = PyThreadState_Get();
PyEval_ReleaseThread(mainstate);
fprintf(stderr, "--- Loop #%d ---\n", i);
fflush(stderr);
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
int err = PyRun_SimpleString(code);
Py_Finalize();
if (err) {
return 0;
}
+/****************************************************************************
+ * Test the Py_Initialize(Ex) convenience/compatibility wrappers
+ ***************************************************************************/
+// This is here to help ensure there are no wrapper resource leaks (gh-96853)
+static int test_repeated_simple_init(void)
+{
+ for (int i=1; i <= INIT_LOOPS; i++) {
+ fprintf(stderr, "--- Loop #%d ---\n", i);
+ fflush(stderr);
+
+ _testembed_Py_Initialize();
+ Py_Finalize();
+ printf("Finalized\n"); // Give test_embed some output to check
+ }
+ return 0;
+}
+
/*****************************************************
* Test forcing a particular IO encoding
fflush(stdout);
/* Force the given IO encoding */
Py_SetStandardStreamEncoding(encoding, errors);
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
PyRun_SimpleString(
"import sys;"
"print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
dynamic_xoption = NULL;
_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
_Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
PyRun_SimpleString("import sys; "
"print('sys.warnoptions:', sys.warnoptions); "
return 1;
}
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
if (thrd == PYTHREAD_INVALID_THREAD_ID) {
static int test_initialize_twice(void)
{
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
/* bpo-33932: Calling Py_Initialize() twice should do nothing
* (and not crash!). */
L"print(f'Py_Main() after Py_Initialize: "
L"sys.argv={sys.argv}')"),
L"arg2"};
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
/* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
Py_Main(Py_ARRAY_LENGTH(argv), argv);
static int test_init_initialize_config(void)
{
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
dump_config();
Py_Finalize();
return 0;
/* Test initialization from environment variables */
Py_IgnoreEnvironmentFlag = 0;
set_all_env_vars();
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
dump_config();
Py_Finalize();
return 0;
/* Test initialization from environment variables */
Py_IgnoreEnvironmentFlag = 0;
set_all_env_vars_dev_mode();
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
dump_config();
Py_Finalize();
return 0;
Py_IgnoreEnvironmentFlag = 0;
set_all_env_vars_dev_mode();
putenv("PYTHONMALLOC=malloc");
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
dump_config();
Py_Finalize();
return 0;
}
Py_IgnoreEnvironmentFlag = 0;
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
result = 0;
PyObject *r = PyFile_OpenCode("$$test-filename");
Py_IgnoreEnvironmentFlag = 0;
PySys_AddAuditHook(_audit_hook, &sawSet);
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
if (PySys_Audit("_testembed.raise", NULL) == 0) {
printf("No error raised");
{
Py_IgnoreEnvironmentFlag = 0;
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
Py_NewInterpreter();
Py_NewInterpreter();
_Py_IDENTIFIER(test_unicode_id_init);
// Initialize Python once without using the identifier
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
Py_Finalize();
// Now initialize Python multiple times and use the identifier.
// The first _PyUnicode_FromId() call initializes the identifier index.
for (int i=0; i<3; i++) {
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
PyObject *str1, *str2;
static int
test_get_incomplete_frame(void)
{
- _testembed_Py_Initialize();
+ _testembed_Py_InitializeFromConfig();
PyMemAllocatorEx allocator;
wrap_allocator(&allocator);
// Force an allocation with an incomplete (generator) frame:
static struct TestCase TestCases[] = {
// Python initialization
{"test_repeated_init_exec", test_repeated_init_exec},
+ {"test_repeated_simple_init", test_repeated_simple_init},
{"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},