Index: Lib/lib2to3/tests/test_fixers.py =================================================================== --- Lib/lib2to3/tests/test_fixers.py (revision 76143) +++ Lib/lib2to3/tests/test_fixers.py (working copy) @@ -1134,36 +1134,36 @@ def test_conversion(self): b = """execfile("fn")""" - a = """exec(compile(open("fn").read(), "fn", 'exec'))""" + a = """exec(compile(open("fn").read()+'\n', "fn", 'exec'))""" self.check(b, a) b = """execfile("fn", glob)""" - a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)""" + a = """exec(compile(open("fn").read()+'\n', "fn", 'exec'), glob)""" self.check(b, a) b = """execfile("fn", glob, loc)""" - a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)""" + a = """exec(compile(open("fn").read()+'\n', "fn", 'exec'), glob, loc)""" self.check(b, a) b = """execfile("fn", globals=glob)""" - a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)""" + a = """exec(compile(open("fn").read()+'\n', "fn", 'exec'), globals=glob)""" self.check(b, a) b = """execfile("fn", locals=loc)""" - a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)""" + a = """exec(compile(open("fn").read()+'\n', "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)""" + a = """exec(compile(open("fn").read()+'\n', "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'))""" + a = """exec(compile(open( "fn" ).read()+'\n', "fn", 'exec'))""" self.check(b, a) b = """execfile("fn", globals = glob)""" - a = """exec(compile(open("fn").read(), "fn", 'exec'), globals = glob)""" + a = """exec(compile(open("fn").read()+'\n', "fn", 'exec'), globals = glob)""" self.check(b, a) Index: Lib/lib2to3/fixes/fix_execfile.py =================================================================== --- Lib/lib2to3/fixes/fix_execfile.py (revision 76143) +++ Lib/lib2to3/fixes/fix_execfile.py (working copy) @@ -8,8 +8,8 @@ """ from .. import fixer_base -from ..fixer_util import (Comma, Name, Call, LParen, RParen, Dot, Node, - ArgList, String, syms) +from ..fixer_util import (Comma, Name, Call, LParen, RParen, Dot, Node, Leaf, + ArgList, String, syms, token) class FixExecfile(fixer_base.BaseFix): @@ -29,18 +29,22 @@ # Copy over the prefix from the right parentheses end of the execfile # call. execfile_paren = node.children[-1].children[-1].clone() - # Construct open().read(). + # Construct open().read() + '\n'. open_args = ArgList([filename.clone()], rparen=execfile_paren) open_call = Node(syms.power, [Name(u"open"), open_args]) read = [Node(syms.trailer, [Dot(), Name(u'read')]), Node(syms.trailer, [LParen(), RParen()])] open_expr = [open_call] + read + open_plus_newline = [Node(syms.arith_expr, + [Node(syms.power, open_expr), + Leaf(token.PLUS, u"+"), + String(repr("\n"))])] # 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.prefix = u" " exec_str = String(u"'exec'", u" ") - compile_args = open_expr + [Comma(), filename_arg, Comma(), exec_str] + compile_args = open_plus_newline + [Comma(), filename_arg, Comma(), exec_str] compile_call = Call(Name(u"compile"), compile_args, u"") # Finally, replace the execfile call with an exec call. args = [compile_call]