-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
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
850a898
7248570
67cc140
8c1ca20
e116604
56718b4
655d2c5
c3390e3
b348afa
be5b530
File filter
Filter by extension
Conversations
Jump to
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
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
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): | ||||||||||
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. 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): | ||||||||||
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. 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.