fix_cmp.py 701 B

123456789101112131415161718192021222324252627282930313233
  1. # coding: utf-8
  2. """
  3. Fixer for the cmp() function on Py2, which was removed in Py3.
  4. Adds this import line::
  5. from past.builtins import cmp
  6. if cmp() is called in the code.
  7. """
  8. from __future__ import unicode_literals
  9. from lib2to3 import fixer_base
  10. from libfuturize.fixer_util import touch_import_top
  11. expression = "name='cmp'"
  12. class FixCmp(fixer_base.BaseFix):
  13. BM_compatible = True
  14. run_order = 9
  15. PATTERN = """
  16. power<
  17. ({0}) trailer< '(' args=[any] ')' >
  18. rest=any* >
  19. """.format(expression)
  20. def transform(self, node, results):
  21. name = results["name"]
  22. touch_import_top(u'past.builtins', name.value, node)