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.
This commit is contained in:
Victor Stinner authored and GitHub committed 2026-09-23 17:46:59 +02:00
1 parent 40edefa348
commit becfd65c38
4 files changed
+58 -5

No files matched your search

+4
View File
@@ -2114,6 +2114,10 @@ class AuditingTests(EmbeddingTestsMixin, unittest.TestCase):
def test_concurrent_finalization_stress(self):
self.run_embedded_interpreter("test_concurrent_finalization_stress")
def test_py_getenv(self):
# Test Py_GETENV() before init, when initialized, and after finalize
self.run_embedded_interpreter("test_py_getenv")
class MiscTests(EmbeddingTestsMixin, unittest.TestCase):
def test_unicode_id_init(self):
+38
View File
@@ -2357,6 +2357,43 @@ static int test_isinitialized_false_during_site_import(void)
}
static int test_py_getenv(void)
{
const char *name = "PYTHON_TESTEMBED_VARIABLE";
const char *expected = "expected_value";
putenv("PYTHON_TESTEMBED_VARIABLE=expected_value");
const char *var = Py_GETENV(name);
if (var == NULL || strcmp(var, expected) != 0) {
error_fmt("%s is not set before Python initialization", name);
return 1;
}
// Initialize Python with use_environment=0
PyConfig config;
_PyConfig_InitCompatConfig(&config);
config_set_program_name(&config);
config.use_environment = 0;
init_from_config_clear(&config);
var = Py_GETENV(name);
if (var != NULL) {
error_fmt("Py_GETENV() doesn't ignore %s after Python init", name);
return 1;
}
Py_Finalize();
var = Py_GETENV(name);
if (var == NULL || strcmp(var, expected) != 0) {
error_fmt("%s is not set after Python finalization", name);
return 1;
}
printf("OK\n");
return 0;
}
#ifndef MS_WINDOWS
#include "test_frozenmain.h" // M_test_frozenmain
@@ -3059,6 +3096,7 @@ static struct TestCase TestCases[] = {
{"test_init_main_interpreter_settings", test_init_main_interpreter_settings},
{"test_init_in_background_thread", test_init_in_background_thread},
{"test_isinitialized_false_during_site_import", test_isinitialized_false_during_site_import},
{"test_py_getenv", test_py_getenv},
// Audit
{"test_open_code_hook", test_open_code_hook},
+8 -4
View File
@@ -712,13 +712,17 @@ _Py_COMP_DIAG_POP
char*
Py_GETENV(const char *name)
{
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
if (Py_IgnoreEnvironmentFlag) {
int use_environment = 1;
PyThreadState *tstate = PyThreadState_GetUnchecked();
if (tstate != NULL) {
const PyConfig *config = &tstate->interp->config;
use_environment = config->use_environment;
}
if (!use_environment) {
return NULL;
}
return getenv(name);
_Py_COMP_DIAG_POP
}
/* --- PyStatus ----------------------------------------------- */
+8 -1
View File
@@ -4540,9 +4540,16 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
void
PySys_SetArgv(int argc, wchar_t **argv)
{
int isolated = 0;
PyThreadState *tstate = PyThreadState_GetUnchecked();
if (tstate != NULL) {
const PyConfig *config = &tstate->interp->config;
isolated = config->isolated;
}
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
PySys_SetArgvEx(argc, argv, Py_IsolatedFlag == 0);
PySys_SetArgvEx(argc, argv, isolated == 0);
_Py_COMP_DIAG_POP
}