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.reload_environ() function #126268

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 11 additions & 9 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ 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
vstinner marked this conversation as resolved.
Show resolved Hide resolved
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
is modified.
Expand Down Expand Up @@ -229,9 +225,6 @@ process and user.
.. versionchanged:: 3.9
Updated to support :pep:`584`'s merge (``|``) and update (``|=``) operators.

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


.. data:: environb

Expand All @@ -249,6 +242,15 @@ process and user.
Updated to support :pep:`584`'s merge (``|``) and update (``|=``) operators.


.. function:: reload_environ()

Update :data:`os.environ` and :data:`os.environb` with changes to the
environment made by :func:`os.putenv`, by :func:`os.unsetenv`, or made
Copy link
Member

Choose a reason for hiding this comment

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

I think that it is better to say "the process environment".
os.putenv and os.unsetenv update the cache, so there is no need to reload after them. You should refer to the C functions.
Please add a note that this function is not thread safe. Calling it while the environment is modified in other thread has undefined behavior. Reading from os.environ or calling os.getenv during reloading can return empty result.

Copy link
Contributor

@rruuaanng rruuaanng Nov 1, 2024

Choose a reason for hiding this comment

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

If the process environment is mentioned, perhaps it can be specifically mentioned that it is the current process environment (I think)

Edit
Or, it can be called the current program.

Copy link
Member Author

Choose a reason for hiding this comment

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

os.putenv() and os.unsetenv() don't update os.environ: see test_reload_environ().

outside Python in the same process.

.. versionadded:: 3.14
vstinner marked this conversation as resolved.
Show resolved Hide resolved


.. function:: chdir(path)
fchdir(fd)
getcwd()
Expand Down Expand Up @@ -568,7 +570,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 :func:`os.reload_environ` function.

.. note::

Expand Down Expand Up @@ -818,7 +820,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 :func:`os.reload_environ` function.

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

Expand Down
7 changes: 4 additions & 3 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,10 @@ operator
os
--

* Add 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.
* Add the :func:`os.reload_environ` function to update :data:`os.environ` and
:data:`os.environb` 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
25 changes: 14 additions & 11 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,17 +765,6 @@ def __ror__(self, other):
new.update(self)
return new

if _exists("_create_environ"):
def refresh(self):
data = _create_environ()
if name == 'nt':
data = {self.encodekey(key): value
for key, value in data.items()}

# modify in-place to keep os.environb in sync
self._data.clear()
self._data.update(data)

def _create_environ_mapping():
if name == 'nt':
# Where Env Var Names Must Be UPPERCASE
Expand Down Expand Up @@ -810,6 +799,20 @@ def decode(value):
del _create_environ_mapping


if _exists("_create_environ"):
def reload_environ():
data = _create_environ()
if name == 'nt':
encodekey = environ.encodekey
data = {encodekey(key): value
for key, value in data.items()}

# modify in-place to keep os.environb in sync
env_data = environ._data
env_data.clear()
env_data.update(data)


def getenv(key, default=None):
"""Get an environment variable, return None if it doesn't exist.
The optional second argument can specify an alternate default.
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_reload_environ(self):
# Test os.reload_environ()
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.reload_environ()
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.reload_environ()
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 reload_environ() on os.environb 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.reload_environ()
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 reload_environ() on os.environb 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.reload_environ()
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
@@ -0,0 +1,2 @@
Replace the ``os.environ.refresh()`` method with a new
:func:`os.reload_environ` function. Patch by Victor Stinner.
Loading