mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-39395: putenv() and unsetenv() always available (GH-18135)
The os.putenv() and os.unsetenv() functions are now always available. On non-Windows platforms, Python now requires setenv() and unsetenv() functions to build. Remove putenv_dict from posixmodule.c: it's not longer needed.
This commit is contained in:
parent
161e7b36b1
commit
b8d1262e8a
12 changed files with 52 additions and 154 deletions
32
Lib/os.py
32
Lib/os.py
|
@ -654,17 +654,15 @@ def get_exec_path(env=None):
|
|||
return path_list.split(pathsep)
|
||||
|
||||
|
||||
# Change environ to automatically call putenv(), unsetenv if they exist.
|
||||
# Change environ to automatically call putenv() and unsetenv()
|
||||
from _collections_abc import MutableMapping
|
||||
|
||||
class _Environ(MutableMapping):
|
||||
def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue, putenv, unsetenv):
|
||||
def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue):
|
||||
self.encodekey = encodekey
|
||||
self.decodekey = decodekey
|
||||
self.encodevalue = encodevalue
|
||||
self.decodevalue = decodevalue
|
||||
self.putenv = putenv
|
||||
self.unsetenv = unsetenv
|
||||
self._data = data
|
||||
|
||||
def __getitem__(self, key):
|
||||
|
@ -678,12 +676,12 @@ class _Environ(MutableMapping):
|
|||
def __setitem__(self, key, value):
|
||||
key = self.encodekey(key)
|
||||
value = self.encodevalue(value)
|
||||
self.putenv(key, value)
|
||||
putenv(key, value)
|
||||
self._data[key] = value
|
||||
|
||||
def __delitem__(self, key):
|
||||
encodedkey = self.encodekey(key)
|
||||
self.unsetenv(encodedkey)
|
||||
unsetenv(encodedkey)
|
||||
try:
|
||||
del self._data[encodedkey]
|
||||
except KeyError:
|
||||
|
@ -712,22 +710,6 @@ class _Environ(MutableMapping):
|
|||
self[key] = value
|
||||
return self[key]
|
||||
|
||||
try:
|
||||
_putenv = putenv
|
||||
except NameError:
|
||||
_putenv = lambda key, value: None
|
||||
else:
|
||||
if "putenv" not in __all__:
|
||||
__all__.append("putenv")
|
||||
|
||||
try:
|
||||
_unsetenv = unsetenv
|
||||
except NameError:
|
||||
_unsetenv = lambda key: _putenv(key, "")
|
||||
else:
|
||||
if "unsetenv" not in __all__:
|
||||
__all__.append("unsetenv")
|
||||
|
||||
def _createenviron():
|
||||
if name == 'nt':
|
||||
# Where Env Var Names Must Be UPPERCASE
|
||||
|
@ -755,8 +737,7 @@ def _createenviron():
|
|||
data = environ
|
||||
return _Environ(data,
|
||||
encodekey, decode,
|
||||
encode, decode,
|
||||
_putenv, _unsetenv)
|
||||
encode, decode)
|
||||
|
||||
# unicode environ
|
||||
environ = _createenviron()
|
||||
|
@ -781,8 +762,7 @@ if supports_bytes_environ:
|
|||
# bytes environ
|
||||
environb = _Environ(environ._data,
|
||||
_check_bytes, bytes,
|
||||
_check_bytes, bytes,
|
||||
_putenv, _unsetenv)
|
||||
_check_bytes, bytes)
|
||||
del _check_bytes
|
||||
|
||||
def getenvb(key, default=None):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue