@@ -20,7 +20,12 @@ def find_module_py33(string, path=None):
2020 loader = importlib .machinery .PathFinder .find_module (string , path )
2121
2222 if loader is None and path is None : # Fallback to find builtins
23- loader = importlib .find_loader (string )
23+ try :
24+ loader = importlib .find_loader (string )
25+ except ValueError as e :
26+ # See #491. Importlib might raise a ValueError, to avoid this, we
27+ # just raise an ImportError to fix the issue.
28+ raise ImportError ("Originally ValueError: " + e .message )
2429
2530 if loader is None :
2631 raise ImportError ("Couldn't find a loader for {0}" .format (string ))
@@ -65,23 +70,6 @@ def find_module_pre_py33(string, path=None):
6570if the module is contained in a package.
6671"""
6772
68- # next was defined in python 2.6, in python 3 obj.next won't be possible
69- # anymore
70- try :
71- next = next
72- except NameError :
73- _raiseStopIteration = object ()
74-
75- def next (iterator , default = _raiseStopIteration ):
76- if not hasattr (iterator , 'next' ):
77- raise TypeError ("not an iterator" )
78- try :
79- return iterator .next ()
80- except StopIteration :
81- if default is _raiseStopIteration :
82- raise
83- else :
84- return default
8573
8674# unicode function
8775try :
@@ -125,18 +113,6 @@ def reraise(exception, traceback):
125113
126114"""
127115
128- # hasattr function used because python
129- if is_py3 :
130- hasattr = hasattr
131- else :
132- def hasattr (obj , name ):
133- try :
134- getattr (obj , name )
135- return True
136- except AttributeError :
137- return False
138-
139-
140116class Python3Method (object ):
141117 def __init__ (self , func ):
142118 self .func = func
@@ -197,3 +173,33 @@ def literal_eval(string):
197173 from itertools import zip_longest
198174except ImportError :
199175 from itertools import izip_longest as zip_longest # Python 2
176+
177+
178+ def no_unicode_pprint (dct ):
179+ """
180+ Python 2/3 dict __repr__ may be different, because of unicode differens
181+ (with or without a `u` prefix). Normally in doctests we could use `pprint`
182+ to sort dicts and check for equality, but here we have to write a separate
183+ function to do that.
184+ """
185+ import pprint
186+ s = pprint .pformat (dct )
187+ print (re .sub ("u'" , "'" , s ))
188+
189+
190+ def utf8_repr (func ):
191+ """
192+ ``__repr__`` methods in Python 2 don't allow unicode objects to be
193+ returned. Therefore cast them to utf-8 bytes in this decorator.
194+ """
195+ def wrapper (self ):
196+ result = func (self )
197+ if isinstance (result , unicode ):
198+ return result .encode ('utf-8' )
199+ else :
200+ return result
201+
202+ if is_py3 :
203+ return func
204+ else :
205+ return wrapper
0 commit comments