@@ -1298,6 +1298,52 @@ def test_ror_operator(self):
12981298 self ._test_underlying_process_env ('_A_' , '' )
12991299 self ._test_underlying_process_env (overridden_key , original_value )
13001300
1301+ def test_refresh (self ):
1302+ # Test os.environ.refresh()
1303+ has_environb = hasattr (os , 'environb' )
1304+
1305+ # Test with putenv() which doesn't update os.environ
1306+ os .environ ['test_env' ] = 'python_value'
1307+ os .putenv ("test_env" , "new_value" )
1308+ self .assertEqual (os .environ ['test_env' ], 'python_value' )
1309+ if has_environb :
1310+ self .assertEqual (os .environb [b'test_env' ], b'python_value' )
1311+
1312+ os .environ .refresh ()
1313+ self .assertEqual (os .environ ['test_env' ], 'new_value' )
1314+ if has_environb :
1315+ self .assertEqual (os .environb [b'test_env' ], b'new_value' )
1316+
1317+ # Test with unsetenv() which doesn't update os.environ
1318+ os .unsetenv ('test_env' )
1319+ self .assertEqual (os .environ ['test_env' ], 'new_value' )
1320+ if has_environb :
1321+ self .assertEqual (os .environb [b'test_env' ], b'new_value' )
1322+
1323+ os .environ .refresh ()
1324+ self .assertNotIn ('test_env' , os .environ )
1325+ if has_environb :
1326+ self .assertNotIn (b'test_env' , os .environb )
1327+
1328+ if has_environb :
1329+ # test os.environb.refresh() with putenv()
1330+ os .environb [b'test_env' ] = b'python_value2'
1331+ os .putenv ("test_env" , "new_value2" )
1332+ self .assertEqual (os .environb [b'test_env' ], b'python_value2' )
1333+ self .assertEqual (os .environ ['test_env' ], 'python_value2' )
1334+
1335+ os .environb .refresh ()
1336+ self .assertEqual (os .environb [b'test_env' ], b'new_value2' )
1337+ self .assertEqual (os .environ ['test_env' ], 'new_value2' )
1338+
1339+ # test os.environb.refresh() with unsetenv()
1340+ os .unsetenv ('test_env' )
1341+ self .assertEqual (os .environb [b'test_env' ], b'new_value2' )
1342+ self .assertEqual (os .environ ['test_env' ], 'new_value2' )
1343+
1344+ os .environb .refresh ()
1345+ self .assertNotIn (b'test_env' , os .environb )
1346+ self .assertNotIn ('test_env' , os .environ )
13011347
13021348class WalkTests (unittest .TestCase ):
13031349 """Tests for os.walk()."""
0 commit comments