Skip to content

gh-120057: Rename os.environ.refresh() to invalidate_cache() #120808

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ 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 by :func:`os.putenv`, by
The :meth:`!os.environ.invalidate_cache()` method updates :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.

This mapping may be used to modify the environment as well as query the
Expand Down Expand Up @@ -230,7 +230,7 @@ process and user.
Updated to support :pep:`584`'s merge (``|``) and update (``|=``) operators.

.. versionchanged:: 3.14
Added the :meth:`!os.environ.refresh()` method.
Added the :meth:`!os.environ.invalidate_cache()` method.


.. data:: environb
Expand Down Expand Up @@ -568,7 +568,7 @@ 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.
See also the :data:`os.environ.invalidate_cache() <os.environ>` method.

.. note::

Expand Down Expand Up @@ -818,7 +818,7 @@ 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.
See also the :data:`os.environ.invalidate_cache() <os.environ>` method.

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

Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Added :func:`ast.compare` for comparing two ASTs.
os
--

* Added the :data:`os.environ.refresh() <os.environ>` method to update
* Added the :data:`os.environ.invalidate_cache() <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
2 changes: 1 addition & 1 deletion Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ def __ror__(self, other):
return new

if _exists("_create_environ"):
def refresh(self):
def invalidate_cache(self):
data = _create_environ()
if name == 'nt':
data = {self.encodekey(key): value
Expand Down
16 changes: 8 additions & 8 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,8 +1298,8 @@ def test_ror_operator(self):
self._test_underlying_process_env('_A_', '')
self._test_underlying_process_env(overridden_key, original_value)

def test_refresh(self):
# Test os.environ.refresh()
def test_invalidate_cache(self):
# Test os.environ.invalidate_cache()
has_environb = hasattr(os, 'environb')

# Test with putenv() which doesn't update os.environ
Expand All @@ -1309,7 +1309,7 @@ def test_refresh(self):
if has_environb:
self.assertEqual(os.environb[b'test_env'], b'python_value')

os.environ.refresh()
os.environ.invalidate_cache()
self.assertEqual(os.environ['test_env'], 'new_value')
if has_environb:
self.assertEqual(os.environb[b'test_env'], b'new_value')
Expand All @@ -1320,28 +1320,28 @@ def test_refresh(self):
if has_environb:
self.assertEqual(os.environb[b'test_env'], b'new_value')

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

if has_environb:
# test os.environb.refresh() with putenv()
# test os.environb.invalidate_cache() with putenv()
os.environb[b'test_env'] = b'python_value2'
os.putenv("test_env", "new_value2")
self.assertEqual(os.environb[b'test_env'], b'python_value2')
self.assertEqual(os.environ['test_env'], 'python_value2')

os.environb.refresh()
os.environb.invalidate_cache()
self.assertEqual(os.environb[b'test_env'], b'new_value2')
self.assertEqual(os.environ['test_env'], 'new_value2')

# test os.environb.refresh() with unsetenv()
# test os.environb.invalidate_cache() with unsetenv()
os.unsetenv('test_env')
self.assertEqual(os.environb[b'test_env'], b'new_value2')
self.assertEqual(os.environ['test_env'], 'new_value2')

os.environb.refresh()
os.environb.invalidate_cache()
self.assertNotIn(b'test_env', os.environb)
self.assertNotIn('test_env', os.environ)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Added the :data:`os.environ.refresh() <os.environ>` method to update
Added the :data:`os.environ.invalidate_cache() <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.