Skip to content

gh-133644: Avoid deprecated Py_SetProgramName() in _testembed.c #133665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def test_pre_initialization_api(self):
if MS_WINDOWS:
expected_path = self.test_exe
else:
expected_path = os.path.join(os.getcwd(), "spam")
expected_path = os.path.join(os.getcwd(), "_testembed")
expected_output = f"sys.executable: {expected_path}\n"
self.assertIn(expected_output, out)
self.assertEqual(err, '')
Expand Down Expand Up @@ -969,7 +969,6 @@ def test_init_global_config(self):
'utf8_mode': True,
}
config = {
'program_name': './globalvar',
'site_import': False,
'bytes_warning': True,
'warnoptions': ['default::BytesWarning'],
Expand Down
77 changes: 24 additions & 53 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,18 @@ static void init_from_config_clear(PyConfig *config)
}


static void _testembed_Py_InitializeFromConfig(void)
static void _testembed_initialize(void)
{
PyConfig config;
_PyConfig_InitCompatConfig(&config);
config_set_program_name(&config);
init_from_config_clear(&config);
}

static void _testembed_Py_Initialize(void)
{
Py_SetProgramName(PROGRAM_NAME);
Py_Initialize();
}


static int test_import_in_subinterpreters(void)
{
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
PyThreadState_Swap(Py_NewInterpreter());
return PyRun_SimpleString("import readline"); // gh-124160
}
Expand Down Expand Up @@ -131,7 +125,7 @@ static int test_repeated_init_and_subinterpreters(void)

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

PyEval_ReleaseThread(mainstate);
Expand Down Expand Up @@ -197,7 +191,7 @@ static int test_repeated_init_exec(void)
code = main_argv[i+2];
}

_testembed_Py_InitializeFromConfig();
_testembed_initialize();
int err = PyRun_SimpleString(code);
Py_Finalize();
if (err) {
Expand All @@ -217,7 +211,7 @@ static int test_repeated_simple_init(void)
fprintf(stderr, "--- Loop #%d ---\n", i);
fflush(stderr);

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

/* Leading "./" ensures getpath.c can still find the standard library */
_Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");
wchar_t *program = Py_DecodeLocale("./spam", NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode program name\n");
return 1;
}
_Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n");
Py_SetProgramName(program);

_Py_EMBED_PREINIT_CHECK("Checking !Py_IsInitialized pre-initialization\n");
if (Py_IsInitialized()) {
fprintf(stderr, "Fatal error: initialized before initialization!\n");
return 1;
}

_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
Py_Initialize();
_testembed_initialize();

_Py_EMBED_PREINIT_CHECK("Checking Py_IsInitialized post-initialization\n");
if (!Py_IsInitialized()) {
Expand All @@ -340,9 +318,6 @@ static int test_pre_initialization_api(void)
fprintf(stderr, "Fatal error: still initialized after finalization!\n");
return 1;
}

_Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n");
PyMem_RawFree(program);
return 0;
}

Expand Down Expand Up @@ -384,7 +359,7 @@ static int test_pre_initialization_sys_options(void)
dynamic_xoption = NULL;

_Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
_Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
PyRun_SimpleString(
"import sys; "
Expand Down Expand Up @@ -431,7 +406,7 @@ static int test_bpo20891(void)
return 1;
}

_testembed_Py_InitializeFromConfig();
_testembed_initialize();

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

static int test_initialize_twice(void)
{
_testembed_Py_InitializeFromConfig();
_testembed_initialize();

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

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

static int test_init_initialize_config(void)
{
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
dump_config();
Py_Finalize();
return 0;
Expand Down Expand Up @@ -569,9 +544,6 @@ static int test_init_global_config(void)
putenv("PYTHONUTF8=0");
Py_UTF8Mode = 1;

/* Test initialization from global configuration variables (Py_xxx) */
Py_SetProgramName(L"./globalvar");

/* Py_IsolatedFlag is not tested */
Py_NoSiteFlag = 1;
Py_BytesWarningFlag = 1;
Expand Down Expand Up @@ -604,7 +576,7 @@ static int test_init_global_config(void)
/* FIXME: test Py_LegacyWindowsFSEncodingFlag */
/* FIXME: test Py_LegacyWindowsStdioFlag */

Py_Initialize();
_testembed_initialize();
dump_config();
Py_Finalize();
return 0;
Expand Down Expand Up @@ -666,7 +638,6 @@ static int test_init_from_config(void)
putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
config_set_string(&config, &config.pycache_prefix, L"conf_pycache_prefix");

Py_SetProgramName(L"./globalvar");
config_set_string(&config, &config.program_name, L"./conf_program_name");

wchar_t* argv[] = {
Expand Down Expand Up @@ -853,7 +824,7 @@ static int test_init_compat_env(void)
/* Test initialization from environment variables */
Py_IgnoreEnvironmentFlag = 0;
set_all_env_vars();
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
dump_config();
Py_Finalize();
return 0;
Expand Down Expand Up @@ -889,7 +860,7 @@ static int test_init_env_dev_mode(void)
/* Test initialization from environment variables */
Py_IgnoreEnvironmentFlag = 0;
set_all_env_vars_dev_mode();
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
dump_config();
Py_Finalize();
return 0;
Expand All @@ -906,7 +877,7 @@ static int test_init_env_dev_mode_alloc(void)
#else
putenv("PYTHONMALLOC=mimalloc");
#endif
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
dump_config();
Py_Finalize();
return 0;
Expand Down Expand Up @@ -1246,7 +1217,7 @@ static int test_open_code_hook(void)
}

Py_IgnoreEnvironmentFlag = 0;
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
result = 0;

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

Py_IgnoreEnvironmentFlag = 0;
PySys_AddAuditHook(_audit_hook, &sawSet);
_testembed_Py_InitializeFromConfig();
_testembed_initialize();

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

ASSERT(!PyErr_Occurred(), 0);

Expand Down Expand Up @@ -1422,7 +1393,7 @@ static int test_audit_subinterpreter(void)
{
Py_IgnoreEnvironmentFlag = 0;
PySys_AddAuditHook(_audit_subinterpreter_hook, NULL);
_testembed_Py_InitializeFromConfig();
_testembed_initialize();

Py_NewInterpreter();
Py_NewInterpreter();
Expand Down Expand Up @@ -2166,13 +2137,13 @@ static int test_unicode_id_init(void)
};

// Initialize Python once without using the identifier
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
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_InitializeFromConfig();
_testembed_initialize();

PyObject *str1, *str2;

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

static int test_init_main_interpreter_settings(void)
{
_testembed_Py_Initialize();
_testembed_initialize();
(void) PyRun_SimpleStringFlags(
"import _testinternalcapi, json; "
"print(json.dumps(_testinternalcapi.get_interp_settings(0)))",
Expand All @@ -2206,7 +2177,7 @@ static int test_init_main_interpreter_settings(void)

static void do_init(void *unused)
{
_testembed_Py_Initialize();
_testembed_initialize();
Py_Finalize();
}

Expand Down Expand Up @@ -2331,7 +2302,7 @@ unwrap_allocator(PyMemAllocatorEx *allocator)
static int
test_get_incomplete_frame(void)
{
_testembed_Py_InitializeFromConfig();
_testembed_initialize();
PyMemAllocatorEx allocator;
wrap_allocator(&allocator);
// Force an allocation with an incomplete (generator) frame:
Expand Down
Loading