Skip to content
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
4 changes: 4 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,10 @@ def test_thread_state_ensure_from_view(self):
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):
Expand Down
38 changes: 38 additions & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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},
Expand Down
12 changes: 8 additions & 4 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------------------- */
Expand Down
9 changes: 8 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading