@@ -22,7 +22,7 @@ char **main_argv;
22
22
/*********************************************************
23
23
* Embedded interpreter tests that need a custom exe
24
24
*
25
- * Executed via 'EmbeddingTests' in Lib/test/test_capi .py
25
+ * Executed via Lib/test/test_embed .py
26
26
*********************************************************/
27
27
28
28
// Use to display the usage
@@ -73,14 +73,20 @@ static void init_from_config_clear(PyConfig *config)
73
73
}
74
74
75
75
76
- static void _testembed_Py_Initialize (void )
76
+ static void _testembed_Py_InitializeFromConfig (void )
77
77
{
78
78
PyConfig config ;
79
79
_PyConfig_InitCompatConfig (& config );
80
80
config_set_program_name (& config );
81
81
init_from_config_clear (& config );
82
82
}
83
83
84
+ static void _testembed_Py_Initialize (void )
85
+ {
86
+ Py_SetProgramName (PROGRAM_NAME );
87
+ Py_Initialize ();
88
+ }
89
+
84
90
85
91
/*****************************************************
86
92
* Test repeated initialisation and subinterpreters
@@ -110,7 +116,7 @@ static int test_repeated_init_and_subinterpreters(void)
110
116
111
117
for (int i = 1 ; i <= INIT_LOOPS ; i ++ ) {
112
118
printf ("--- Pass %d ---\n" , i );
113
- _testembed_Py_Initialize ();
119
+ _testembed_Py_InitializeFromConfig ();
114
120
mainstate = PyThreadState_Get ();
115
121
116
122
PyEval_ReleaseThread (mainstate );
@@ -168,7 +174,7 @@ static int test_repeated_init_exec(void)
168
174
fprintf (stderr , "--- Loop #%d ---\n" , i );
169
175
fflush (stderr );
170
176
171
- _testembed_Py_Initialize ();
177
+ _testembed_Py_InitializeFromConfig ();
172
178
int err = PyRun_SimpleString (code );
173
179
Py_Finalize ();
174
180
if (err ) {
@@ -178,6 +184,23 @@ static int test_repeated_init_exec(void)
178
184
return 0 ;
179
185
}
180
186
187
+ /****************************************************************************
188
+ * Test the Py_Initialize(Ex) convenience/compatibility wrappers
189
+ ***************************************************************************/
190
+ // This is here to help ensure there are no wrapper resource leaks (gh-96853)
191
+ static int test_repeated_simple_init (void )
192
+ {
193
+ for (int i = 1 ; i <= INIT_LOOPS ; i ++ ) {
194
+ fprintf (stderr , "--- Loop #%d ---\n" , i );
195
+ fflush (stderr );
196
+
197
+ _testembed_Py_Initialize ();
198
+ Py_Finalize ();
199
+ printf ("Finalized\n" ); // Give test_embed some output to check
200
+ }
201
+ return 0 ;
202
+ }
203
+
181
204
182
205
/*****************************************************
183
206
* Test forcing a particular IO encoding
@@ -199,7 +222,7 @@ static void check_stdio_details(const char *encoding, const char * errors)
199
222
fflush (stdout );
200
223
/* Force the given IO encoding */
201
224
Py_SetStandardStreamEncoding (encoding , errors );
202
- _testembed_Py_Initialize ();
225
+ _testembed_Py_InitializeFromConfig ();
203
226
PyRun_SimpleString (
204
227
"import sys;"
205
228
"print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
@@ -308,7 +331,7 @@ static int test_pre_initialization_sys_options(void)
308
331
dynamic_xoption = NULL ;
309
332
310
333
_Py_EMBED_PREINIT_CHECK ("Initializing interpreter\n" );
311
- _testembed_Py_Initialize ();
334
+ _testembed_Py_InitializeFromConfig ();
312
335
_Py_EMBED_PREINIT_CHECK ("Check sys module contents\n" );
313
336
PyRun_SimpleString ("import sys; "
314
337
"print('sys.warnoptions:', sys.warnoptions); "
@@ -352,7 +375,7 @@ static int test_bpo20891(void)
352
375
return 1 ;
353
376
}
354
377
355
- _testembed_Py_Initialize ();
378
+ _testembed_Py_InitializeFromConfig ();
356
379
357
380
unsigned long thrd = PyThread_start_new_thread (bpo20891_thread , & lock );
358
381
if (thrd == PYTHREAD_INVALID_THREAD_ID ) {
@@ -375,7 +398,7 @@ static int test_bpo20891(void)
375
398
376
399
static int test_initialize_twice (void )
377
400
{
378
- _testembed_Py_Initialize ();
401
+ _testembed_Py_InitializeFromConfig ();
379
402
380
403
/* bpo-33932: Calling Py_Initialize() twice should do nothing
381
404
* (and not crash!). */
@@ -393,7 +416,7 @@ static int test_initialize_pymain(void)
393
416
L"print(f'Py_Main() after Py_Initialize: "
394
417
L"sys.argv={sys.argv}')" ),
395
418
L"arg2" };
396
- _testembed_Py_Initialize ();
419
+ _testembed_Py_InitializeFromConfig ();
397
420
398
421
/* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
399
422
Py_Main (Py_ARRAY_LENGTH (argv ), argv );
@@ -416,7 +439,7 @@ dump_config(void)
416
439
417
440
static int test_init_initialize_config (void )
418
441
{
419
- _testembed_Py_Initialize ();
442
+ _testembed_Py_InitializeFromConfig ();
420
443
dump_config ();
421
444
Py_Finalize ();
422
445
return 0 ;
@@ -767,7 +790,7 @@ static int test_init_compat_env(void)
767
790
/* Test initialization from environment variables */
768
791
Py_IgnoreEnvironmentFlag = 0 ;
769
792
set_all_env_vars ();
770
- _testembed_Py_Initialize ();
793
+ _testembed_Py_InitializeFromConfig ();
771
794
dump_config ();
772
795
Py_Finalize ();
773
796
return 0 ;
@@ -803,7 +826,7 @@ static int test_init_env_dev_mode(void)
803
826
/* Test initialization from environment variables */
804
827
Py_IgnoreEnvironmentFlag = 0 ;
805
828
set_all_env_vars_dev_mode ();
806
- _testembed_Py_Initialize ();
829
+ _testembed_Py_InitializeFromConfig ();
807
830
dump_config ();
808
831
Py_Finalize ();
809
832
return 0 ;
@@ -816,7 +839,7 @@ static int test_init_env_dev_mode_alloc(void)
816
839
Py_IgnoreEnvironmentFlag = 0 ;
817
840
set_all_env_vars_dev_mode ();
818
841
putenv ("PYTHONMALLOC=malloc" );
819
- _testembed_Py_Initialize ();
842
+ _testembed_Py_InitializeFromConfig ();
820
843
dump_config ();
821
844
Py_Finalize ();
822
845
return 0 ;
@@ -1156,7 +1179,7 @@ static int test_open_code_hook(void)
1156
1179
}
1157
1180
1158
1181
Py_IgnoreEnvironmentFlag = 0 ;
1159
- _testembed_Py_Initialize ();
1182
+ _testembed_Py_InitializeFromConfig ();
1160
1183
result = 0 ;
1161
1184
1162
1185
PyObject * r = PyFile_OpenCode ("$$test-filename" );
@@ -1220,7 +1243,7 @@ static int _test_audit(Py_ssize_t setValue)
1220
1243
1221
1244
Py_IgnoreEnvironmentFlag = 0 ;
1222
1245
PySys_AddAuditHook (_audit_hook , & sawSet );
1223
- _testembed_Py_Initialize ();
1246
+ _testembed_Py_InitializeFromConfig ();
1224
1247
1225
1248
if (PySys_Audit ("_testembed.raise" , NULL ) == 0 ) {
1226
1249
printf ("No error raised" );
@@ -1276,7 +1299,7 @@ static int test_audit_subinterpreter(void)
1276
1299
{
1277
1300
Py_IgnoreEnvironmentFlag = 0 ;
1278
1301
PySys_AddAuditHook (_audit_subinterpreter_hook , NULL );
1279
- _testembed_Py_Initialize ();
1302
+ _testembed_Py_InitializeFromConfig ();
1280
1303
1281
1304
Py_NewInterpreter ();
1282
1305
Py_NewInterpreter ();
@@ -1871,13 +1894,13 @@ static int test_unicode_id_init(void)
1871
1894
_Py_IDENTIFIER (test_unicode_id_init );
1872
1895
1873
1896
// Initialize Python once without using the identifier
1874
- _testembed_Py_Initialize ();
1897
+ _testembed_Py_InitializeFromConfig ();
1875
1898
Py_Finalize ();
1876
1899
1877
1900
// Now initialize Python multiple times and use the identifier.
1878
1901
// The first _PyUnicode_FromId() call initializes the identifier index.
1879
1902
for (int i = 0 ; i < 3 ; i ++ ) {
1880
- _testembed_Py_Initialize ();
1903
+ _testembed_Py_InitializeFromConfig ();
1881
1904
1882
1905
PyObject * str1 , * str2 ;
1883
1906
@@ -2021,7 +2044,7 @@ unwrap_allocator(PyMemAllocatorEx *allocator)
2021
2044
static int
2022
2045
test_get_incomplete_frame (void )
2023
2046
{
2024
- _testembed_Py_Initialize ();
2047
+ _testembed_Py_InitializeFromConfig ();
2025
2048
PyMemAllocatorEx allocator ;
2026
2049
wrap_allocator (& allocator );
2027
2050
// Force an allocation with an incomplete (generator) frame:
@@ -2053,6 +2076,7 @@ struct TestCase
2053
2076
static struct TestCase TestCases [] = {
2054
2077
// Python initialization
2055
2078
{"test_repeated_init_exec" , test_repeated_init_exec },
2079
+ {"test_repeated_simple_init" , test_repeated_simple_init },
2056
2080
{"test_forced_io_encoding" , test_forced_io_encoding },
2057
2081
{"test_repeated_init_and_subinterpreters" , test_repeated_init_and_subinterpreters },
2058
2082
{"test_repeated_init_and_inittab" , test_repeated_init_and_inittab },
0 commit comments