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 fixer_base
11 from ..fixer_util import (Comma, Name, Call, LParen, RParen, Dot, Node,
12 ArgList, String, syms)
13
14
15 class ESC[4;38;5;81mFixExecfile(ESC[4;38;5;149mfixer_baseESC[4;38;5;149m.ESC[4;38;5;149mBaseFix):
16 BM_compatible = True
17
18 PATTERN = """
19 power< 'execfile' trailer< '(' arglist< filename=any [',' globals=any [',' locals=any ] ] > ')' > >
20 |
21 power< 'execfile' trailer< '(' filename=any ')' > >
22 """
23
24 def transform(self, node, results):
25 assert results
26 filename = results["filename"]
27 globals = results.get("globals")
28 locals = results.get("locals")
29
30 # Copy over the prefix from the right parentheses end of the execfile
31 # call.
32 execfile_paren = node.children[-1].children[-1].clone()
33 # Construct open().read().
34 open_args = ArgList([filename.clone(), Comma(), String('"rb"', ' ')],
35 rparen=execfile_paren)
36 open_call = Node(syms.power, [Name("open"), open_args])
37 read = [Node(syms.trailer, [Dot(), Name('read')]),
38 Node(syms.trailer, [LParen(), RParen()])]
39 open_expr = [open_call] + read
40 # Wrap the open call in a compile call. This is so the filename will be
41 # preserved in the execed code.
42 filename_arg = filename.clone()
43 filename_arg.prefix = " "
44 exec_str = String("'exec'", " ")
45 compile_args = open_expr + [Comma(), filename_arg, Comma(), exec_str]
46 compile_call = Call(Name("compile"), compile_args, "")
47 # Finally, replace the execfile call with an exec call.
48 args = [compile_call]
49 if globals is not None:
50 args.extend([Comma(), globals.clone()])
51 if locals is not None:
52 args.extend([Comma(), locals.clone()])
53 return Call(Name("exec"), args, prefix=node.prefix)