py3compat.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. from .encoding import DEFAULT_ENCODING
  10. def no_code(x, encoding=None):
  11. return x
  12. def decode(s, encoding=None):
  13. encoding = encoding or DEFAULT_ENCODING
  14. return s.decode(encoding, "replace")
  15. def encode(u, encoding=None):
  16. encoding = encoding or DEFAULT_ENCODING
  17. return u.encode(encoding, "replace")
  18. def cast_unicode(s, encoding=None):
  19. if isinstance(s, bytes):
  20. return decode(s, encoding)
  21. return s
  22. def cast_bytes(s, encoding=None):
  23. if not isinstance(s, bytes):
  24. return encode(s, encoding)
  25. return s
  26. def buffer_to_bytes(buf):
  27. """Cast a buffer or memoryview object to bytes"""
  28. if isinstance(buf, memoryview):
  29. return buf.tobytes()
  30. if not isinstance(buf, bytes):
  31. return bytes(buf)
  32. return buf
  33. def _modify_str_or_docstring(str_change_func):
  34. @functools.wraps(str_change_func)
  35. def wrapper(func_or_str):
  36. if isinstance(func_or_str, string_types):
  37. func = None
  38. doc = func_or_str
  39. else:
  40. func = func_or_str
  41. doc = func.__doc__
  42. doc = str_change_func(doc)
  43. if func:
  44. func.__doc__ = doc
  45. return func
  46. return doc
  47. return wrapper
  48. def safe_unicode(e):
  49. """unicode(e) with various fallbacks. Used for exceptions, which may not be
  50. safe to call unicode() on.
  51. """
  52. try:
  53. return unicode_type(e)
  54. except UnicodeError:
  55. pass
  56. try:
  57. return str_to_unicode(str(e))
  58. except UnicodeError:
  59. pass
  60. try:
  61. return str_to_unicode(repr(e))
  62. except UnicodeError:
  63. pass
  64. return u'Unrecoverably corrupt evalue'
  65. # shutil.which from Python 3.4
  66. def _shutil_which(cmd, mode=os.F_OK | os.X_OK, path=None):
  67. """Given a command, mode, and a PATH string, return the path which
  68. conforms to the given mode on the PATH, or None if there is no such
  69. file.
  70. `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
  71. of os.environ.get("PATH"), or can be overridden with a custom search
  72. path.
  73. This is a backport of shutil.which from Python 3.4
  74. """
  75. # Check that a given file can be accessed with the correct mode.
  76. # Additionally check that `file` is not a directory, as on Windows
  77. # directories pass the os.access check.
  78. def _access_check(fn, mode):
  79. return (os.path.exists(fn) and os.access(fn, mode)
  80. and not os.path.isdir(fn))
  81. # If we're given a path with a directory part, look it up directly rather
  82. # than referring to PATH directories. This includes checking relative to the
  83. # current directory, e.g. ./script
  84. if os.path.dirname(cmd):
  85. if _access_check(cmd, mode):
  86. return cmd
  87. return None
  88. if path is None:
  89. path = os.environ.get("PATH", os.defpath)
  90. if not path:
  91. return None
  92. path = path.split(os.pathsep)
  93. if sys.platform == "win32":
  94. # The current directory takes precedence on Windows.
  95. if not os.curdir in path:
  96. path.insert(0, os.curdir)
  97. # PATHEXT is necessary to check on Windows.
  98. pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
  99. # See if the given file matches any of the expected path extensions.
  100. # This will allow us to short circuit when given "python.exe".
  101. # If it does match, only test that one, otherwise we have to try
  102. # others.
  103. if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
  104. files = [cmd]
  105. else:
  106. files = [cmd + ext for ext in pathext]
  107. else:
  108. # On other platforms you don't have things like PATHEXT to tell you
  109. # what file suffixes are executable, so just pass on cmd as-is.
  110. files = [cmd]
  111. seen = set()
  112. for dir in path:
  113. normdir = os.path.normcase(dir)
  114. if not normdir in seen:
  115. seen.add(normdir)
  116. for thefile in files:
  117. name = os.path.join(dir, thefile)
  118. if _access_check(name, mode):
  119. return name
  120. return None
  121. import platform
  122. if sys.version_info[0] >= 3 or platform.python_implementation() == 'IronPython':
  123. str_to_unicode = no_code
  124. unicode_to_str = no_code
  125. str_to_bytes = encode
  126. bytes_to_str = decode
  127. cast_bytes_py2 = no_code
  128. cast_unicode_py2 = no_code
  129. buffer_to_bytes_py2 = no_code
  130. string_types = (str,)
  131. unicode_type = str
  132. else:
  133. str_to_unicode = decode
  134. unicode_to_str = encode
  135. str_to_bytes = no_code
  136. bytes_to_str = no_code
  137. cast_bytes_py2 = cast_bytes
  138. cast_unicode_py2 = cast_unicode
  139. buffer_to_bytes_py2 = buffer_to_bytes
  140. string_types = (str, unicode)
  141. unicode_type = unicode
  142. if sys.version_info[0] >= 3:
  143. PY3 = True
  144. # keep reference to builtin_mod because the kernel overrides that value
  145. # to forward requests to a frontend.
  146. def input(prompt=''):
  147. return builtin_mod.input(prompt)
  148. builtin_mod_name = "builtins"
  149. import builtins as builtin_mod
  150. which = shutil.which
  151. def isidentifier(s, dotted=False):
  152. if dotted:
  153. return all(isidentifier(a) for a in s.split("."))
  154. return s.isidentifier()
  155. xrange = range
  156. def iteritems(d): return iter(d.items())
  157. def itervalues(d): return iter(d.values())
  158. getcwd = os.getcwd
  159. MethodType = types.MethodType
  160. def execfile(fname, glob, loc=None, compiler=None):
  161. loc = loc if (loc is not None) else glob
  162. with open(fname, 'rb') as f:
  163. compiler = compiler or compile
  164. exec(compiler(f.read(), fname, 'exec'), glob, loc)
  165. # Refactor print statements in doctests.
  166. _print_statement_re = re.compile(r"\bprint (?P<expr>.*)$", re.MULTILINE)
  167. def _print_statement_sub(match):
  168. expr = match.groups('expr')
  169. return "print(%s)" % expr
  170. @_modify_str_or_docstring
  171. def doctest_refactor_print(doc):
  172. """Refactor 'print x' statements in a doctest to print(x) style. 2to3
  173. unfortunately doesn't pick up on our doctests.
  174. Can accept a string or a function, so it can be used as a decorator."""
  175. return _print_statement_re.sub(_print_statement_sub, doc)
  176. # Abstract u'abc' syntax:
  177. @_modify_str_or_docstring
  178. def u_format(s):
  179. """"{u}'abc'" --> "'abc'" (Python 3)
  180. Accepts a string or a function, so it can be used as a decorator."""
  181. return s.format(u='')
  182. def get_closure(f):
  183. """Get a function's closure attribute"""
  184. return f.__closure__
  185. else:
  186. PY3 = False
  187. # keep reference to builtin_mod because the kernel overrides that value
  188. # to forward requests to a frontend.
  189. def input(prompt=''):
  190. return builtin_mod.raw_input(prompt)
  191. builtin_mod_name = "__builtin__"
  192. import __builtin__ as builtin_mod
  193. import re
  194. _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
  195. def isidentifier(s, dotted=False):
  196. if dotted:
  197. return all(isidentifier(a) for a in s.split("."))
  198. return bool(_name_re.match(s))
  199. xrange = xrange
  200. def iteritems(d): return d.iteritems()
  201. def itervalues(d): return d.itervalues()
  202. getcwd = os.getcwdu
  203. def MethodType(func, instance):
  204. return types.MethodType(func, instance, type(instance))
  205. def doctest_refactor_print(func_or_str):
  206. return func_or_str
  207. def get_closure(f):
  208. """Get a function's closure attribute"""
  209. return f.func_closure
  210. which = _shutil_which
  211. # Abstract u'abc' syntax:
  212. @_modify_str_or_docstring
  213. def u_format(s):
  214. """"{u}'abc'" --> "u'abc'" (Python 2)
  215. Accepts a string or a function, so it can be used as a decorator."""
  216. return s.format(u='u')
  217. if sys.platform == 'win32':
  218. def execfile(fname, glob=None, loc=None, compiler=None):
  219. loc = loc if (loc is not None) else glob
  220. scripttext = builtin_mod.open(fname).read()+ '\n'
  221. # compile converts unicode filename to str assuming
  222. # ascii. Let's do the conversion before calling compile
  223. if isinstance(fname, unicode):
  224. filename = unicode_to_str(fname)
  225. else:
  226. filename = fname
  227. compiler = compiler or compile
  228. exec(compiler(scripttext, filename, 'exec'), glob, loc)
  229. else:
  230. def execfile(fname, glob=None, loc=None, compiler=None):
  231. if isinstance(fname, unicode):
  232. filename = fname.encode(sys.getfilesystemencoding())
  233. else:
  234. filename = fname
  235. where = [ns for ns in [glob, loc] if ns is not None]
  236. if compiler is None:
  237. builtin_mod.execfile(filename, *where)
  238. else:
  239. scripttext = builtin_mod.open(fname).read().rstrip() + '\n'
  240. exec(compiler(scripttext, filename, 'exec'), glob, loc)
  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, {})