mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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:
parent
d3840503b0
commit
4a0d574273
5 changed files with 52 additions and 31 deletions
25
Lib/os.py
25
Lib/os.py
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue