| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 40edefa commit becfd65
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2114,6 +2114,10 @@ def test_thread_state_ensure_from_view(self): | |||
| 2114 | 2114 | def test_concurrent_finalization_stress(self): | |
| 2115 | 2115 | self.run_embedded_interpreter("test_concurrent_finalization_stress") | |
| 2116 | 2116 | ||
| 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 | + | ||
| 2117 | 2121 | ||
| 2118 | 2122 | class MiscTests(EmbeddingTestsMixin, unittest.TestCase): | |
| 2119 | 2123 | def test_unicode_id_init(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2357,6 +2357,43 @@ static int test_isinitialized_false_during_site_import(void) | |||
| 2357 | 2357 | } | |
| 2358 | 2358 | ||
| 2359 | 2359 | ||
| 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 | + | ||
| 2360 | 2397 | #ifndef MS_WINDOWS | |
| 2361 | 2398 | #include "test_frozenmain.h" // M_test_frozenmain | |
| 2362 | 2399 | ||
@@ -3059,6 +3096,7 @@ static struct TestCase TestCases[] = { | |||
| 3059 | 3096 | {"test_init_main_interpreter_settings", test_init_main_interpreter_settings}, | |
| 3060 | 3097 | {"test_init_in_background_thread", test_init_in_background_thread}, | |
| 3061 | 3098 | {"test_isinitialized_false_during_site_import", test_isinitialized_false_during_site_import}, | |
| 3099 | + {"test_py_getenv", test_py_getenv}, | ||
| 3062 | 3100 | ||
| 3063 | 3101 | // Audit | |
| 3064 | 3102 | {"test_open_code_hook", test_open_code_hook}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -712,13 +712,17 @@ _Py_COMP_DIAG_POP | |||
| 712 | 712 | char* | |
| 713 | 713 | Py_GETENV(const char *name) | |
| 714 | 714 | { | |
| 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) { | ||
| 718 | 723 | return NULL; | |
| 719 | 724 | } | |
| 720 | 725 | return getenv(name); | |
| 721 | - _Py_COMP_DIAG_POP | ||
| 722 | 726 | } | |
| 723 | 727 | ||
| 724 | 728 | /* --- PyStatus ----------------------------------------------- */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4540,9 +4540,16 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) | |||
| 4540 | 4540 | void | |
| 4541 | 4541 | PySys_SetArgv(int argc, wchar_t **argv) | |
| 4542 | 4542 | { | |
| 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 | + | ||
| 4543 | 4550 | _Py_COMP_DIAG_PUSH | |
| 4544 | 4551 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS | |
| 4545 | - PySys_SetArgvEx(argc, argv, Py_IsolatedFlag == 0); | ||
| 4552 | + PySys_SetArgvEx(argc, argv, isolated == 0); | ||
| 4546 | 4553 | _Py_COMP_DIAG_POP | |
| 4547 | 4554 | } | |
| 4548 | 4555 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments