|
| 1 | +""" |
| 2 | +To ensure compatibility from Python ``2.6`` - ``3.3``, a module has been |
| 3 | +created. Clearly there is huge need to use conforming syntax. |
| 4 | +""" |
| 5 | +import sys |
| 6 | +import imp |
| 7 | +import os |
| 8 | +import re |
| 9 | +import pkgutil |
| 10 | +try: |
| 11 | + import importlib |
| 12 | +except ImportError: |
| 13 | + pass |
| 14 | + |
| 15 | +is_py3 = sys.version_info[0] >= 3 |
| 16 | +is_py33 = is_py3 and sys.version_info.minor >= 3 |
| 17 | +is_py34 = is_py3 and sys.version_info.minor >= 4 |
| 18 | +is_py35 = is_py3 and sys.version_info.minor >= 5 |
| 19 | +is_py26 = not is_py3 and sys.version_info[1] < 7 |
| 20 | + |
| 21 | + |
| 22 | +class DummyFile(object): |
| 23 | + def __init__(self, loader, string): |
| 24 | + self.loader = loader |
| 25 | + self.string = string |
| 26 | + |
| 27 | + def read(self): |
| 28 | + return self.loader.get_source(self.string) |
| 29 | + |
| 30 | + def close(self): |
| 31 | + del self.loader |
| 32 | + |
| 33 | + |
| 34 | +def find_module_py33(string, path=None): |
| 35 | + loader = importlib.machinery.PathFinder.find_module(string, path) |
| 36 | + |
| 37 | + if loader is None and path is None: # Fallback to find builtins |
| 38 | + try: |
| 39 | + loader = importlib.find_loader(string) |
| 40 | + except ValueError as e: |
| 41 | + # See #491. Importlib might raise a ValueError, to avoid this, we |
| 42 | + # just raise an ImportError to fix the issue. |
| 43 | + raise ImportError("Originally " + repr(e)) |
| 44 | + |
| 45 | + if loader is None: |
| 46 | + raise ImportError("Couldn't find a loader for {0}".format(string)) |
| 47 | + |
| 48 | + try: |
| 49 | + is_package = loader.is_package(string) |
| 50 | + if is_package: |
| 51 | + if hasattr(loader, 'path'): |
| 52 | + module_path = os.path.dirname(loader.path) |
| 53 | + else: |
| 54 | + # At least zipimporter does not have path attribute |
| 55 | + module_path = os.path.dirname(loader.get_filename(string)) |
| 56 | + if hasattr(loader, 'archive'): |
| 57 | + module_file = DummyFile(loader, string) |
| 58 | + else: |
| 59 | + module_file = None |
| 60 | + else: |
| 61 | + module_path = loader.get_filename(string) |
| 62 | + module_file = DummyFile(loader, string) |
| 63 | + except AttributeError: |
| 64 | + # ExtensionLoader has not attribute get_filename, instead it has a |
| 65 | + # path attribute that we can use to retrieve the module path |
| 66 | + try: |
| 67 | + module_path = loader.path |
| 68 | + module_file = DummyFile(loader, string) |
| 69 | + except AttributeError: |
| 70 | + module_path = string |
| 71 | + module_file = None |
| 72 | + finally: |
| 73 | + is_package = False |
| 74 | + |
| 75 | + if hasattr(loader, 'archive'): |
| 76 | + module_path = loader.archive |
| 77 | + |
| 78 | + return module_file, module_path, is_package |
| 79 | + |
| 80 | + |
| 81 | +def find_module_pre_py33(string, path=None): |
| 82 | + try: |
| 83 | + module_file, module_path, description = imp.find_module(string, path) |
| 84 | + module_type = description[2] |
| 85 | + return module_file, module_path, module_type is imp.PKG_DIRECTORY |
| 86 | + except ImportError: |
| 87 | + pass |
| 88 | + |
| 89 | + if path is None: |
| 90 | + path = sys.path |
| 91 | + for item in path: |
| 92 | + loader = pkgutil.get_importer(item) |
| 93 | + if loader: |
| 94 | + try: |
| 95 | + loader = loader.find_module(string) |
| 96 | + if loader: |
| 97 | + is_package = loader.is_package(string) |
| 98 | + is_archive = hasattr(loader, 'archive') |
| 99 | + try: |
| 100 | + module_path = loader.get_filename(string) |
| 101 | + except AttributeError: |
| 102 | + # fallback for py26 |
| 103 | + try: |
| 104 | + module_path = loader._get_filename(string) |
| 105 | + except AttributeError: |
| 106 | + continue |
| 107 | + if is_package: |
| 108 | + module_path = os.path.dirname(module_path) |
| 109 | + if is_archive: |
| 110 | + module_path = loader.archive |
| 111 | + file = None |
| 112 | + if not is_package or is_archive: |
| 113 | + file = DummyFile(loader, string) |
| 114 | + return (file, module_path, is_package) |
| 115 | + except ImportError: |
| 116 | + pass |
| 117 | + raise ImportError("No module named {0}".format(string)) |
| 118 | + |
| 119 | + |
| 120 | +find_module = find_module_py33 if is_py33 else find_module_pre_py33 |
| 121 | +find_module.__doc__ = """ |
| 122 | +Provides information about a module. |
| 123 | +
|
| 124 | +This function isolates the differences in importing libraries introduced with |
| 125 | +python 3.3 on; it gets a module name and optionally a path. It will return a |
| 126 | +tuple containin an open file for the module (if not builtin), the filename |
| 127 | +or the name of the module if it is a builtin one and a boolean indicating |
| 128 | +if the module is contained in a package. |
| 129 | +""" |
| 130 | + |
| 131 | + |
| 132 | +# unicode function |
| 133 | +try: |
| 134 | + unicode = unicode |
| 135 | +except NameError: |
| 136 | + unicode = str |
| 137 | + |
| 138 | +if is_py3: |
| 139 | + u = lambda s: s |
| 140 | +else: |
| 141 | + u = lambda s: s.decode('utf-8') |
| 142 | + |
| 143 | +u.__doc__ = """ |
| 144 | +Decode a raw string into unicode object. Do nothing in Python 3. |
| 145 | +""" |
| 146 | + |
| 147 | +# exec function |
| 148 | +if is_py3: |
| 149 | + def exec_function(source, global_map): |
| 150 | + exec(source, global_map) |
| 151 | +else: |
| 152 | + eval(compile("""def exec_function(source, global_map): |
| 153 | + exec source in global_map """, 'blub', 'exec')) |
| 154 | + |
| 155 | +# re-raise function |
| 156 | +if is_py3: |
| 157 | + def reraise(exception, traceback): |
| 158 | + raise exception.with_traceback(traceback) |
| 159 | +else: |
| 160 | + eval(compile(""" |
| 161 | +def reraise(exception, traceback): |
| 162 | + raise exception, None, traceback |
| 163 | +""", 'blub', 'exec')) |
| 164 | + |
| 165 | +reraise.__doc__ = """ |
| 166 | +Re-raise `exception` with a `traceback` object. |
| 167 | +
|
| 168 | +Usage:: |
| 169 | +
|
| 170 | + reraise(Exception, sys.exc_info()[2]) |
| 171 | +
|
| 172 | +""" |
| 173 | + |
| 174 | +class Python3Method(object): |
| 175 | + def __init__(self, func): |
| 176 | + self.func = func |
| 177 | + |
| 178 | + def __get__(self, obj, objtype): |
| 179 | + if obj is None: |
| 180 | + return lambda *args, **kwargs: self.func(*args, **kwargs) |
| 181 | + else: |
| 182 | + return lambda *args, **kwargs: self.func(obj, *args, **kwargs) |
| 183 | + |
| 184 | + |
| 185 | +def use_metaclass(meta, *bases): |
| 186 | + """ Create a class with a metaclass. """ |
| 187 | + if not bases: |
| 188 | + bases = (object,) |
| 189 | + return meta("HackClass", bases, {}) |
| 190 | + |
| 191 | + |
| 192 | +try: |
| 193 | + encoding = sys.stdout.encoding |
| 194 | + if encoding is None: |
| 195 | + encoding = 'utf-8' |
| 196 | +except AttributeError: |
| 197 | + encoding = 'ascii' |
| 198 | + |
| 199 | + |
| 200 | +def u(string): |
| 201 | + """Cast to unicode DAMMIT! |
| 202 | + Written because Python2 repr always implicitly casts to a string, so we |
| 203 | + have to cast back to a unicode (and we now that we always deal with valid |
| 204 | + unicode, because we check that in the beginning). |
| 205 | + """ |
| 206 | + if is_py3: |
| 207 | + return str(string) |
| 208 | + elif not isinstance(string, unicode): |
| 209 | + return unicode(str(string), 'UTF-8') |
| 210 | + return string |
| 211 | + |
| 212 | +try: |
| 213 | + import builtins # module name in python 3 |
| 214 | +except ImportError: |
| 215 | + import __builtin__ as builtins |
| 216 | + |
| 217 | + |
| 218 | +import ast |
| 219 | + |
| 220 | + |
| 221 | +def literal_eval(string): |
| 222 | + # py3.0, py3.1 and py32 don't support unicode literals. Support those, I |
| 223 | + # don't want to write two versions of the tokenizer. |
| 224 | + if is_py3 and sys.version_info.minor < 3: |
| 225 | + if re.match('[uU][\'"]', string): |
| 226 | + string = string[1:] |
| 227 | + return ast.literal_eval(string) |
| 228 | + |
| 229 | + |
| 230 | +try: |
| 231 | + from itertools import zip_longest |
| 232 | +except ImportError: |
| 233 | + from itertools import izip_longest as zip_longest # Python 2 |
| 234 | + |
| 235 | + |
| 236 | +def no_unicode_pprint(dct): |
| 237 | + """ |
| 238 | + Python 2/3 dict __repr__ may be different, because of unicode differens |
| 239 | + (with or without a `u` prefix). Normally in doctests we could use `pprint` |
| 240 | + to sort dicts and check for equality, but here we have to write a separate |
| 241 | + function to do that. |
| 242 | + """ |
| 243 | + import pprint |
| 244 | + s = pprint.pformat(dct) |
| 245 | + print(re.sub("u'", "'", s)) |
| 246 | + |
| 247 | + |
| 248 | +def utf8_repr(func): |
| 249 | + """ |
| 250 | + ``__repr__`` methods in Python 2 don't allow unicode objects to be |
| 251 | + returned. Therefore cast them to utf-8 bytes in this decorator. |
| 252 | + """ |
| 253 | + def wrapper(self): |
| 254 | + result = func(self) |
| 255 | + if isinstance(result, unicode): |
| 256 | + return result.encode('utf-8') |
| 257 | + else: |
| 258 | + return result |
| 259 | + |
| 260 | + if is_py3: |
| 261 | + return func |
| 262 | + else: |
| 263 | + return wrapper |
0 commit comments