comparison tests/run-tests.py @ 39714:491fc3f4be67

py3: make osenvironb a proxy for, instead of a copy of os.environ where needed Without this, TESTDIR and a few other variables weren't defined in the *.t test. I didn't bother implementing all of the view functions for simplicity. All that is actually used is __{get,set}item__(), get() and pop(), but the rest seems easy enough to add for futureproofing.
author Matt Harbison <matt_harbison@yahoo.com>
date Tue, 18 Sep 2018 22:14:03 -0400
parents 030d558c6456
children 7f8b7a060584
comparison
equal deleted inserted replaced
39713:a5dafefc4a53 39714:491fc3f4be67
154 return p 154 return p
155 return p.decode('utf-8') 155 return p.decode('utf-8')
156 156
157 osenvironb = getattr(os, 'environb', None) 157 osenvironb = getattr(os, 'environb', None)
158 if osenvironb is None: 158 if osenvironb is None:
159 # Windows lacks os.environb, for instance. 159 # Windows lacks os.environb, for instance. A proxy over the real thing
160 osenvironb = { 160 # instead of a copy allows the environment to be updated via bytes on
161 _bytespath(k): _bytespath(v) for k, v in os.environ.items() 161 # all platforms.
162 } 162 class environbytes(object):
163 def __init__(self, strenv):
164 self.__len__ = strenv.__len__
165 self.clear = strenv.clear
166 self._strenv = strenv
167 def __getitem__(self, k):
168 v = self._strenv.__getitem__(_strpath(k))
169 return _bytespath(v)
170 def __setitem__(self, k, v):
171 self._strenv.__setitem__(_strpath(k), _strpath(v))
172 def __delitem__(self, k):
173 self._strenv.__delitem__(_strpath(k))
174 def __contains__(self, k):
175 return self._strenv.__contains__(_strpath(k))
176 def __iter__(self):
177 return iter([_bytespath(k) for k in iter(self._strenv)])
178 def get(self, k, default=None):
179 v = self._strenv.get(_strpath(k), _strpath(default))
180 return _bytespath(v)
181 def pop(self, k, default=None):
182 v = self._strenv.pop(_strpath(k), _strpath(default))
183 return _bytespath(v)
184
185 osenvironb = environbytes(os.environ)
163 186
164 elif sys.version_info >= (3, 0, 0): 187 elif sys.version_info >= (3, 0, 0):
165 print('%s is only supported on Python 3.5+ and 2.7, not %s' % 188 print('%s is only supported on Python 3.5+ and 2.7, not %s' %
166 (sys.argv[0], '.'.join(str(v) for v in sys.version_info[:3]))) 189 (sys.argv[0], '.'.join(str(v) for v in sys.version_info[:3])))
167 sys.exit(70) # EX_SOFTWARE from `man 3 sysexit` 190 sys.exit(70) # EX_SOFTWARE from `man 3 sysexit`