Skip to content

Commit f5e00f4

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
[2.7] bpo-16965: 2to3 now rewrites execfile() to open with rb. (GH-8569) (GH-9890)
(cherry picked from commit d4d6013)
1 parent aadb44e commit f5e00f4

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(u"open"), open_args])
3637
read = [Node(syms.trailer, [Dot(), Name(u'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
@@ -1143,36 +1143,36 @@ class Test_execfile(FixerTestCase):
11431143

11441144
def test_conversion(self):
11451145
b = """execfile("fn")"""
1146-
a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
1146+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'))"""
11471147
self.check(b, a)
11481148

11491149
b = """execfile("fn", glob)"""
1150-
a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
1150+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), glob)"""
11511151
self.check(b, a)
11521152

11531153
b = """execfile("fn", glob, loc)"""
1154-
a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
1154+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), glob, loc)"""
11551155
self.check(b, a)
11561156

11571157
b = """execfile("fn", globals=glob)"""
1158-
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
1158+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals=glob)"""
11591159
self.check(b, a)
11601160

11611161
b = """execfile("fn", locals=loc)"""
1162-
a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
1162+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), locals=loc)"""
11631163
self.check(b, a)
11641164

11651165
b = """execfile("fn", globals=glob, locals=loc)"""
1166-
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
1166+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals=glob, locals=loc)"""
11671167
self.check(b, a)
11681168

11691169
def test_spacing(self):
11701170
b = """execfile( "fn" )"""
1171-
a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
1171+
a = """exec(compile(open( "fn", "rb" ).read(), "fn", 'exec'))"""
11721172
self.check(b, a)
11731173

11741174
b = """execfile("fn", globals = glob)"""
1175-
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals = glob)"""
1175+
a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals = glob)"""
11761176
self.check(b, a)
11771177

11781178

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)