From ed54cfa9768f7fcff299518b9982df30a0aa9cd6 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 30 Jul 2018 12:19:08 -0600 Subject: [PATCH 1/3] bpo-16965: 2to3 now rewrites execfile() to open with 'rb' --- Lib/lib2to3/fixes/fix_execfile.py | 3 ++- Lib/lib2to3/tests/test_fixers.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Lib/lib2to3/fixes/fix_execfile.py b/Lib/lib2to3/fixes/fix_execfile.py index 09cb6f6661ca47..b6c786fd4e8b6a 100644 --- a/Lib/lib2to3/fixes/fix_execfile.py +++ b/Lib/lib2to3/fixes/fix_execfile.py @@ -31,7 +31,8 @@ def transform(self, node, results): # 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("open"), open_args]) read = [Node(syms.trailer, [Dot(), Name('read')]), Node(syms.trailer, [LParen(), RParen()])] diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index 8cecf3cce61b80..3da5dd845c93c6 100644 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -1201,36 +1201,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) From 84b322cd5e911617ed729bc91fbc85aa8376eb05 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Fri, 12 Oct 2018 20:31:13 -0600 Subject: [PATCH 2/3] Add a news entry. --- .../next/Library/2018-10-12-20-30-42.bpo-16965.xo5LAr.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-10-12-20-30-42.bpo-16965.xo5LAr.rst 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 00000000000000..313bde22284c5c --- /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'``. From e5d9005c137797d6e48381f8883d0957852e1893 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 13 Oct 2018 11:53:00 +0300 Subject: [PATCH 3/3] Update 2018-10-12-20-30-42.bpo-16965.xo5LAr.rst --- .../next/Library/2018-10-12-20-30-42.bpo-16965.xo5LAr.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 313bde22284c5c..8e9d2f9482d28c 100644 --- 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 @@ -1,2 +1,2 @@ The :term:`2to3` :2to3fixer:`execfile` fixer now opens the file with mode -``'rb'``. +``'rb'``. Patch by Zackery Spytz.