Skip to content

Commit d4d6013

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
bpo-16965: 2to3 now rewrites execfile() to open with 'rb'. (pythonGH-8569)
1 parent 0461704 commit d4d6013

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

Lib/lib2to3/fixes/fix_execfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def transform(self, node, results):
3131
# call.
3232
execfile_paren = node.children[-1].children[-1].clone()
3333
# Construct open().read().
34-
open_args = ArgList([filename.clone()], rparen=execfile_paren)
34+
open_args = ArgList([filename.clone(), Comma(), String('"rb"', ' ')],
35+
rparen=execfile_paren)
3536
open_call = Node(syms.power, [Name("open"), open_args])
3637
read = [Node(syms.trailer, [Dot(), Name('read')]),
3738
Node(syms.trailer, [LParen(), RParen()])]

Lib/lib2to3/tests/test_fixers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,36 +1201,36 @@ class Test_execfile(FixerTestCase):
12011201

12021202
def test_conversion(self):
12031203
b = """execfile("fn")"""
1204-
a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
1204+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'))"""
12051205
self.check(b, a)
12061206

12071207
b = """execfile("fn", glob)"""
1208-
a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
1208+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), glob)"""
12091209
self.check(b, a)
12101210

12111211
b = """execfile("fn", glob, loc)"""
1212-
a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
1212+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), glob, loc)"""
12131213
self.check(b, a)
12141214

12151215
b = """execfile("fn", globals=glob)"""
1216-
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
1216+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals=glob)"""
12171217
self.check(b, a)
12181218

12191219
b = """execfile("fn", locals=loc)"""
1220-
a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
1220+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), locals=loc)"""
12211221
self.check(b, a)
12221222

12231223
b = """execfile("fn", globals=glob, locals=loc)"""
1224-
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
1224+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals=glob, locals=loc)"""
12251225
self.check(b, a)
12261226

12271227
def test_spacing(self):
12281228
b = """execfile( "fn" )"""
1229-
a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
1229+
a = """exec(compile(open( "fn", "rb" ).read(), "fn", 'exec'))"""
12301230
self.check(b, a)
12311231

12321232
b = """execfile("fn", globals = glob)"""
1233-
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals = glob)"""
1233+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals = glob)"""
12341234
self.check(b, a)
12351235

12361236

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :term:`2to3` :2to3fixer:`execfile` fixer now opens the file with mode
2+
``'rb'``. Patch by Zackery Spytz.

0 commit comments

Comments
 (0)