py3compat.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. # coding: utf-8
  2. """Compatibility tricks for Python 3. Mainly to do with unicode."""
  3. import functools
  4. import os
  5. import sys
  6. import re
  7. import shutil
  8. import types
  9. import platform
  10. from .encoding import DEFAULT_ENCODING
  11. def no_code(x, encoding=None):
  12. return x
  13. def decode(s, encoding=None):
  14. encoding = encoding or DEFAULT_ENCODING
  15. return s.decode(encoding, "replace")
  16. def encode(u, encoding=None):
  17. encoding = encoding or DEFAULT_ENCODING
  18. return u.encode(encoding, "replace")
  19. def cast_unicode(s, encoding=None):
  20. if isinstance(s, bytes):
  21. return decode(s, encoding)
  22. return s
  23. def cast_bytes(s, encoding=None):
  24. if not isinstance(s, bytes):
  25. return encode(s, encoding)
  26. return s
  27. def buffer_to_bytes(buf):
  28. """Cast a buffer object to bytes"""
  29. if not isinstance(buf, bytes):
  30. buf = bytes(buf)
  31. return buf
  32. def _modify_str_or_docstring(str_change_func):
  33. @functools.wraps(str_change_func)
  34. def wrapper(func_or_str):
  35. if isinstance(func_or_str, string_types):
  36. func = None
  37. doc = func_or_str
  38. else:
  39. func = func_or_str
  40. doc = func.__doc__
  41. # PYTHONOPTIMIZE=2 strips docstrings, so they can disappear unexpectedly
  42. if doc is not None:
  43. doc = str_change_func(doc)
  44. if func:
  45. func.__doc__ = doc
  46. return func
  47. return doc
  48. return wrapper
  49. def safe_unicode(e):
  50. """unicode(e) with various fallbacks. Used for exceptions, which may not be
  51. safe to call unicode() on.
  52. """
  53. try:
  54. return unicode_type(e)
  55. except UnicodeError:
  56. pass
  57. try:
  58. return str_to_unicode(str(e))
  59. except UnicodeError:
  60. pass
  61. try:
  62. return str_to_unicode(repr(e))
  63. except UnicodeError:
  64. pass
  65. return u'Unrecoverably corrupt evalue'
  66. # shutil.which from Python 3.4
  67. def _shutil_which(cmd, mode=os.F_OK | os.X_OK, path=None):
  68. """Given a command, mode, and a PATH string, return the path which
  69. conforms to the given mode on the PATH, or None if there is no such
  70. file.
  71. `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
  72. of os.environ.get("PATH"), or can be overridden with a custom search
  73. path.
  74. This is a backport of shutil.which from Python 3.4
  75. """
  76. # Check that a given file can be accessed with the correct mode.
  77. # Additionally check that `file` is not a directory, as on Windows
  78. # directories pass the os.access check.
  79. def _access_check(fn, mode):
  80. return (os.path.exists(fn) and os.access(fn, mode)
  81. and not os.path.isdir(fn))
  82. # If we're given a path with a directory part, look it up directly rather
  83. # than referring to PATH directories. This includes checking relative to the
  84. # current directory, e.g. ./script
  85. if os.path.dirname(cmd):
  86. if _access_check(cmd, mode):
  87. return cmd
  88. return None
  89. if path is None:
  90. path = os.environ.get("PATH", os.defpath)
  91. if not path:
  92. return None
  93. path = path.split(os.pathsep)
  94. if sys.platform == "win32":
  95. # The current directory takes precedence on Windows.
  96. if not os.curdir in path:
  97. path.insert(0, os.curdir)
  98. # PATHEXT is necessary to check on Windows.
  99. pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
  100. # See if the given file matches any of the expected path extensions.
  101. # This will allow us to short circuit when given "python.exe".
  102. # If it does match, only test that one, otherwise we have to try
  103. # others.
  104. if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
  105. files = [cmd]
  106. else:
  107. files = [cmd + ext for ext in pathext]
  108. else:
  109. # On other platforms you don't have things like PATHEXT to tell you
  110. # what file suffixes are executable, so just pass on cmd as-is.
  111. files = [cmd]
  112. seen = set()
  113. for dir in path:
  114. normdir = os.path.normcase(dir)
  115. if not normdir in seen:
  116. seen.add(normdir)
  117. for thefile in files:
  118. name = os.path.join(dir, thefile)
  119. if _access_check(name, mode):
  120. return name
  121. return None
  122. if sys.version_info[0] >= 3:
  123. PY3 = True
  124. # keep reference to builtin_mod because the kernel overrides that value
  125. # to forward requests to a frontend.
  126. def input(prompt=''):
  127. return builtin_mod.input(prompt)
  128. builtin_mod_name = "builtins"
  129. import builtins as builtin_mod
  130. str_to_unicode = no_code
  131. unicode_to_str = no_code
  132. str_to_bytes = encode
  133. bytes_to_str = decode
  134. cast_bytes_py2 = no_code
  135. cast_unicode_py2 = no_code
  136. buffer_to_bytes_py2 = no_code
  137. string_types = (str,)
  138. unicode_type = str
  139. which = shutil.which
  140. def isidentifier(s, dotted=False):
  141. if dotted:
  142. return all(isidentifier(a) for a in s.split("."))
  143. return s.isidentifier()
  144. xrange = range
  145. def iteritems(d): return iter(d.items())
  146. def itervalues(d): return iter(d.values())
  147. getcwd = os.getcwd
  148. MethodType = types.MethodType
  149. def execfile(fname, glob, loc=None, compiler=None):
  150. loc = loc if (loc is not None) else glob
  151. with open(fname, 'rb') as f:
  152. compiler = compiler or compile
  153. exec(compiler(f.read(), fname, 'exec'), glob, loc)
  154. # Refactor print statements in doctests.
  155. _print_statement_re = re.compile(r"\bprint (?P<expr>.*)$", re.MULTILINE)
  156. def _print_statement_sub(match):
  157. expr = match.groups('expr')
  158. return "print(%s)" % expr
  159. @_modify_str_or_docstring
  160. def doctest_refactor_print(doc):
  161. """Refactor 'print x' statements in a doctest to print(x) style. 2to3
  162. unfortunately doesn't pick up on our doctests.
  163. Can accept a string or a function, so it can be used as a decorator."""
  164. return _print_statement_re.sub(_print_statement_sub, doc)
  165. # Abstract u'abc' syntax:
  166. @_modify_str_or_docstring
  167. def u_format(s):
  168. """"{u}'abc'" --> "'abc'" (Python 3)
  169. Accepts a string or a function, so it can be used as a decorator."""
  170. return s.format(u='')
  171. def get_closure(f):
  172. """Get a function's closure attribute"""
  173. return f.__closure__
  174. else:
  175. PY3 = False
  176. # keep reference to builtin_mod because the kernel overrides that value
  177. # to forward requests to a frontend.
  178. def input(prompt=''):
  179. return builtin_mod.raw_input(prompt)
  180. builtin_mod_name = "__builtin__"
  181. import __builtin__ as builtin_mod
  182. str_to_unicode = decode
  183. unicode_to_str = encode
  184. str_to_bytes = no_code
  185. bytes_to_str = no_code
  186. cast_bytes_py2 = cast_bytes
  187. cast_unicode_py2 = cast_unicode
  188. buffer_to_bytes_py2 = buffer_to_bytes
  189. string_types = (str, unicode)
  190. unicode_type = unicode
  191. import re
  192. _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
  193. def isidentifier(s, dotted=False):
  194. if dotted:
  195. return all(isidentifier(a) for a in s.split("."))
  196. return bool(_name_re.match(s))
  197. xrange = xrange
  198. def iteritems(d): return d.iteritems()
  199. def itervalues(d): return d.itervalues()
  200. getcwd = os.getcwdu
  201. def MethodType(func, instance):
  202. return types.MethodType(func, instance, type(instance))
  203. def doctest_refactor_print(func_or_str):
  204. return func_or_str
  205. def get_closure(f):
  206. """Get a function's closure attribute"""
  207. return f.func_closure
  208. which = _shutil_which
  209. # Abstract u'abc' syntax:
  210. @_modify_str_or_docstring
  211. def u_format(s):
  212. """"{u}'abc'" --> "u'abc'" (Python 2)
  213. Accepts a string or a function, so it can be used as a decorator."""
  214. return s.format(u='u')
  215. if sys.platform == 'win32':
  216. def execfile(fname, glob=None, loc=None, compiler=None):
  217. loc = loc if (loc is not None) else glob
  218. scripttext = builtin_mod.open(fname).read()+ '\n'
  219. # compile converts unicode filename to str assuming
  220. # ascii. Let's do the conversion before calling compile
  221. if isinstance(fname, unicode):
  222. filename = unicode_to_str(fname)
  223. else:
  224. filename = fname
  225. compiler = compiler or compile
  226. exec(compiler(scripttext, filename, 'exec'), glob, loc)
  227. else:
  228. def execfile(fname, glob=None, loc=None, compiler=None):
  229. if isinstance(fname, unicode):
  230. filename = fname.encode(sys.getfilesystemencoding())
  231. else:
  232. filename = fname
  233. where = [ns for ns in [glob, loc] if ns is not None]
  234. if compiler is None:
  235. builtin_mod.execfile(filename, *where)
  236. else:
  237. scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
  238. exec(compiler(scripttext, filename, 'exec'), glob, loc)
  239. PY2 = not PY3
  240. PYPY = platform.python_implementation() == "PyPy"
  241. def annotate(**kwargs):
  242. """Python 3 compatible function annotation for Python 2."""
  243. if not kwargs:
  244. raise ValueError('annotations must be provided as keyword arguments')
  245. def dec(f):
  246. if hasattr(f, '__annotations__'):
  247. for k, v in kwargs.items():
  248. f.__annotations__[k] = v
  249. else:
  250. f.__annotations__ = kwargs
  251. return f
  252. return dec
  253. # Parts below taken from six:
  254. # Copyright (c) 2010-2013 Benjamin Peterson
  255. #
  256. # Permission is hereby granted, free of charge, to any person obtaining a copy
  257. # of this software and associated documentation files (the "Software"), to deal
  258. # in the Software without restriction, including without limitation the rights
  259. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  260. # copies of the Software, and to permit persons to whom the Software is
  261. # furnished to do so, subject to the following conditions:
  262. #
  263. # The above copyright notice and this permission notice shall be included in all
  264. # copies or substantial portions of the Software.
  265. #
  266. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  267. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  268. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  269. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  270. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  271. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  272. # SOFTWARE.
  273. def with_metaclass(meta, *bases):
  274. """Create a base class with a metaclass."""
  275. return meta("_NewBase", bases, {})