@@ -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 },
0 commit comments