blob: 2c9b72d5425343a8b150f364fe853a98843379cd [file] [log] [blame]
Martin v. Löwisef04c442008-03-19 05:04:44 +00001# Copyright 2006 Google, Inc. All Rights Reserved.
2# Licensed to PSF under a Contributor Agreement.
3
4"""Fixer for exec.
5
6This converts usages of the exec statement into calls to a built-in
7exec() function.
8
9exec code in ns1, ns2 -> exec(code, ns1, ns2)
10"""
11
12# Local imports
13from .. import pytree
Benjamin Petersondf6dc8f2008-06-15 02:57:40 +000014from .. import fixer_base
15from ..fixer_util import Comma, Name, Call
Martin v. Löwisef04c442008-03-19 05:04:44 +000016
17
Benjamin Petersondf6dc8f2008-06-15 02:57:40 +000018class FixExec(fixer_base.BaseFix):
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000019 BM_compatible = True
Martin v. Löwisef04c442008-03-19 05:04:44 +000020
21 PATTERN = """
22 exec_stmt< 'exec' a=any 'in' b=any [',' c=any] >
23 |
24 exec_stmt< 'exec' (not atom<'(' [any] ')'>) a=any >
25 """
26
27 def transform(self, node, results):
28 assert results
29 syms = self.syms
30 a = results["a"]
31 b = results.get("b")
32 c = results.get("c")
33 args = [a.clone()]
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000034 args[0].prefix = ""
Martin v. Löwisef04c442008-03-19 05:04:44 +000035 if b is not None:
36 args.extend([Comma(), b.clone()])
37 if c is not None:
38 args.extend([Comma(), c.clone()])
39
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000040 return Call(Name("exec"), args, prefix=node.prefix)