diff 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
line wrap: on
line diff
--- a/compat.py	Mon Jan 03 01:07:02 2011 +0200
+++ b/compat.py	Mon Jan 03 01:27:56 2011 +0200
@@ -43,9 +43,9 @@
 except ImportError:
 	import __builtin__ as builtins
 
-__all__ = ('say', 'basestring', 'range', 'map', 'zip', 'filter', 'items',
-           'keys', 'values', 'zip_longest', 'callable',
-           'ABCMeta', 'abstractmethod', 'CompatBuiltins')
+pseudobuiltins = ('say', 'basestring', 'range', 'map', 'zip', 'filter', 'next',
+                  'items', 'keys', 'values', 'zip_longest', 'callable')
+__all__ = pseudobuiltins + ('ABCMeta', 'abstractmethod', 'CompatBuiltins')
 
 try:
 	# Python 3
@@ -135,7 +135,7 @@
 	try:
 		# Python 3
 		import urllib.request
-		return urllib.request, lambda url: urllib.request.urlopen(url).read().decode()
+		return urllib.request, lambda url: urllib.request.urlopen(url).read().decode('ascii')
 	except ImportError:
 		# Python 2
 		import urllib
@@ -146,19 +146,16 @@
 except ImportError:
 	ABCMeta, abstractmethod = None, lambda x: x
 
-# In all of the following, the try clause is for Python 2 and the except
-# clause is for Python 3. More checks are performed than needed
-# for standard builds of Python to ensure as much as possible works
-# on custom builds.
 try:
 	basestring = basestring
 except NameError:
 	basestring = str
 
+# xrange is set to support simple testconf.py's written for test.py 1.x
 try:
-	range = xrange
+	xrange = range = xrange
 except NameError:
-	range = range
+	xrange = range = range
 
 try:
 	callable = callable
@@ -186,9 +183,20 @@
 except ImportError:
 	filter = filter
 
-items = dict.iteritems if hasattr(dict, 'iteritems') else dict.items
-keys = dict.iterkeys if hasattr(dict, 'iterkeys') else dict.keys
-values = dict.itervalues if hasattr(dict, 'itervalues') else dict.values
+try:
+	items = dict.iteritems
+except AttributeError:
+	items = dict.items
+
+try:
+	keys = dict.iterkeys
+except AttributeError:
+	keys = dict.keys
+
+try:
+	values = dict.itervalues
+except AttributeError:
+	values = dict.values
 
 try:
 	# Python 3
@@ -216,17 +224,16 @@
 # Automatically import * from this module into testconf.py's
 class CompatBuiltins(object):
 	__slots__ = 'originals'
-	def __init__(self):
-		self.originals = {}
+	globals = globals()
 	def __enter__(self):
-		g = globals()
-		for name in __all__:
-			if hasattr(builtins, name):
+		self.originals = {}
+		for name in pseudobuiltins:
+			try:
 				self.originals[name] = getattr(builtins, name)
-			setattr(builtins, name, g[name])
+			except AttributeError:
+				pass
+			setattr(builtins, name, self.globals[name])
+		return self
 	def __exit__(self, exc_type, exc_val, exc_tb):
 		for name in self.originals:
-			setattr(builtins, name, self.originals[name])
-
-# Support simple testconf.py's written for test.py 1.x
-builtins.xrange = range
\ No newline at end of file
+			setattr(builtins, name, self.originals[name])
\ No newline at end of file