Skip to content

Commit

Permalink
Revert "pythongh-120057: Add os.environ.refresh() method (python#120059
Browse files Browse the repository at this point in the history
…)"

This reverts commit 7aff2de.

The function is controversial and it was decided to remove it.
  • Loading branch information
vstinner committed Jun 20, 2024
1 parent 55596ae commit d3da724
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 124 deletions.
11 changes: 0 additions & 11 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
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 Down Expand Up @@ -568,8 +561,6 @@ 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 @@ -818,8 +809,6 @@ 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
7 changes: 0 additions & 7 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ ast
Added :func:`ast.compare` for comparing two ASTs.
(Contributed by Batuhan Taskaya and Jeremy Hylton in :issue:`15987`.)

os
--

* 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`.)

pathlib
-------
Expand Down
25 changes: 3 additions & 22 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ def _get_exports_list(module):
from posix import _have_functions
except ImportError:
pass
try:
from posix import _create_environ
except ImportError:
pass

import posix
__all__.extend(_get_exports_list(posix))
Expand All @@ -92,10 +88,6 @@ def _get_exports_list(module):
from nt import _have_functions
except ImportError:
pass
try:
from nt import _create_environ
except ImportError:
pass

else:
raise ImportError('no os specific module found')
Expand Down Expand Up @@ -781,18 +773,7 @@ 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():
def _createenviron():
if name == 'nt':
# Where Env Var Names Must Be UPPERCASE
def check_str(value):
Expand Down Expand Up @@ -822,8 +803,8 @@ def decode(value):
encode, decode)

# unicode environ
environ = _create_environ_mapping()
del _create_environ_mapping
environ = _createenviron()
del _createenviron


def getenv(key, default=None):
Expand Down
46 changes: 0 additions & 46 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,52 +1298,6 @@ 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()
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")
self.assertEqual(os.environ['test_env'], 'python_value')
if has_environb:
self.assertEqual(os.environb[b'test_env'], b'python_value')

os.environ.refresh()
self.assertEqual(os.environ['test_env'], 'new_value')
if has_environb:
self.assertEqual(os.environb[b'test_env'], b'new_value')

# Test with unsetenv() which doesn't update os.environ
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() 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()
self.assertEqual(os.environb[b'test_env'], b'new_value2')
self.assertEqual(os.environ['test_env'], 'new_value2')

# test os.environb.refresh() 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()
self.assertNotIn(b'test_env', os.environb)
self.assertNotIn('test_env', os.environ)

class WalkTests(unittest.TestCase):
"""Tests for os.walk()."""
Expand Down

This file was deleted.

20 changes: 1 addition & 19 deletions Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -16811,20 +16811,6 @@ os__is_inputhook_installed_impl(PyObject *module)
return PyBool_FromLong(PyOS_InputHook != NULL);
}

/*[clinic input]
os._create_environ
Create the environment dictionary.
[clinic start generated code]*/

static PyObject *
os__create_environ_impl(PyObject *module)
/*[clinic end generated code: output=19d9039ab14f8ad4 input=a4c05686b34635e8]*/
{
return convertenviron();
}


static PyMethodDef posix_methods[] = {

OS_STAT_METHODDEF
Expand Down Expand Up @@ -17039,7 +17025,6 @@ static PyMethodDef posix_methods[] = {
OS__SUPPORTS_VIRTUAL_TERMINAL_METHODDEF
OS__INPUTHOOK_METHODDEF
OS__IS_INPUTHOOK_INSTALLED_METHODDEF
OS__CREATE_ENVIRON_METHODDEF
{NULL, NULL} /* Sentinel */
};

Expand Down

0 comments on commit d3da724

Please sign in to comment.