Skip to content

Commit 6d9ec4e

Browse files
authored
gh-133644: Avoid deprecated Py_SetProgramName() in _testembed.c (#133665)
* Rename _testembed_Py_InitializeFromConfig() to _testembed_initialize(). * Replace _testembed_Py_Initialize() with _testembed_initialize().
1 parent de28651 commit 6d9ec4e

File tree

2 files changed

+25
-55
lines changed

2 files changed

+25
-55
lines changed

Lib/test/test_embed.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def test_pre_initialization_api(self):
296296
if MS_WINDOWS:
297297
expected_path = self.test_exe
298298
else:
299-
expected_path = os.path.join(os.getcwd(), "spam")
299+
expected_path = os.path.join(os.getcwd(), "_testembed")
300300
expected_output = f"sys.executable: {expected_path}\n"
301301
self.assertIn(expected_output, out)
302302
self.assertEqual(err, '')
@@ -969,7 +969,6 @@ def test_init_global_config(self):
969969
'utf8_mode': True,
970970
}
971971
config = {
972-
'program_name': './globalvar',
973972
'site_import': False,
974973
'bytes_warning': True,
975974
'warnoptions': ['default::BytesWarning'],

Programs/_testembed.c

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,18 @@ static void init_from_config_clear(PyConfig *config)
8080
}
8181

8282

83-
static void _testembed_Py_InitializeFromConfig(void)
83+
static void _testembed_initialize(void)
8484
{
8585
PyConfig config;
8686
_PyConfig_InitCompatConfig(&config);
8787
config_set_program_name(&config);
8888
init_from_config_clear(&config);
8989
}
9090

91-
static void _testembed_Py_Initialize(void)
92-
{
93-
Py_SetProgramName(PROGRAM_NAME);
94-
Py_Initialize();
95-
}
96-
9791

9892
static int test_import_in_subinterpreters(void)
9993
{
100-
_testembed_Py_InitializeFromConfig();
94+
_testembed_initialize();
10195
PyThreadState_Swap(Py_NewInterpreter());
10296
return PyRun_SimpleString("import readline"); // gh-124160
10397
}
@@ -131,7 +125,7 @@ static int test_repeated_init_and_subinterpreters(void)
131125

132126
for (int i=1; i <= INIT_LOOPS; i++) {
133127
printf("--- Pass %d ---\n", i);
134-
_testembed_Py_InitializeFromConfig();
128+
_testembed_initialize();
135129
mainstate = PyThreadState_Get();
136130

137131
PyEval_ReleaseThread(mainstate);
@@ -197,7 +191,7 @@ static int test_repeated_init_exec(void)
197191
code = main_argv[i+2];
198192
}
199193

200-
_testembed_Py_InitializeFromConfig();
194+
_testembed_initialize();
201195
int err = PyRun_SimpleString(code);
202196
Py_Finalize();
203197
if (err) {
@@ -217,7 +211,7 @@ static int test_repeated_simple_init(void)
217211
fprintf(stderr, "--- Loop #%d ---\n", i);
218212
fflush(stderr);
219213

220-
_testembed_Py_Initialize();
214+
_testembed_initialize();
221215
Py_Finalize();
222216
printf("Finalized\n"); // Give test_embed some output to check
223217
}
@@ -301,24 +295,8 @@ static int test_pre_initialization_api(void)
301295
/* the test doesn't support custom memory allocators */
302296
putenv("PYTHONMALLOC=");
303297

304-
/* Leading "./" ensures getpath.c can still find the standard library */
305-
_Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");
306-
wchar_t *program = Py_DecodeLocale("./spam", NULL);
307-
if (program == NULL) {
308-
fprintf(stderr, "Fatal error: cannot decode program name\n");
309-
return 1;
310-
}
311-
_Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n");
312-
Py_SetProgramName(program);
313-
314-
_Py_EMBED_PREINIT_CHECK("Checking !Py_IsInitialized pre-initialization\n");
315-
if (Py_IsInitialized()) {
316-
fprintf(stderr, "Fatal error: initialized before initialization!\n");
317-
return 1;
318-
}
319-
320298
_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
321-
Py_Initialize();
299+
_testembed_initialize();
322300

323301
_Py_EMBED_PREINIT_CHECK("Checking Py_IsInitialized post-initialization\n");
324302
if (!Py_IsInitialized()) {
@@ -340,9 +318,6 @@ static int test_pre_initialization_api(void)
340318
fprintf(stderr, "Fatal error: still initialized after finalization!\n");
341319
return 1;
342320
}
343-
344-
_Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n");
345-
PyMem_RawFree(program);
346321
return 0;
347322
}
348323

@@ -384,7 +359,7 @@ static int test_pre_initialization_sys_options(void)
384359
dynamic_xoption = NULL;
385360

386361
_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
387-
_testembed_Py_InitializeFromConfig();
362+
_testembed_initialize();
388363
_Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
389364
PyRun_SimpleString(
390365
"import sys; "
@@ -431,7 +406,7 @@ static int test_bpo20891(void)
431406
return 1;
432407
}
433408

434-
_testembed_Py_InitializeFromConfig();
409+
_testembed_initialize();
435410

436411
unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
437412
if (thrd == PYTHREAD_INVALID_THREAD_ID) {
@@ -454,7 +429,7 @@ static int test_bpo20891(void)
454429

455430
static int test_initialize_twice(void)
456431
{
457-
_testembed_Py_InitializeFromConfig();
432+
_testembed_initialize();
458433

459434
/* bpo-33932: Calling Py_Initialize() twice should do nothing
460435
* (and not crash!). */
@@ -472,7 +447,7 @@ static int test_initialize_pymain(void)
472447
L"print(f'Py_Main() after Py_Initialize: "
473448
L"sys.argv={sys.argv}')"),
474449
L"arg2"};
475-
_testembed_Py_InitializeFromConfig();
450+
_testembed_initialize();
476451

477452
/* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
478453
Py_Main(Py_ARRAY_LENGTH(argv), argv);
@@ -495,7 +470,7 @@ dump_config(void)
495470

496471
static int test_init_initialize_config(void)
497472
{
498-
_testembed_Py_InitializeFromConfig();
473+
_testembed_initialize();
499474
dump_config();
500475
Py_Finalize();
501476
return 0;
@@ -569,9 +544,6 @@ static int test_init_global_config(void)
569544
putenv("PYTHONUTF8=0");
570545
Py_UTF8Mode = 1;
571546

572-
/* Test initialization from global configuration variables (Py_xxx) */
573-
Py_SetProgramName(L"./globalvar");
574-
575547
/* Py_IsolatedFlag is not tested */
576548
Py_NoSiteFlag = 1;
577549
Py_BytesWarningFlag = 1;
@@ -604,7 +576,7 @@ static int test_init_global_config(void)
604576
/* FIXME: test Py_LegacyWindowsFSEncodingFlag */
605577
/* FIXME: test Py_LegacyWindowsStdioFlag */
606578

607-
Py_Initialize();
579+
_testembed_initialize();
608580
dump_config();
609581
Py_Finalize();
610582
return 0;
@@ -666,7 +638,6 @@ static int test_init_from_config(void)
666638
putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
667639
config_set_string(&config, &config.pycache_prefix, L"conf_pycache_prefix");
668640

669-
Py_SetProgramName(L"./globalvar");
670641
config_set_string(&config, &config.program_name, L"./conf_program_name");
671642

672643
wchar_t* argv[] = {
@@ -853,7 +824,7 @@ static int test_init_compat_env(void)
853824
/* Test initialization from environment variables */
854825
Py_IgnoreEnvironmentFlag = 0;
855826
set_all_env_vars();
856-
_testembed_Py_InitializeFromConfig();
827+
_testembed_initialize();
857828
dump_config();
858829
Py_Finalize();
859830
return 0;
@@ -889,7 +860,7 @@ static int test_init_env_dev_mode(void)
889860
/* Test initialization from environment variables */
890861
Py_IgnoreEnvironmentFlag = 0;
891862
set_all_env_vars_dev_mode();
892-
_testembed_Py_InitializeFromConfig();
863+
_testembed_initialize();
893864
dump_config();
894865
Py_Finalize();
895866
return 0;
@@ -906,7 +877,7 @@ static int test_init_env_dev_mode_alloc(void)
906877
#else
907878
putenv("PYTHONMALLOC=mimalloc");
908879
#endif
909-
_testembed_Py_InitializeFromConfig();
880+
_testembed_initialize();
910881
dump_config();
911882
Py_Finalize();
912883
return 0;
@@ -1246,7 +1217,7 @@ static int test_open_code_hook(void)
12461217
}
12471218

12481219
Py_IgnoreEnvironmentFlag = 0;
1249-
_testembed_Py_InitializeFromConfig();
1220+
_testembed_initialize();
12501221
result = 0;
12511222

12521223
PyObject *r = PyFile_OpenCode("$$test-filename");
@@ -1310,7 +1281,7 @@ static int _test_audit(Py_ssize_t setValue)
13101281

13111282
Py_IgnoreEnvironmentFlag = 0;
13121283
PySys_AddAuditHook(_audit_hook, &sawSet);
1313-
_testembed_Py_InitializeFromConfig();
1284+
_testembed_initialize();
13141285

13151286
if (PySys_Audit("_testembed.raise", NULL) == 0) {
13161287
printf("No error raised");
@@ -1369,7 +1340,7 @@ static int test_audit_tuple(void)
13691340
// we need at least one hook, otherwise code checking for
13701341
// PySys_AuditTuple() is skipped.
13711342
PySys_AddAuditHook(_audit_hook, &sawSet);
1372-
_testembed_Py_InitializeFromConfig();
1343+
_testembed_initialize();
13731344

13741345
ASSERT(!PyErr_Occurred(), 0);
13751346

@@ -1422,7 +1393,7 @@ static int test_audit_subinterpreter(void)
14221393
{
14231394
Py_IgnoreEnvironmentFlag = 0;
14241395
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
1425-
_testembed_Py_InitializeFromConfig();
1396+
_testembed_initialize();
14261397

14271398
Py_NewInterpreter();
14281399
Py_NewInterpreter();
@@ -2166,13 +2137,13 @@ static int test_unicode_id_init(void)
21662137
};
21672138

21682139
// Initialize Python once without using the identifier
2169-
_testembed_Py_InitializeFromConfig();
2140+
_testembed_initialize();
21702141
Py_Finalize();
21712142

21722143
// Now initialize Python multiple times and use the identifier.
21732144
// The first _PyUnicode_FromId() call initializes the identifier index.
21742145
for (int i=0; i<3; i++) {
2175-
_testembed_Py_InitializeFromConfig();
2146+
_testembed_initialize();
21762147

21772148
PyObject *str1, *str2;
21782149

@@ -2195,7 +2166,7 @@ static int test_unicode_id_init(void)
21952166

21962167
static int test_init_main_interpreter_settings(void)
21972168
{
2198-
_testembed_Py_Initialize();
2169+
_testembed_initialize();
21992170
(void) PyRun_SimpleStringFlags(
22002171
"import _testinternalcapi, json; "
22012172
"print(json.dumps(_testinternalcapi.get_interp_settings(0)))",
@@ -2206,7 +2177,7 @@ static int test_init_main_interpreter_settings(void)
22062177

22072178
static void do_init(void *unused)
22082179
{
2209-
_testembed_Py_Initialize();
2180+
_testembed_initialize();
22102181
Py_Finalize();
22112182
}
22122183

@@ -2331,7 +2302,7 @@ unwrap_allocator(PyMemAllocatorEx *allocator)
23312302
static int
23322303
test_get_incomplete_frame(void)
23332304
{
2334-
_testembed_Py_InitializeFromConfig();
2305+
_testembed_initialize();
23352306
PyMemAllocatorEx allocator;
23362307
wrap_allocator(&allocator);
23372308
// Force an allocation with an incomplete (generator) frame:

0 commit comments

Comments
 (0)