/Lib/lib2to3/fixes/fix_exec.py

http://unladen-swallow.googlecode.com/ · Python · 39 lines · 22 code · 7 blank · 10 comment · 2 complexity · 679db75847dfd56367a8cd2b4286949c MD5 · raw file

  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 pytree
  10. from .. import fixer_base
  11. from ..fixer_util import Comma, Name, Call
  12. class FixExec(fixer_base.BaseFix):
  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].set_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.get_prefix())