@@ -1504,6 +1504,76 @@ def test_reload_environ(self):
15041504 self .assertNotIn (b'test_env' , os .environb )
15051505 self .assertNotIn ('test_env' , os .environ )
15061506
1507+ def test_clear_empties_environ (self ):
1508+ os .environ ["test_key_to_clear1" ] = "test_value_to_clear1"
1509+ os .environ ["test_key_to_clear2" ] = "test_value_to_clear2"
1510+ os .environ ["test_key_to_clear3" ] = "test_value_to_clear3"
1511+
1512+ # test the environ.clear() removes the
1513+ os .environ .clear ()
1514+
1515+ self .assertEqual (os .environ , {})
1516+ if os .supports_bytes_environ :
1517+ self .assertEqual (os .environb , {})
1518+
1519+ # repeated calls should be idempotent
1520+ os .environ .clear ()
1521+
1522+ self .assertEqual (os .environ , {})
1523+ if os .supports_bytes_environ :
1524+ self .assertEqual (os .environb , {})
1525+
1526+ @unittest .skipUnless (os .supports_bytes_environ , "os.environb required for this test." )
1527+ def test_clear_empties_environb (self ):
1528+ os .environb [b"test_key_to_clear1" ] = b"test_value_to_clear1"
1529+ os .environb [b"test_key_to_clear2" ] = b"test_value_to_clear2"
1530+ os .environb [b"test_key_to_clear3" ] = b"test_value_to_clear3"
1531+
1532+ # test the environ.clear() removes the
1533+ os .environb .clear ()
1534+
1535+ self .assertEqual (os .environ , {})
1536+ self .assertEqual (os .environb , {})
1537+
1538+ # repeated calls should be idempotent
1539+ os .environb .clear ()
1540+
1541+ self .assertEqual (os .environ , {})
1542+ self .assertEqual (os .environb , {})
1543+
1544+ def test_clear_empties_process_environment (self ):
1545+ # Determine if on the current platform os.unsetenv()
1546+ # updates process environment.
1547+ os .environ ['to_remove' ] = 'value'
1548+ os .unsetenv ('to_remove' )
1549+ os .reload_environ ()
1550+ if 'to_remove' in os .environ :
1551+ self .skipTest ("os.unsetenv() doesn't update the process environment on this platform." )
1552+
1553+ # Set up two environment variables to be cleared
1554+ os .environ ["test_env1" ] = "some_value1"
1555+ os .environ ["test_env2" ] = "some_value2"
1556+
1557+ # Ensure the variables were persisted on process level.
1558+ os .reload_environ ()
1559+ self .assertEqual (os .getenv ("test_env1" ), "some_value1" )
1560+ self .assertEqual (os .getenv ("test_env2" ), "some_value2" )
1561+
1562+ # Test that os.clear() clears both os.environ and os.environb
1563+ os .environ .clear ()
1564+
1565+ self .assertEqual (os .environ , {})
1566+ if os .supports_bytes_environ :
1567+ self .assertEqual (os .environb , {})
1568+
1569+ # Test that os.clear() also clears the process environment,
1570+ # so that after os.reload_environ() environ and environb are still empty.
1571+ os .reload_environ ()
1572+ self .assertEqual (os .environ , {})
1573+ if os .supports_bytes_environ :
1574+ self .assertEqual (os .environb , {})
1575+
1576+
15071577class WalkTests (unittest .TestCase ):
15081578 """Tests for os.walk()."""
15091579 is_fwalk = False
0 commit comments