Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3da724

Browse files
committed
Revert "pythongh-120057: Add os.environ.refresh() method (python#120059)"
This reverts commit 7aff2de. The function is controversial and it was decided to remove it.
1 parent 55596ae commit d3da724

7 files changed

Lines changed: 4 additions & 124 deletions

File tree

Doc/library/os.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ process and user.
193193
to the environment made after this time are not reflected in :data:`os.environ`,
194194
except for changes made by modifying :data:`os.environ` directly.
195195

196-
The :meth:`!os.environ.refresh()` method updates :data:`os.environ` with
197-
changes to the environment made by :func:`os.putenv`, by
198-
:func:`os.unsetenv`, or made outside Python in the same process.
199-
200196
This mapping may be used to modify the environment as well as query the
201197
environment. :func:`putenv` will be called automatically when the mapping
202198
is modified.
@@ -229,9 +225,6 @@ process and user.
229225
.. versionchanged:: 3.9
230226
Updated to support :pep:`584`'s merge (``|``) and update (``|=``) operators.
231227

232-
.. versionchanged:: 3.14
233-
Added the :meth:`!os.environ.refresh()` method.
234-
235228

236229
.. data:: environb
237230

@@ -568,8 +561,6 @@ process and user.
568561
of :data:`os.environ`. This also applies to :func:`getenv` and :func:`getenvb`, which
569562
respectively use :data:`os.environ` and :data:`os.environb` in their implementations.
570563

571-
See also the :data:`os.environ.refresh() <os.environ>` method.
572-
573564
.. note::
574565

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

821-
See also the :data:`os.environ.refresh() <os.environ>` method.
822-
823812
.. audit-event:: os.unsetenv key os.unsetenv
824813

825814
.. versionchanged:: 3.9

Doc/whatsnew/3.14.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ ast
9292
Added :func:`ast.compare` for comparing two ASTs.
9393
(Contributed by Batuhan Taskaya and Jeremy Hylton in :issue:`15987`.)
9494

95-
os
96-
--
97-
98-
* Added the :data:`os.environ.refresh() <os.environ>` method to update
99-
:data:`os.environ` with changes to the environment made by :func:`os.putenv`,
100-
by :func:`os.unsetenv`, or made outside Python in the same process.
101-
(Contributed by Victor Stinner in :gh:`120057`.)
10295

10396
pathlib
10497
-------

Lib/os.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ def _get_exports_list(module):
6464
from posix import _have_functions
6565
except ImportError:
6666
pass
67-
try:
68-
from posix import _create_environ
69-
except ImportError:
70-
pass
7167

7268
import posix
7369
__all__.extend(_get_exports_list(posix))
@@ -92,10 +88,6 @@ def _get_exports_list(module):
9288
from nt import _have_functions
9389
except ImportError:
9490
pass
95-
try:
96-
from nt import _create_environ
97-
except ImportError:
98-
pass
9991

10092
else:
10193
raise ImportError('no os specific module found')
@@ -781,18 +773,7 @@ def __ror__(self, other):
781773
new.update(self)
782774
return new
783775

784-
if _exists("_create_environ"):
785-
def refresh(self):
786-
data = _create_environ()
787-
if name == 'nt':
788-
data = {self.encodekey(key): value
789-
for key, value in data.items()}
790-
791-
# modify in-place to keep os.environb in sync
792-
self._data.clear()
793-
self._data.update(data)
794-
795-
def _create_environ_mapping():
776+
def _createenviron():
796777
if name == 'nt':
797778
# Where Env Var Names Must Be UPPERCASE
798779
def check_str(value):
@@ -822,8 +803,8 @@ def decode(value):
822803
encode, decode)
823804

824805
# unicode environ
825-
environ = _create_environ_mapping()
826-
del _create_environ_mapping
806+
environ = _createenviron()
807+
del _createenviron
827808

828809

829810
def getenv(key, default=None):

Lib/test/test_os.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,52 +1298,6 @@ def test_ror_operator(self):
12981298
self._test_underlying_process_env('_A_', '')
12991299
self._test_underlying_process_env(overridden_key, original_value)
13001300

1301-
def test_refresh(self):
1302-
# Test os.environ.refresh()
1303-
has_environb = hasattr(os, 'environb')
1304-
1305-
# Test with putenv() which doesn't update os.environ
1306-
os.environ['test_env'] = 'python_value'
1307-
os.putenv("test_env", "new_value")
1308-
self.assertEqual(os.environ['test_env'], 'python_value')
1309-
if has_environb:
1310-
self.assertEqual(os.environb[b'test_env'], b'python_value')
1311-
1312-
os.environ.refresh()
1313-
self.assertEqual(os.environ['test_env'], 'new_value')
1314-
if has_environb:
1315-
self.assertEqual(os.environb[b'test_env'], b'new_value')
1316-
1317-
# Test with unsetenv() which doesn't update os.environ
1318-
os.unsetenv('test_env')
1319-
self.assertEqual(os.environ['test_env'], 'new_value')
1320-
if has_environb:
1321-
self.assertEqual(os.environb[b'test_env'], b'new_value')
1322-
1323-
os.environ.refresh()
1324-
self.assertNotIn('test_env', os.environ)
1325-
if has_environb:
1326-
self.assertNotIn(b'test_env', os.environb)
1327-
1328-
if has_environb:
1329-
# test os.environb.refresh() with putenv()
1330-
os.environb[b'test_env'] = b'python_value2'
1331-
os.putenv("test_env", "new_value2")
1332-
self.assertEqual(os.environb[b'test_env'], b'python_value2')
1333-
self.assertEqual(os.environ['test_env'], 'python_value2')
1334-
1335-
os.environb.refresh()
1336-
self.assertEqual(os.environb[b'test_env'], b'new_value2')
1337-
self.assertEqual(os.environ['test_env'], 'new_value2')
1338-
1339-
# test os.environb.refresh() with unsetenv()
1340-
os.unsetenv('test_env')
1341-
self.assertEqual(os.environb[b'test_env'], b'new_value2')
1342-
self.assertEqual(os.environ['test_env'], 'new_value2')
1343-
1344-
os.environb.refresh()
1345-
self.assertNotIn(b'test_env', os.environb)
1346-
self.assertNotIn('test_env', os.environ)
13471301

13481302
class WalkTests(unittest.TestCase):
13491303
"""Tests for os.walk()."""

Misc/NEWS.d/next/Library/2024-06-04-18-53-10.gh-issue-120057.RSD9_Z.rst

Lines changed: 0 additions & 4 deletions
This file was deleted.

Modules/clinic/posixmodule.c.h

Lines changed: 1 addition & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16811,20 +16811,6 @@ os__is_inputhook_installed_impl(PyObject *module)
1681116811
return PyBool_FromLong(PyOS_InputHook != NULL);
1681216812
}
1681316813

16814-
/*[clinic input]
16815-
os._create_environ
16816-
16817-
Create the environment dictionary.
16818-
[clinic start generated code]*/
16819-
16820-
static PyObject *
16821-
os__create_environ_impl(PyObject *module)
16822-
/*[clinic end generated code: output=19d9039ab14f8ad4 input=a4c05686b34635e8]*/
16823-
{
16824-
return convertenviron();
16825-
}
16826-
16827-
1682816814
static PyMethodDef posix_methods[] = {
1682916815

1683016816
OS_STAT_METHODDEF
@@ -17039,7 +17025,6 @@ static PyMethodDef posix_methods[] = {
1703917025
OS__SUPPORTS_VIRTUAL_TERMINAL_METHODDEF
1704017026
OS__INPUTHOOK_METHODDEF
1704117027
OS__IS_INPUTHOOK_INSTALLED_METHODDEF
17042-
OS__CREATE_ENVIRON_METHODDEF
1704317028
{NULL, NULL} /* Sentinel */
1704417029
};
1704517030

0 commit comments

Comments
 (0)