Skip to content

Commit becfd65

Browse files
authored
gh-158001: Avoid deprecated variable in Py_GETENV() (#158003)
No longer use deprecated Py_IgnoreEnvironmentFlag in Py_GETENV(). Instead, use PyConfig.use_environment when Python is initialized. Modify also PySys_SetArgv() to use PyConfig.isolated when Python is initialized.
1 parent 40edefa commit becfd65

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

Lib/test/test_embed.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,10 @@ def test_thread_state_ensure_from_view(self):
21142114
def test_concurrent_finalization_stress(self):
21152115
self.run_embedded_interpreter("test_concurrent_finalization_stress")
21162116

2117+
def test_py_getenv(self):
2118+
# Test Py_GETENV() before init, when initialized, and after finalize
2119+
self.run_embedded_interpreter("test_py_getenv")
2120+
21172121

21182122
class MiscTests(EmbeddingTestsMixin, unittest.TestCase):
21192123
def test_unicode_id_init(self):

Programs/_testembed.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,43 @@ static int test_isinitialized_false_during_site_import(void)
23572357
}
23582358

23592359

2360+
static int test_py_getenv(void)
2361+
{
2362+
const char *name = "PYTHON_TESTEMBED_VARIABLE";
2363+
const char *expected = "expected_value";
2364+
putenv("PYTHON_TESTEMBED_VARIABLE=expected_value");
2365+
2366+
const char *var = Py_GETENV(name);
2367+
if (var == NULL || strcmp(var, expected) != 0) {
2368+
error_fmt("%s is not set before Python initialization", name);
2369+
return 1;
2370+
}
2371+
2372+
// Initialize Python with use_environment=0
2373+
PyConfig config;
2374+
_PyConfig_InitCompatConfig(&config);
2375+
config_set_program_name(&config);
2376+
config.use_environment = 0;
2377+
init_from_config_clear(&config);
2378+
2379+
var = Py_GETENV(name);
2380+
if (var != NULL) {
2381+
error_fmt("Py_GETENV() doesn't ignore %s after Python init", name);
2382+
return 1;
2383+
}
2384+
2385+
Py_Finalize();
2386+
var = Py_GETENV(name);
2387+
if (var == NULL || strcmp(var, expected) != 0) {
2388+
error_fmt("%s is not set after Python finalization", name);
2389+
return 1;
2390+
}
2391+
2392+
printf("OK\n");
2393+
return 0;
2394+
}
2395+
2396+
23602397
#ifndef MS_WINDOWS
23612398
#include "test_frozenmain.h" // M_test_frozenmain
23622399

@@ -3059,6 +3096,7 @@ static struct TestCase TestCases[] = {
30593096
{"test_init_main_interpreter_settings", test_init_main_interpreter_settings},
30603097
{"test_init_in_background_thread", test_init_in_background_thread},
30613098
{"test_isinitialized_false_during_site_import", test_isinitialized_false_during_site_import},
3099+
{"test_py_getenv", test_py_getenv},
30623100

30633101
// Audit
30643102
{"test_open_code_hook", test_open_code_hook},

Python/initconfig.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,13 +712,17 @@ _Py_COMP_DIAG_POP
712712
char*
713713
Py_GETENV(const char *name)
714714
{
715-
_Py_COMP_DIAG_PUSH
716-
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
717-
if (Py_IgnoreEnvironmentFlag) {
715+
int use_environment = 1;
716+
PyThreadState *tstate = PyThreadState_GetUnchecked();
717+
if (tstate != NULL) {
718+
const PyConfig *config = &tstate->interp->config;
719+
use_environment = config->use_environment;
720+
}
721+
722+
if (!use_environment) {
718723
return NULL;
719724
}
720725
return getenv(name);
721-
_Py_COMP_DIAG_POP
722726
}
723727

724728
/* --- PyStatus ----------------------------------------------- */

Python/sysmodule.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4540,9 +4540,16 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
45404540
void
45414541
PySys_SetArgv(int argc, wchar_t **argv)
45424542
{
4543+
int isolated = 0;
4544+
PyThreadState *tstate = PyThreadState_GetUnchecked();
4545+
if (tstate != NULL) {
4546+
const PyConfig *config = &tstate->interp->config;
4547+
isolated = config->isolated;
4548+
}
4549+
45434550
_Py_COMP_DIAG_PUSH
45444551
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
4545-
PySys_SetArgvEx(argc, argv, Py_IsolatedFlag == 0);
4552+
PySys_SetArgvEx(argc, argv, isolated == 0);
45464553
_Py_COMP_DIAG_POP
45474554
}
45484555

0 commit comments

Comments
 (0)