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