[Python-checkins] r67900 - in sandbox/trunk/2to3/lib2to3: fixes/fix_execfile.py tests/test_fixers.py
benjamin.peterson
python-checkins at python.org
Mon Dec 22 21:02:46 CET 2008
Author: benjamin.peterson
Date: Mon Dec 22 21:02:45 2008
New Revision: 67900
Log:
fix_execfile: wrap the open(fn).read() call in compile(), so the filename is preserved
also add unittests for the fixer
Modified:
sandbox/trunk/2to3/lib2to3/fixes/fix_execfile.py
sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_execfile.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_execfile.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_execfile.py Mon Dec 22 21:02:45 2008
@@ -9,7 +9,8 @@
from .. import pytree
from .. import fixer_base
-from ..fixer_util import Comma, Name, Call, LParen, RParen, Dot
+from ..fixer_util import (Comma, Name, Call, LParen, RParen, Dot, Node,
+ ArgList, String, syms)
class FixExecfile(fixer_base.BaseFix):
@@ -22,16 +23,30 @@
def transform(self, node, results):
assert results
- syms = self.syms
filename = results["filename"]
globals = results.get("globals")
locals = results.get("locals")
- args = [Name('open'), LParen(), filename.clone(), RParen(), Dot(),
- Name('read'), LParen(), RParen()]
- args[0].set_prefix("")
+
+ # Copy over the prefix from the right parentheses end of the execfile
+ # call.
+ execfile_paren = node.children[-1].children[-1].clone()
+ # Construct open().read().
+ open_args = ArgList([filename.clone()], rparen=execfile_paren)
+ open_call = Node(syms.power, [Name("open"), open_args])
+ read = [Node(syms.trailer, [Dot(), Name('read')]),
+ Node(syms.trailer, [LParen(), RParen()])]
+ open_expr = [open_call] + read
+ # Wrap the open call in a compile call. This is so the filename will be
+ # preserved in the execed code.
+ filename_arg = filename.clone()
+ filename_arg.set_prefix(" ")
+ exec_str = String("'exec'", " ")
+ compile_args = open_expr + [Comma(), filename_arg, Comma(), exec_str]
+ compile_call = Call(Name("compile"), compile_args, "")
+ # Finally, replace the execfile call with an exec call.
+ args = [compile_call]
if globals is not None:
args.extend([Comma(), globals.clone()])
if locals is not None:
args.extend([Comma(), locals.clone()])
-
return Call(Name("exec"), args, prefix=node.get_prefix())
Modified: sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_fixers.py Mon Dec 22 21:02:45 2008
@@ -1076,6 +1076,45 @@
a = """x = int( x )"""
self.check(b, a)
+
+class Test_execfile(FixerTestCase):
+ fixer = "execfile"
+
+ def test_conversion(self):
+ b = """execfile("fn")"""
+ a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
+ self.check(b, a)
+
+ b = """execfile("fn", glob)"""
+ a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
+ self.check(b, a)
+
+ b = """execfile("fn", glob, loc)"""
+ a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
+ self.check(b, a)
+
+ b = """execfile("fn", globals=glob)"""
+ a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
+ self.check(b, a)
+
+ b = """execfile("fn", locals=loc)"""
+ a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
+ self.check(b, a)
+
+ b = """execfile("fn", globals=glob, locals=loc)"""
+ a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
+ self.check(b, a)
+
+ def test_spacing(self):
+ b = """execfile( "fn" )"""
+ a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
+ self.check(b, a)
+
+ b = """execfile("fn", globals = glob)"""
+ a = """exec(compile(open("fn").read(), "fn", 'exec'), globals = glob)"""
+ self.check(b, a)
+
+
class Test_isinstance(FixerTestCase):
fixer = "isinstance"
More information about the Python-checkins
mailing list