fix_execfile.py 921 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # coding: utf-8
  2. """
  3. Fixer for the execfile() function on Py2, which was removed in Py3.
  4. The Lib/lib2to3/fixes/fix_execfile.py module has some problems: see
  5. python-future issue #37. This fixer merely imports execfile() from
  6. past.builtins and leaves the code alone.
  7. Adds this import line::
  8. from past.builtins import execfile
  9. for the function execfile() that was removed from Py3.
  10. """
  11. from __future__ import unicode_literals
  12. from lib2to3 import fixer_base
  13. from libfuturize.fixer_util import touch_import_top
  14. expression = "name='execfile'"
  15. class FixExecfile(fixer_base.BaseFix):
  16. BM_compatible = True
  17. run_order = 9
  18. PATTERN = """
  19. power<
  20. ({0}) trailer< '(' args=[any] ')' >
  21. rest=any* >
  22. """.format(expression)
  23. def transform(self, node, results):
  24. name = results["name"]
  25. touch_import_top(u'past.builtins', name.value, node)