PageRenderTime 242ms CodeModel.GetById 100ms app.highlight 4ms RepoModel.GetById 137ms app.codeStats 0ms

/Lib/lib2to3/fixes/fix_exec.py

http://unladen-swallow.googlecode.com/
Python | 39 lines | 24 code | 5 blank | 10 comment | 0 complexity | 679db75847dfd56367a8cd2b4286949c MD5 | raw file
 1# 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
14from .. import fixer_base
15from ..fixer_util import Comma, Name, Call
16
17
18class FixExec(fixer_base.BaseFix):
19
20    PATTERN = """
21    exec_stmt< 'exec' a=any 'in' b=any [',' c=any] >
22    |
23    exec_stmt< 'exec' (not atom<'(' [any] ')'>) a=any >
24    """
25
26    def transform(self, node, results):
27        assert results
28        syms = self.syms
29        a = results["a"]
30        b = results.get("b")
31        c = results.get("c")
32        args = [a.clone()]
33        args[0].set_prefix("")
34        if b is not None:
35            args.extend([Comma(), b.clone()])
36        if c is not None:
37            args.extend([Comma(), c.clone()])
38
39        return Call(Name("exec"), args, prefix=node.get_prefix())