| 1 |
|
| 2 |
|
| 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 FixExecfile(fixer_base.BaseFix): |
| 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 |
|
| 31 |
|
| 32 |
execfile_paren = node.children[-1].children[-1].clone() |
| 33 |
|
| 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 |
|
| 41 |
|
| 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 |
|
| 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) |