summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZackery Spytz <zspytz@gmail.com>2018-10-16 05:46:35 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2018-10-16 05:46:35 (GMT)
commitf5e00f490ab5abfcf5e38e58bf969c7b5dcb4818 (patch)
tree1c689f6bfd7ed11b6a38fa68a667dbf1d9d60428
parentaadb44ee98bc73bc5132acea5848ac6aef1ff8c0 (diff)
downloadcpython-f5e00f490ab5abfcf5e38e58bf969c7b5dcb4818.zip
cpython-f5e00f490ab5abfcf5e38e58bf969c7b5dcb4818.tar.gz
cpython-f5e00f490ab5abfcf5e38e58bf969c7b5dcb4818.tar.bz2
[2.7] bpo-16965: 2to3 now rewrites execfile() to open with rb. (GH-8569) (GH-9890)
(cherry picked from commit d4d60134b29290049e28df54f23493de4f1824b6)
-rw-r--r--Lib/lib2to3/fixes/fix_execfile.py3
-rw-r--r--Lib/lib2to3/tests/test_fixers.py16
-rw-r--r--Misc/NEWS.d/next/Library/2018-10-12-20-30-42.bpo-16965.xo5LAr.rst2
3 files changed, 12 insertions, 9 deletions
diff --git a/Lib/lib2to3/fixes/fix_execfile.py b/Lib/lib2to3/fixes/fix_execfile.py
index 2f29d3b..786268b 100644
--- a/Lib/lib2to3/fixes/fix_execfile.py
+++ b/Lib/lib2to3/fixes/fix_execfile.py
@@ -31,7 +31,8 @@ class FixExecfile(fixer_base.BaseFix):
# call.
execfile_paren = node.children[-1].children[-1].clone()
# Construct open().read().
- open_args = ArgList([filename.clone()], rparen=execfile_paren)
+ open_args = ArgList([filename.clone(), Comma(), String('"rb"', ' ')],
+ 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()])]
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
index b0e60fe..c7d5ff9 100644
--- a/Lib/lib2to3/tests/test_fixers.py
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -1143,36 +1143,36 @@ class Test_execfile(FixerTestCase):
def test_conversion(self):
b = """execfile("fn")"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
+ a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'))"""
self.check(b, a)
b = """execfile("fn", glob)"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
+ a = """exec(compile(open("fn", "rb").read(), "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", "rb").read(), "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", "rb").read(), "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", "rb").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)"""
+ a = """exec(compile(open("fn", "rb").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'))"""
+ a = """exec(compile(open( "fn", "rb" ).read(), "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", "rb").read(), "fn", 'exec'), globals = glob)"""
self.check(b, a)
diff --git a/Misc/NEWS.d/next/Library/2018-10-12-20-30-42.bpo-16965.xo5LAr.rst b/Misc/NEWS.d/next/Library/2018-10-12-20-30-42.bpo-16965.xo5LAr.rst
new file mode 100644
index 0000000..8e9d2f9
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-10-12-20-30-42.bpo-16965.xo5LAr.rst
@@ -0,0 +1,2 @@
+The :term:`2to3` :2to3fixer:`execfile` fixer now opens the file with mode
+``'rb'``. Patch by Zackery Spytz.