fix_intern.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright 2006 Georg Brandl.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Fixer for intern().
  4. intern(s) -> sys.intern(s)"""
  5. # Local imports
  6. from .. import fixer_base
  7. from ..fixer_util import ImportAndCall, touch_import
  8. class FixIntern(fixer_base.BaseFix):
  9. BM_compatible = True
  10. order = "pre"
  11. PATTERN = """
  12. power< 'intern'
  13. trailer< lpar='('
  14. ( not(arglist | argument<any '=' any>) obj=any
  15. | obj=arglist<(not argument<any '=' any>) any ','> )
  16. rpar=')' >
  17. after=any*
  18. >
  19. """
  20. def transform(self, node, results):
  21. if results:
  22. # I feel like we should be able to express this logic in the
  23. # PATTERN above but I don't know how to do it so...
  24. obj = results['obj']
  25. if obj:
  26. if (obj.type == self.syms.argument and
  27. obj.children[0].value in {'**', '*'}):
  28. return # Make no change.
  29. names = ('sys', 'intern')
  30. new = ImportAndCall(node, results, names)
  31. touch_import(None, 'sys', node)
  32. return new