gh-120057: Add os.reload_environ() function (#126268)

Replace the os.environ.refresh() method with a new
os.reload_environ() function.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
Victor Stinner 2024-11-05 08:43:34 +01:00 committed by GitHub
parent d3840503b0
commit 4a0d574273
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 52 additions and 31 deletions

View file

@ -765,17 +765,6 @@ class _Environ(MutableMapping):
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
@ -810,6 +799,20 @@ environ = _create_environ_mapping()
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.