|
1 # Copyright 2006 Google, Inc. All Rights Reserved. |
|
2 # Licensed to PSF under a Contributor Agreement. |
|
3 |
|
4 """Fixer for execfile. |
|
5 |
|
6 This converts usages of the execfile function into calls to the built-in |
|
7 exec() function. |
|
8 """ |
|
9 |
|
10 from .. import pytree |
|
11 from .. import fixer_base |
|
12 from ..fixer_util import Comma, Name, Call, LParen, RParen, Dot |
|
13 |
|
14 |
|
15 class FixExecfile(fixer_base.BaseFix): |
|
16 |
|
17 PATTERN = """ |
|
18 power< 'execfile' trailer< '(' arglist< filename=any [',' globals=any [',' locals=any ] ] > ')' > > |
|
19 | |
|
20 power< 'execfile' trailer< '(' filename=any ')' > > |
|
21 """ |
|
22 |
|
23 def transform(self, node, results): |
|
24 assert results |
|
25 syms = self.syms |
|
26 filename = results["filename"] |
|
27 globals = results.get("globals") |
|
28 locals = results.get("locals") |
|
29 args = [Name('open'), LParen(), filename.clone(), RParen(), Dot(), |
|
30 Name('read'), LParen(), RParen()] |
|
31 args[0].set_prefix("") |
|
32 if globals is not None: |
|
33 args.extend([Comma(), globals.clone()]) |
|
34 if locals is not None: |
|
35 args.extend([Comma(), locals.clone()]) |
|
36 |
|
37 return Call(Name("exec"), args, prefix=node.get_prefix()) |