diff --git a/Lib/os.py b/Lib/os.py index 87547e369db817..c969281abf95f6 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -720,6 +720,11 @@ def get_exec_path(env=None): # Change environ to automatically call putenv() and unsetenv() from _collections_abc import MutableMapping, Mapping +# Sentinel used for seeing if a value is found within the internal _Environ +# dictionary. +_MISSING = sentinel("MISSING") + + class _Environ(MutableMapping): def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue): self.encodekey = encodekey @@ -728,6 +733,9 @@ def __init__(self, data, encodekey, decodekey, encodevalue, decodevalue): self.decodevalue = decodevalue self._data = data + def __contains__(self, key): + return self.encodekey(key) in self._data + def __getitem__(self, key): try: value = self._data[self.encodekey(key)] @@ -770,6 +778,10 @@ def __repr__(self): def copy(self): return dict(self) + def get(self, key, default = None): + val = self._data.get(self.encodekey(key), _MISSING) + return default if val is _MISSING else self.decodevalue(val) + def setdefault(self, key, value): if key not in self: self[key] = value