Skip to content
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

gh-120057: Add os.environ.refresh() method #120059

Merged
merged 13 commits into from
Jun 10, 2024
10 changes: 7 additions & 3 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ process and user.
to the environment made after this time are not reflected in :data:`os.environ`,
except for changes made by modifying :data:`os.environ` directly.

The :meth:`!os.environ.refresh()` method updates
:data:`os.environ` with changes to the environment made in the same process
outside Python or by :func:`os.putenv`.
The :meth:`!os.environ.refresh()` method updates :data:`os.environ` with
Copy link
Contributor

Choose a reason for hiding this comment

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

FTR, the :meth: and :func: roles will implicitly add parentheses in the rendered output; you do not need to add them explicitly.

changes to the environment made by :func:`os.putenv`, by
:func:`os.unsetenv`, or made outside Python in the same process.

This mapping may be used to modify the environment as well as query the
environment. :func:`putenv` will be called automatically when the mapping
Expand Down Expand Up @@ -568,6 +568,8 @@ process and user.
of :data:`os.environ`. This also applies to :func:`getenv` and :func:`getenvb`, which
respectively use :data:`os.environ` and :data:`os.environb` in their implementations.

See also the :data:`os.environ.refresh() <os.environ>` method.

.. note::

On some platforms, including FreeBSD and macOS, setting ``environ`` may
Expand Down Expand Up @@ -816,6 +818,8 @@ process and user.
don't update :data:`os.environ`, so it is actually preferable to delete items of
:data:`os.environ`.

See also the :data:`os.environ.refresh() <os.environ>` method.

.. audit-event:: os.unsetenv key os.unsetenv

.. versionchanged:: 3.9
Expand Down
6 changes: 3 additions & 3 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ Added :func:`ast.compare` for comparing two ASTs.
os
--

* Added the :meth:`os.environ.refresh() <os.environ>` method to update
:data:`os.environ` with environment changes made in the same process outside
Python or by :func:`os.putenv`.
* Added the :data:`os.environ.refresh() <os.environ>` method to update
:data:`os.environ` with changes to the environment made by :func:`os.putenv`,
by :func:`os.unsetenv`, or made outside Python in the same process.
(Contributed by Victor Stinner in :gh:`120057`.)


Expand Down
6 changes: 2 additions & 4 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,10 @@ def __ror__(self, other):

if _exists("_create_environ"):
def refresh(self):
environ = _create_environ()
data = _create_environ()
if name == 'nt':
data = {self.encodekey(key): value
vstinner marked this conversation as resolved.
Show resolved Hide resolved
for key, value in environ.items()}
else:
data = environ
for key, value in data.items()}

# modify in-place to keep os.environb in sync
self._data.clear()
Expand Down
17 changes: 13 additions & 4 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,13 +1298,11 @@ def test_ror_operator(self):
self._test_underlying_process_env('_A_', '')
self._test_underlying_process_env(overridden_key, original_value)

@unittest.skipUnless(hasattr(os.environ, 'refresh'),
'need os.environ.refresh()')
def test_refresh(self):
# Test os.environ.refresh() with putenv() which doesn't update
# os.environ
# Test os.environ.refresh()
has_environb = hasattr(os, 'environb')

# Test with putenv() which doesn't update os.environ
os.environ['test_env'] = 'python_value'
os.putenv("test_env", "new_value")
vstinner marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(os.environ['test_env'], 'python_value')
Expand All @@ -1316,6 +1314,17 @@ def test_refresh(self):
if has_environb:
self.assertEqual(os.environb[b'test_env'], b'new_value')

# Test with unsetenv() which doesn't update os.environ
vstinner marked this conversation as resolved.
Show resolved Hide resolved
os.unsetenv('test_env')
self.assertEqual(os.environ['test_env'], 'new_value')
if has_environb:
self.assertEqual(os.environb[b'test_env'], b'new_value')

os.environ.refresh()
self.assertNotIn('test_env', os.environ)
if has_environb:
self.assertNotIn(b'test_env', os.environb)

if has_environb:
# test os.environb.refresh()
os.environb[b'test_env'] = b'python_value2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Added the :meth:`os.environ.refresh() <os.environ>` method to update
:data:`os.environ` with environment changes made in the same process outside
Python or by :func:`os.putenv`. Patch by Victor Stinner.
Added the :data:`os.environ.refresh() <os.environ>` method to update
:data:`os.environ` with changes to the environment made by :func:`os.putenv`,
by :func:`os.unsetenv`, or made outside Python in the same process.
Patch by Victor Stinner.
Loading