comparison compat.py @ 70:b9d5857f7b9a

Better emulation of built-ins for testconf Bug fix: next is now in compat.__all__. Members of compat that do not correspond to built-ins or members of itertools are no longer added to built-ins while loading testconf. xrange is now added to built-ins only while loading testconf and only in legacy mode. testconf can now access the original built-ins through the CompatBuiltins context manager which is now exposed as config.builtins. Bug fix: the default string encoding is no longer relied upon.
author Oleg Oshmyan <chortos@inbox.lv>
date Mon, 03 Jan 2011 01:27:56 +0200
parents e00ab6d1f0ba
children d46bd7ee3e69
comparison
equal deleted inserted replaced
69:c0f1b87013ad 70:b9d5857f7b9a
41 try: 41 try:
42 import builtins 42 import builtins
43 except ImportError: 43 except ImportError:
44 import __builtin__ as builtins 44 import __builtin__ as builtins
45 45
46 __all__ = ('say', 'basestring', 'range', 'map', 'zip', 'filter', 'items', 46 pseudobuiltins = ('say', 'basestring', 'range', 'map', 'zip', 'filter', 'next',
47 'keys', 'values', 'zip_longest', 'callable', 47 'items', 'keys', 'values', 'zip_longest', 'callable')
48 'ABCMeta', 'abstractmethod', 'CompatBuiltins') 48 __all__ = pseudobuiltins + ('ABCMeta', 'abstractmethod', 'CompatBuiltins')
49 49
50 try: 50 try:
51 # Python 3 51 # Python 3
52 exec('say = print') 52 exec('say = print')
53 except SyntaxError: 53 except SyntaxError:
133 133
134 def import_urllib(): 134 def import_urllib():
135 try: 135 try:
136 # Python 3 136 # Python 3
137 import urllib.request 137 import urllib.request
138 return urllib.request, lambda url: urllib.request.urlopen(url).read().decode() 138 return urllib.request, lambda url: urllib.request.urlopen(url).read().decode('ascii')
139 except ImportError: 139 except ImportError:
140 # Python 2 140 # Python 2
141 import urllib 141 import urllib
142 return urllib, lambda url: urllib.urlopen(url).read() 142 return urllib, lambda url: urllib.urlopen(url).read()
143 143
144 try: 144 try:
145 from abc import ABCMeta, abstractmethod 145 from abc import ABCMeta, abstractmethod
146 except ImportError: 146 except ImportError:
147 ABCMeta, abstractmethod = None, lambda x: x 147 ABCMeta, abstractmethod = None, lambda x: x
148 148
149 # In all of the following, the try clause is for Python 2 and the except
150 # clause is for Python 3. More checks are performed than needed
151 # for standard builds of Python to ensure as much as possible works
152 # on custom builds.
153 try: 149 try:
154 basestring = basestring 150 basestring = basestring
155 except NameError: 151 except NameError:
156 basestring = str 152 basestring = str
157 153
158 try: 154 # xrange is set to support simple testconf.py's written for test.py 1.x
159 range = xrange 155 try:
160 except NameError: 156 xrange = range = xrange
161 range = range 157 except NameError:
158 xrange = range = range
162 159
163 try: 160 try:
164 callable = callable 161 callable = callable
165 except NameError: 162 except NameError:
166 from collections import Callable 163 from collections import Callable
184 try: 181 try:
185 from itertools import ifilter as filter 182 from itertools import ifilter as filter
186 except ImportError: 183 except ImportError:
187 filter = filter 184 filter = filter
188 185
189 items = dict.iteritems if hasattr(dict, 'iteritems') else dict.items 186 try:
190 keys = dict.iterkeys if hasattr(dict, 'iterkeys') else dict.keys 187 items = dict.iteritems
191 values = dict.itervalues if hasattr(dict, 'itervalues') else dict.values 188 except AttributeError:
189 items = dict.items
190
191 try:
192 keys = dict.iterkeys
193 except AttributeError:
194 keys = dict.keys
195
196 try:
197 values = dict.itervalues
198 except AttributeError:
199 values = dict.values
192 200
193 try: 201 try:
194 # Python 3 202 # Python 3
195 from itertools import zip_longest 203 from itertools import zip_longest
196 except ImportError: 204 except ImportError:
214 pass 222 pass
215 223
216 # Automatically import * from this module into testconf.py's 224 # Automatically import * from this module into testconf.py's
217 class CompatBuiltins(object): 225 class CompatBuiltins(object):
218 __slots__ = 'originals' 226 __slots__ = 'originals'
219 def __init__(self): 227 globals = globals()
228 def __enter__(self):
220 self.originals = {} 229 self.originals = {}
221 def __enter__(self): 230 for name in pseudobuiltins:
222 g = globals() 231 try:
223 for name in __all__:
224 if hasattr(builtins, name):
225 self.originals[name] = getattr(builtins, name) 232 self.originals[name] = getattr(builtins, name)
226 setattr(builtins, name, g[name]) 233 except AttributeError:
234 pass
235 setattr(builtins, name, self.globals[name])
236 return self
227 def __exit__(self, exc_type, exc_val, exc_tb): 237 def __exit__(self, exc_type, exc_val, exc_tb):
228 for name in self.originals: 238 for name in self.originals:
229 setattr(builtins, name, self.originals[name]) 239 setattr(builtins, name, self.originals[name])
230
231 # Support simple testconf.py's written for test.py 1.x
232 builtins.xrange = range