-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-36144: Update os.environ and os.environb for PEP 584 #18911
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 2 commits
66878fa
4f9c057
4b69c50
c42d9d5
399a122
adf2903
571a329
4a2960a
99826fe
7a693ba
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 |
---|---|---|
|
@@ -1026,6 +1026,74 @@ def test_iter_error_when_changing_os_environ_items(self): | |
def test_iter_error_when_changing_os_environ_values(self): | ||
self._test_environ_iteration(os.environ.values()) | ||
|
||
def test_or_operator(self): | ||
overridden_key = '_test_var_' | ||
os.environ[overridden_key] = 'original_value' | ||
|
||
new_vars_dict = {'_a_': '1', '_b_': '2', overridden_key: '3'} | ||
expected = os.environ.copy() | ||
expected.update(new_vars_dict) | ||
|
||
actual = os.environ | new_vars_dict | ||
self.assertDictEqual(expected, actual) | ||
self.assertEqual('3', actual[overridden_key]) | ||
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. Can you find a way to test whether this operation has a side effect on the process environment? (It shouldn't have. But it should for 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. Good point. I extended the tests to check this is behaving as expected. |
||
|
||
new_vars_items = new_vars_dict.items() | ||
self.assertIs(NotImplemented, os.environ.__or__(new_vars_items)) | ||
|
||
def test_ior_operator(self): | ||
overridden_key = '_test_var_' | ||
os.environ[overridden_key] = 'original_value' | ||
|
||
new_vars_dict = {'_a_': '1', '_b_': '2', overridden_key: '3'} | ||
expected = os.environ.copy() | ||
expected.update(new_vars_dict) | ||
|
||
os.environ |= new_vars_dict | ||
self.assertEqual(expected, os.environ) | ||
self.assertEqual('3', os.environ[overridden_key]) | ||
|
||
def test_ior_operator_invalid_dicts(self): | ||
os_environ_copy = os.environ.copy() | ||
with self.assertRaises(TypeError): | ||
dict_with_bad_key = {'a': 1} | ||
chaburkland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
os.environ |= dict_with_bad_key | ||
|
||
with self.assertRaises(TypeError): | ||
dict_with_bad_val = {1: 'a'} | ||
chaburkland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
os.environ |= dict_with_bad_val | ||
|
||
# Check nothing was added. | ||
self.assertEqual(os_environ_copy, os.environ.copy()) | ||
chaburkland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_ior_operator_key_value_iterable(self): | ||
overridden_key = '_test_var_' | ||
os.environ[overridden_key] = 'original_value' | ||
|
||
new_vars_items = (('_a_', '1'), ('_b_', '2'), (overridden_key, '3')) | ||
expected = os.environ.copy() | ||
expected.update(new_vars_items) | ||
|
||
os.environ |= new_vars_items | ||
self.assertEqual(expected, os.environ) | ||
self.assertEqual('3', os.environ[overridden_key]) | ||
|
||
def test_ror_operator(self): | ||
overridden_key = '_test_var_' | ||
original_value = 'original_value' | ||
os.environ[overridden_key] = original_value | ||
|
||
new_vars_dict = {'_a_': '1', '_b_': '2', overridden_key: '3'} | ||
expected = dict(new_vars_dict) | ||
expected.update(os.environ) | ||
|
||
actual = new_vars_dict | os.environ | ||
self.assertDictEqual(expected, actual) | ||
self.assertEqual(original_value, actual[overridden_key]) | ||
|
||
new_vars_items = new_vars_dict.items() | ||
self.assertIs(NotImplemented, os.environ.__ror__(new_vars_items)) | ||
|
||
|
||
class WalkTests(unittest.TestCase): | ||
"""Tests for os.walk().""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Updated :data:`os.environ` and :data:`os.environb` to support :pep:`584`'s | ||
merge (``|``) and update (``|=``) operators. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
dict(os.environ)
to emphasize that we expect the actual value to be a plain dict?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.