Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.3k
gh-139482: speed up os.environ.clear by 2x#139483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
850a898724857067cc1408c1ca20e11660456718b4655d2c5c3390e3b348afabe5b530File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1494,6 +1494,71 @@ def test_reload_environ(self): | ||||||||||
| self.assertNotIn(b'test_env', os.environb) | ||||||||||
| self.assertNotIn('test_env', os.environ) | ||||||||||
| def test_clear_empties_environ(self): | ||||||||||
| os.environ["test_key_to_clear1"] = "test_value_to_clear1" | ||||||||||
| os.environ["test_key_to_clear2"] = "test_value_to_clear2" | ||||||||||
| os.environ["test_key_to_clear3"] = "test_value_to_clear3" | ||||||||||
Comment on lines
+1498
to
+1500
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that 3 variables are useful. IMO a single variable to make sure that os.environ is non-empty is enough.
Suggested change
Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to also test the edge case when an errorneous | ||||||||||
| # Test environ.clear() removes the environment variables | ||||||||||
| os.environ.clear() | ||||||||||
| self.assertEqual(os.environ, {}) | ||||||||||
| if os.supports_bytes_environ: | ||||||||||
| self.assertEqual(os.environb, {}) | ||||||||||
| # Repeated calls should be idempotent | ||||||||||
| os.environ.clear() | ||||||||||
| self.assertEqual(os.environ, {}) | ||||||||||
| if os.supports_bytes_environ: | ||||||||||
| self.assertEqual(os.environb, {}) | ||||||||||
| @unittest.skipUnless(os.supports_bytes_environ, "os.environb required for this test.") | ||||||||||
| def test_clear_empties_environb(self): | ||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is more or less the same as test_clear_empties_environ() but on os.environb. I'm not sure that it's useful. Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not useful? Otherwise side-effects of | ||||||||||
| os.environb[b"test_key_to_clear1"] = b"test_value_to_clear1" | ||||||||||
| os.environb[b"test_key_to_clear2"] = b"test_value_to_clear2" | ||||||||||
| os.environb[b"test_key_to_clear3"] = b"test_value_to_clear3" | ||||||||||
| # Test environ.clear() removes the environment variables | ||||||||||
| os.environb.clear() | ||||||||||
| self.assertEqual(os.environ, {}) | ||||||||||
| self.assertEqual(os.environb, {}) | ||||||||||
| # Repeated calls should be idempotent | ||||||||||
| os.environb.clear() | ||||||||||
| self.assertEqual(os.environ, {}) | ||||||||||
| self.assertEqual(os.environb, {}) | ||||||||||
| def test_clear_empties_process_environment(self): | ||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced that this test is useful. Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? The point of calling This test verifies that this modification actually happens in case of | ||||||||||
| # Determine if on the current platform os.unsetenv() | ||||||||||
| # updates process environment. | ||||||||||
| os.environ['to_remove'] = 'value' | ||||||||||
| os.unsetenv('to_remove') | ||||||||||
| os.reload_environ() | ||||||||||
| if 'to_remove' in os.environ: | ||||||||||
| self.skipTest("os.unsetenv() doesn't update the process environment on this platform.") | ||||||||||
| # Set up two environment variables to be cleared | ||||||||||
| os.environ["test_env1"] = "some_value1" | ||||||||||
| os.environ["test_env2"] = "some_value2" | ||||||||||
| # Ensure the variables were persisted on process level. | ||||||||||
| os.reload_environ() | ||||||||||
| self.assertEqual(os.getenv("test_env1"), "some_value1") | ||||||||||
| self.assertEqual(os.getenv("test_env2"), "some_value2") | ||||||||||
| # Test that os.clear() clears both os.environ and os.environb | ||||||||||
| os.environ.clear() | ||||||||||
| self.assertEqual(os.environ, {}) | ||||||||||
| if os.supports_bytes_environ: | ||||||||||
| self.assertEqual(os.environb, {}) | ||||||||||
| # Test that os.clear() also clears the process environment, | ||||||||||
| # so that after os.reload_environ() environ and environb are still empty. | ||||||||||
| os.reload_environ() | ||||||||||
| self.assertEqual(os.environ, {}) | ||||||||||
| if os.supports_bytes_environ: | ||||||||||
| self.assertEqual(os.environb, {}) | ||||||||||
| class WalkTests(unittest.TestCase): | ||||||||||
| """Tests for os.walk().""" | ||||||||||
| is_fwalk = False | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Ensure that :data:`os.environ.clear() <os.environ>` | ||
| has linear complexity instead of quadratic complexity. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.