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:
Victor Stinner 2020-01-24 14:05:48 +01:00 committed by GitHub
parent 161e7b36b1
commit b8d1262e8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 52 additions and 154 deletions

View file

@ -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):