From df97d03fd01b4b7f55985144e85ce640a9125e94 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 5 Jun 2024 09:39:30 +0200 Subject: [PATCH] Fix os.environb --- Lib/os.py | 5 ++++- Lib/test/test_os.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/os.py b/Lib/os.py index df212c06d985f98..9804136aa4bac42 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -790,7 +790,10 @@ def refresh(self): data[encodekey(key)] = value else: data = environ - self._data = data + + # modify in-place to keep os.environb in sync + self._data.clear() + self._data.update(data) def _create_environ_mapping(): if name == 'nt': diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 38a327038fc1d15..74df151747f64be 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1310,9 +1310,13 @@ def test_refresh(self): os.environ['test_env'] = 'python_value' putenv("test_env", "new_value") self.assertEqual(os.environ['test_env'], 'python_value') + if hasattr(os, 'environb'): + self.assertEqual(os.environb[b'test_env'], b'python_value') os.environ.refresh() self.assertEqual(os.environ['test_env'], 'new_value') + if hasattr(os, 'environb'): + self.assertEqual(os.environb[b'test_env'], b'new_value') class WalkTests(unittest.TestCase):