__init__.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. # -*- coding: utf-8 -*-
  2. """
  3. past.translation
  4. ==================
  5. The ``past.translation`` package provides an import hook for Python 3 which
  6. transparently runs ``futurize`` fixers over Python 2 code on import to convert
  7. print statements into functions, etc.
  8. It is intended to assist users in migrating to Python 3.x even if some
  9. dependencies still only support Python 2.x.
  10. Usage
  11. -----
  12. Once your Py2 package is installed in the usual module search path, the import
  13. hook is invoked as follows:
  14. >>> from past.translation import autotranslate
  15. >>> autotranslate('mypackagename')
  16. Or:
  17. >>> autotranslate(['mypackage1', 'mypackage2'])
  18. You can unregister the hook using::
  19. >>> from past.translation import remove_hooks
  20. >>> remove_hooks()
  21. Author: Ed Schofield.
  22. Inspired by and based on ``uprefix`` by Vinay M. Sajip.
  23. """
  24. import imp
  25. import logging
  26. import marshal
  27. import os
  28. import sys
  29. import copy
  30. from lib2to3.pgen2.parse import ParseError
  31. from lib2to3.refactor import RefactoringTool
  32. from libfuturize import fixes
  33. logger = logging.getLogger(__name__)
  34. logger.setLevel(logging.DEBUG)
  35. myfixes = (list(fixes.libfuturize_fix_names_stage1) +
  36. list(fixes.lib2to3_fix_names_stage1) +
  37. list(fixes.libfuturize_fix_names_stage2) +
  38. list(fixes.lib2to3_fix_names_stage2))
  39. # We detect whether the code is Py2 or Py3 by applying certain lib2to3 fixers
  40. # to it. If the diff is empty, it's Python 3 code.
  41. py2_detect_fixers = [
  42. # From stage 1:
  43. 'lib2to3.fixes.fix_apply',
  44. # 'lib2to3.fixes.fix_dict', # TODO: add support for utils.viewitems() etc. and move to stage2
  45. 'lib2to3.fixes.fix_except',
  46. 'lib2to3.fixes.fix_execfile',
  47. 'lib2to3.fixes.fix_exitfunc',
  48. 'lib2to3.fixes.fix_funcattrs',
  49. 'lib2to3.fixes.fix_filter',
  50. 'lib2to3.fixes.fix_has_key',
  51. 'lib2to3.fixes.fix_idioms',
  52. 'lib2to3.fixes.fix_import', # makes any implicit relative imports explicit. (Use with ``from __future__ import absolute_import)
  53. 'lib2to3.fixes.fix_intern',
  54. 'lib2to3.fixes.fix_isinstance',
  55. 'lib2to3.fixes.fix_methodattrs',
  56. 'lib2to3.fixes.fix_ne',
  57. 'lib2to3.fixes.fix_numliterals', # turns 1L into 1, 0755 into 0o755
  58. 'lib2to3.fixes.fix_paren',
  59. 'lib2to3.fixes.fix_print',
  60. 'lib2to3.fixes.fix_raise', # uses incompatible with_traceback() method on exceptions
  61. 'lib2to3.fixes.fix_renames',
  62. 'lib2to3.fixes.fix_reduce',
  63. # 'lib2to3.fixes.fix_set_literal', # this is unnecessary and breaks Py2.6 support
  64. 'lib2to3.fixes.fix_repr',
  65. 'lib2to3.fixes.fix_standarderror',
  66. 'lib2to3.fixes.fix_sys_exc',
  67. 'lib2to3.fixes.fix_throw',
  68. 'lib2to3.fixes.fix_tuple_params',
  69. 'lib2to3.fixes.fix_types',
  70. 'lib2to3.fixes.fix_ws_comma',
  71. 'lib2to3.fixes.fix_xreadlines',
  72. # From stage 2:
  73. 'lib2to3.fixes.fix_basestring',
  74. # 'lib2to3.fixes.fix_buffer', # perhaps not safe. Test this.
  75. # 'lib2to3.fixes.fix_callable', # not needed in Py3.2+
  76. # 'lib2to3.fixes.fix_dict', # TODO: add support for utils.viewitems() etc.
  77. 'lib2to3.fixes.fix_exec',
  78. # 'lib2to3.fixes.fix_future', # we don't want to remove __future__ imports
  79. 'lib2to3.fixes.fix_getcwdu',
  80. # 'lib2to3.fixes.fix_imports', # called by libfuturize.fixes.fix_future_standard_library
  81. # 'lib2to3.fixes.fix_imports2', # we don't handle this yet (dbm)
  82. # 'lib2to3.fixes.fix_input',
  83. # 'lib2to3.fixes.fix_itertools',
  84. # 'lib2to3.fixes.fix_itertools_imports',
  85. 'lib2to3.fixes.fix_long',
  86. # 'lib2to3.fixes.fix_map',
  87. # 'lib2to3.fixes.fix_metaclass', # causes SyntaxError in Py2! Use the one from ``six`` instead
  88. 'lib2to3.fixes.fix_next',
  89. 'lib2to3.fixes.fix_nonzero', # TODO: add a decorator for mapping __bool__ to __nonzero__
  90. # 'lib2to3.fixes.fix_operator', # we will need support for this by e.g. extending the Py2 operator module to provide those functions in Py3
  91. 'lib2to3.fixes.fix_raw_input',
  92. # 'lib2to3.fixes.fix_unicode', # strips off the u'' prefix, which removes a potentially helpful source of information for disambiguating unicode/byte strings
  93. # 'lib2to3.fixes.fix_urllib',
  94. 'lib2to3.fixes.fix_xrange',
  95. # 'lib2to3.fixes.fix_zip',
  96. ]
  97. class RTs:
  98. """
  99. A namespace for the refactoring tools. This avoids creating these at
  100. the module level, which slows down the module import. (See issue #117).
  101. There are two possible grammars: with or without the print statement.
  102. Hence we have two possible refactoring tool implementations.
  103. """
  104. _rt = None
  105. _rtp = None
  106. _rt_py2_detect = None
  107. _rtp_py2_detect = None
  108. @staticmethod
  109. def setup():
  110. """
  111. Call this before using the refactoring tools to create them on demand
  112. if needed.
  113. """
  114. if None in [RTs._rt, RTs._rtp]:
  115. RTs._rt = RefactoringTool(myfixes)
  116. RTs._rtp = RefactoringTool(myfixes, {'print_function': True})
  117. @staticmethod
  118. def setup_detect_python2():
  119. """
  120. Call this before using the refactoring tools to create them on demand
  121. if needed.
  122. """
  123. if None in [RTs._rt_py2_detect, RTs._rtp_py2_detect]:
  124. RTs._rt_py2_detect = RefactoringTool(py2_detect_fixers)
  125. RTs._rtp_py2_detect = RefactoringTool(py2_detect_fixers,
  126. {'print_function': True})
  127. # We need to find a prefix for the standard library, as we don't want to
  128. # process any files there (they will already be Python 3).
  129. #
  130. # The following method is used by Sanjay Vinip in uprefix. This fails for
  131. # ``conda`` environments:
  132. # # In a non-pythonv virtualenv, sys.real_prefix points to the installed Python.
  133. # # In a pythonv venv, sys.base_prefix points to the installed Python.
  134. # # Outside a virtual environment, sys.prefix points to the installed Python.
  135. # if hasattr(sys, 'real_prefix'):
  136. # _syslibprefix = sys.real_prefix
  137. # else:
  138. # _syslibprefix = getattr(sys, 'base_prefix', sys.prefix)
  139. # Instead, we use the portion of the path common to both the stdlib modules
  140. # ``math`` and ``urllib``.
  141. def splitall(path):
  142. """
  143. Split a path into all components. From Python Cookbook.
  144. """
  145. allparts = []
  146. while True:
  147. parts = os.path.split(path)
  148. if parts[0] == path: # sentinel for absolute paths
  149. allparts.insert(0, parts[0])
  150. break
  151. elif parts[1] == path: # sentinel for relative paths
  152. allparts.insert(0, parts[1])
  153. break
  154. else:
  155. path = parts[0]
  156. allparts.insert(0, parts[1])
  157. return allparts
  158. def common_substring(s1, s2):
  159. """
  160. Returns the longest common substring to the two strings, starting from the
  161. left.
  162. """
  163. chunks = []
  164. path1 = splitall(s1)
  165. path2 = splitall(s2)
  166. for (dir1, dir2) in zip(path1, path2):
  167. if dir1 != dir2:
  168. break
  169. chunks.append(dir1)
  170. return os.path.join(*chunks)
  171. # _stdlibprefix = common_substring(math.__file__, urllib.__file__)
  172. def detect_python2(source, pathname):
  173. """
  174. Returns a bool indicating whether we think the code is Py2
  175. """
  176. RTs.setup_detect_python2()
  177. try:
  178. tree = RTs._rt_py2_detect.refactor_string(source, pathname)
  179. except ParseError as e:
  180. if e.msg != 'bad input' or e.value != '=':
  181. raise
  182. tree = RTs._rtp.refactor_string(source, pathname)
  183. if source != str(tree)[:-1]: # remove added newline
  184. # The above fixers made changes, so we conclude it's Python 2 code
  185. logger.debug('Detected Python 2 code: {0}'.format(pathname))
  186. return True
  187. else:
  188. logger.debug('Detected Python 3 code: {0}'.format(pathname))
  189. return False
  190. class Py2Fixer(object):
  191. """
  192. An import hook class that uses lib2to3 for source-to-source translation of
  193. Py2 code to Py3.
  194. """
  195. # See the comments on :class:future.standard_library.RenameImport.
  196. # We add this attribute here so remove_hooks() and install_hooks() can
  197. # unambiguously detect whether the import hook is installed:
  198. PY2FIXER = True
  199. def __init__(self):
  200. self.found = None
  201. self.base_exclude_paths = ['future', 'past']
  202. self.exclude_paths = copy.copy(self.base_exclude_paths)
  203. self.include_paths = []
  204. def include(self, paths):
  205. """
  206. Pass in a sequence of module names such as 'plotrique.plotting' that,
  207. if present at the leftmost side of the full package name, would
  208. specify the module to be transformed from Py2 to Py3.
  209. """
  210. self.include_paths += paths
  211. def exclude(self, paths):
  212. """
  213. Pass in a sequence of strings such as 'mymodule' that, if
  214. present at the leftmost side of the full package name, would cause
  215. the module not to undergo any source transformation.
  216. """
  217. self.exclude_paths += paths
  218. def find_module(self, fullname, path=None):
  219. logger.debug('Running find_module: {0}...'.format(fullname))
  220. if '.' in fullname:
  221. parent, child = fullname.rsplit('.', 1)
  222. if path is None:
  223. loader = self.find_module(parent, path)
  224. mod = loader.load_module(parent)
  225. path = mod.__path__
  226. fullname = child
  227. # Perhaps we should try using the new importlib functionality in Python
  228. # 3.3: something like this?
  229. # thing = importlib.machinery.PathFinder.find_module(fullname, path)
  230. try:
  231. self.found = imp.find_module(fullname, path)
  232. except Exception as e:
  233. logger.debug('Py2Fixer could not find {0}')
  234. logger.debug('Exception was: {0})'.format(fullname, e))
  235. return None
  236. self.kind = self.found[-1][-1]
  237. if self.kind == imp.PKG_DIRECTORY:
  238. self.pathname = os.path.join(self.found[1], '__init__.py')
  239. elif self.kind == imp.PY_SOURCE:
  240. self.pathname = self.found[1]
  241. return self
  242. def transform(self, source):
  243. # This implementation uses lib2to3,
  244. # you can override and use something else
  245. # if that's better for you
  246. # lib2to3 likes a newline at the end
  247. RTs.setup()
  248. source += '\n'
  249. try:
  250. tree = RTs._rt.refactor_string(source, self.pathname)
  251. except ParseError as e:
  252. if e.msg != 'bad input' or e.value != '=':
  253. raise
  254. tree = RTs._rtp.refactor_string(source, self.pathname)
  255. # could optimise a bit for only doing str(tree) if
  256. # getattr(tree, 'was_changed', False) returns True
  257. return str(tree)[:-1] # remove added newline
  258. def load_module(self, fullname):
  259. logger.debug('Running load_module for {0}...'.format(fullname))
  260. if fullname in sys.modules:
  261. mod = sys.modules[fullname]
  262. else:
  263. if self.kind in (imp.PY_COMPILED, imp.C_EXTENSION, imp.C_BUILTIN,
  264. imp.PY_FROZEN):
  265. convert = False
  266. # elif (self.pathname.startswith(_stdlibprefix)
  267. # and 'site-packages' not in self.pathname):
  268. # # We assume it's a stdlib package in this case. Is this too brittle?
  269. # # Please file a bug report at https://github.com/PythonCharmers/python-future
  270. # # if so.
  271. # convert = False
  272. # in theory, other paths could be configured to be excluded here too
  273. elif any([fullname.startswith(path) for path in self.exclude_paths]):
  274. convert = False
  275. elif any([fullname.startswith(path) for path in self.include_paths]):
  276. convert = True
  277. else:
  278. convert = False
  279. if not convert:
  280. logger.debug('Excluded {0} from translation'.format(fullname))
  281. mod = imp.load_module(fullname, *self.found)
  282. else:
  283. logger.debug('Autoconverting {0} ...'.format(fullname))
  284. mod = imp.new_module(fullname)
  285. sys.modules[fullname] = mod
  286. # required by PEP 302
  287. mod.__file__ = self.pathname
  288. mod.__name__ = fullname
  289. mod.__loader__ = self
  290. # This:
  291. # mod.__package__ = '.'.join(fullname.split('.')[:-1])
  292. # seems to result in "SystemError: Parent module '' not loaded,
  293. # cannot perform relative import" for a package's __init__.py
  294. # file. We use the approach below. Another option to try is the
  295. # minimal load_module pattern from the PEP 302 text instead.
  296. # Is the test in the next line more or less robust than the
  297. # following one? Presumably less ...
  298. # ispkg = self.pathname.endswith('__init__.py')
  299. if self.kind == imp.PKG_DIRECTORY:
  300. mod.__path__ = [ os.path.dirname(self.pathname) ]
  301. mod.__package__ = fullname
  302. else:
  303. #else, regular module
  304. mod.__path__ = []
  305. mod.__package__ = fullname.rpartition('.')[0]
  306. try:
  307. cachename = imp.cache_from_source(self.pathname)
  308. if not os.path.exists(cachename):
  309. update_cache = True
  310. else:
  311. sourcetime = os.stat(self.pathname).st_mtime
  312. cachetime = os.stat(cachename).st_mtime
  313. update_cache = cachetime < sourcetime
  314. # # Force update_cache to work around a problem with it being treated as Py3 code???
  315. # update_cache = True
  316. if not update_cache:
  317. with open(cachename, 'rb') as f:
  318. data = f.read()
  319. try:
  320. code = marshal.loads(data)
  321. except Exception:
  322. # pyc could be corrupt. Regenerate it
  323. update_cache = True
  324. if update_cache:
  325. if self.found[0]:
  326. source = self.found[0].read()
  327. elif self.kind == imp.PKG_DIRECTORY:
  328. with open(self.pathname) as f:
  329. source = f.read()
  330. if detect_python2(source, self.pathname):
  331. source = self.transform(source)
  332. code = compile(source, self.pathname, 'exec')
  333. dirname = os.path.dirname(cachename)
  334. try:
  335. if not os.path.exists(dirname):
  336. os.makedirs(dirname)
  337. with open(cachename, 'wb') as f:
  338. data = marshal.dumps(code)
  339. f.write(data)
  340. except Exception: # could be write-protected
  341. pass
  342. exec(code, mod.__dict__)
  343. except Exception as e:
  344. # must remove module from sys.modules
  345. del sys.modules[fullname]
  346. raise # keep it simple
  347. if self.found[0]:
  348. self.found[0].close()
  349. return mod
  350. _hook = Py2Fixer()
  351. def install_hooks(include_paths=(), exclude_paths=()):
  352. if isinstance(include_paths, str):
  353. include_paths = (include_paths,)
  354. if isinstance(exclude_paths, str):
  355. exclude_paths = (exclude_paths,)
  356. assert len(include_paths) + len(exclude_paths) > 0, 'Pass at least one argument'
  357. _hook.include(include_paths)
  358. _hook.exclude(exclude_paths)
  359. # _hook.debug = debug
  360. enable = sys.version_info[0] >= 3 # enabled for all 3.x+
  361. if enable and _hook not in sys.meta_path:
  362. sys.meta_path.insert(0, _hook) # insert at beginning. This could be made a parameter
  363. # We could return the hook when there are ways of configuring it
  364. #return _hook
  365. def remove_hooks():
  366. if _hook in sys.meta_path:
  367. sys.meta_path.remove(_hook)
  368. def detect_hooks():
  369. """
  370. Returns True if the import hooks are installed, False if not.
  371. """
  372. return _hook in sys.meta_path
  373. # present = any([hasattr(hook, 'PY2FIXER') for hook in sys.meta_path])
  374. # return present
  375. class hooks(object):
  376. """
  377. Acts as a context manager. Use like this:
  378. >>> from past import translation
  379. >>> with translation.hooks():
  380. ... import mypy2module
  381. >>> import requests # py2/3 compatible anyway
  382. >>> # etc.
  383. """
  384. def __enter__(self):
  385. self.hooks_were_installed = detect_hooks()
  386. install_hooks()
  387. return self
  388. def __exit__(self, *args):
  389. if not self.hooks_were_installed:
  390. remove_hooks()
  391. class suspend_hooks(object):
  392. """
  393. Acts as a context manager. Use like this:
  394. >>> from past import translation
  395. >>> translation.install_hooks()
  396. >>> import http.client
  397. >>> # ...
  398. >>> with translation.suspend_hooks():
  399. >>> import requests # or others that support Py2/3
  400. If the hooks were disabled before the context, they are not installed when
  401. the context is left.
  402. """
  403. def __enter__(self):
  404. self.hooks_were_installed = detect_hooks()
  405. remove_hooks()
  406. return self
  407. def __exit__(self, *args):
  408. if self.hooks_were_installed:
  409. install_hooks()
  410. # alias
  411. autotranslate = install_hooks