Skip to content

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

Merged
merged 10 commits into from
Mar 13, 2020

Conversation

chaburkland
Copy link
Contributor

@chaburkland chaburkland commented Mar 11, 2020

@brandtbucher

When implementing __or__ and __ror__, I found there to be two potential candidates for a return value, either a dict, or an os._Environ class.

I decided on returning a dict for two reaons.

  1. It followed the existing pattern of the copy() method
  2. It avoids having two different os._Environ objects that are not in sync with each other.

https://bugs.python.org/issue36144

@brandtbucher brandtbucher added the type-feature A feature request or enhancement label Mar 11, 2020
Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your time @chaburkland, and welcome to CPython! 😉

Just a couple of minor minor comments, but otherwise this looks good:

@brandtbucher
Copy link
Member

@chaburkland It looks like the environment isn't updating properly on the Windows builds.

I think this is because Windows environment variables must be uppercased! Updating all of your test data with UPPERCASED keys should fix the failing tests.

Charles Burkland and others added 4 commits March 11, 2020 09:19
@brandtbucher brandtbucher requested a review from gvanrossum March 11, 2020 16:47
os.environ[overridden_key] = 'original_value'

new_vars_dict = {'_A_': '1', '_B_': '2', overridden_key: '3'}
expected = os.environ.copy()
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


actual = os.environ | new_vars_dict
self.assertDictEqual(expected, actual)
self.assertEqual('3', actual[overridden_key])
Copy link
Member

Choose a reason for hiding this comment

The 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 __ior__.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I just wonder if we can save a few subprocesses by not testing for _B_.

@@ -1041,18 +1051,26 @@ def test_or_operator(self):
new_vars_items = new_vars_dict.items()
self.assertIs(NotImplemented, os.environ.__or__(new_vars_items))

self._test_underlying_process_env('_A_', '')
self._test_underlying_process_env('_B_', '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we gain anything by testing for _B_? It seems it's handled exactly the same as _A_. (I'm asking because this test runs a subprocess, which is pretty expensive compared to almost everything else these tests do.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option could be checking all three with one subprocess.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that would uglify the code. What's gained by checking three instead of two?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anything is gained other than the code "feeling" complete in what it's testing. Admittedly, it's not actually testing different behavior.

The additional subprocess calls aren't very expensive though - it's only adding between 10 & 20 milliseconds. That being said, I don't have a strong feeling about it, so I'd be willing to remove the _B_ tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still something, and not everybody's machine is as fast. Let's cut the _B_ tests -- it's too high a price for a sense of completeness or symmetry.

@gvanrossum gvanrossum merged commit d648ef1 into python:master Mar 13, 2020
@bedevere-bot
Copy link

@gvanrossum: Please replace # with GH- in the commit message next time. Thanks!

@brandtbucher
Copy link
Member

Congrats on your first CPython contribution @chaburkland! 🍾

Looking forward to seeing more from you in the future.

@gvanrossum
Copy link
Member

@chaburkland Congrats indeed! Great job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants