fix_exec.py 979 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright 2006 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Fixer for exec.
  4. This converts usages of the exec statement into calls to a built-in
  5. exec() function.
  6. exec code in ns1, ns2 -> exec(code, ns1, ns2)
  7. """
  8. # Local imports
  9. from .. import fixer_base
  10. from ..fixer_util import Comma, Name, Call
  11. class FixExec(fixer_base.BaseFix):
  12. BM_compatible = True
  13. PATTERN = """
  14. exec_stmt< 'exec' a=any 'in' b=any [',' c=any] >
  15. |
  16. exec_stmt< 'exec' (not atom<'(' [any] ')'>) a=any >
  17. """
  18. def transform(self, node, results):
  19. assert results
  20. syms = self.syms
  21. a = results["a"]
  22. b = results.get("b")
  23. c = results.get("c")
  24. args = [a.clone()]
  25. args[0].prefix = ""
  26. if b is not None:
  27. args.extend([Comma(), b.clone()])
  28. if c is not None:
  29. args.extend([Comma(), c.clone()])
  30. return Call(Name("exec"), args, prefix=node.prefix)