From f227b392ca257ae4db73aa43e31987eb86ccd549 Mon Sep 17 00:00:00 2001 From: aldwinaldwin Date: Wed, 14 Aug 2019 15:28:12 +0800 Subject: [PATCH 1/5] bpo-5664: 2to3 convert Cookie.Cookie properly --- Lib/lib2to3/fixes/fix_imports.py | 7 +++++-- Lib/lib2to3/fixes/fix_imports3.py | 16 ++++++++++++++++ Lib/lib2to3/fixes/fix_renames.py | 3 ++- Lib/lib2to3/tests/test_fixers.py | 17 +++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 Lib/lib2to3/fixes/fix_imports3.py diff --git a/Lib/lib2to3/fixes/fix_imports.py b/Lib/lib2to3/fixes/fix_imports.py index aaf4f2f642efb5..f760ad70fdf7e0 100644 --- a/Lib/lib2to3/fixes/fix_imports.py +++ b/Lib/lib2to3/fixes/fix_imports.py @@ -44,7 +44,7 @@ 'httplib': 'http.client', 'htmlentitydefs' : 'html.entities', 'HTMLParser' : 'html.parser', - 'Cookie': 'http.cookies', + #'Cookie': 'http.cookies', is handled by fix_imports3 + renames 'cookielib': 'http.cookiejar', 'BaseHTTPServer': 'http.server', 'SimpleHTTPServer': 'http.server', @@ -139,7 +139,10 @@ def transform(self, node, results): self.transform(node, results) else: # Replace usage of the module. - bare_name = results["bare_with_attr"][0] + if isinstance(results["bare_with_attr"],list): + bare_name = results["bare_with_attr"][0] + else: + bare_name = results["bare_with_attr"] new_name = self.replace.get(bare_name.value) if new_name: bare_name.replace(Name(new_name, prefix=bare_name.prefix)) diff --git a/Lib/lib2to3/fixes/fix_imports3.py b/Lib/lib2to3/fixes/fix_imports3.py new file mode 100644 index 00000000000000..5c74b94d45fd33 --- /dev/null +++ b/Lib/lib2to3/fixes/fix_imports3.py @@ -0,0 +1,16 @@ +"""Fix incompatible imports and module references that get fix_renames after +fix_imports. +""" +from . import fix_imports + + +MAPPING = { + 'Cookie': 'http.cookies', + } + + +class FixImports3(fix_imports.FixImports): + + run_order = 7 + + mapping = MAPPING diff --git a/Lib/lib2to3/fixes/fix_renames.py b/Lib/lib2to3/fixes/fix_renames.py index c0e3705ab7be19..6d67fc30c32bfd 100644 --- a/Lib/lib2to3/fixes/fix_renames.py +++ b/Lib/lib2to3/fixes/fix_renames.py @@ -10,7 +10,8 @@ from .. import fixer_base from ..fixer_util import Name, attr_chain -MAPPING = {"sys": {"maxint" : "maxsize"}, +MAPPING = {"sys": {"maxint": "maxsize"}, + "http.cookies": {"Cookie": "SimpleCookie"}, } LOOKUP = {} diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index 3da5dd845c93c6..c4ffd421839cf1 100644 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -1815,6 +1815,23 @@ class Test_imports2(FixerTestCase, ImportsFixerTests): from ..fixes.fix_imports2 import MAPPING as modules +class Test_imports3(FixerTestCase): + + def setUp(self): + super(Test_imports3, self).setUp(['imports3', 'renames']) + + def test_cookie(self): + b = """ + import Cookie + c = Cookie.Cookie('abc') + """ + a = """ + import http.cookies + c = http.cookies.SimpleCookie('abc') + """ + self.check(b, a) + + class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests): def setUp(self): From ad3c21da7d489caedab678511b960d7c98e02165 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 14 Aug 2019 07:31:51 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst diff --git a/Misc/NEWS.d/next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst b/Misc/NEWS.d/next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst new file mode 100644 index 00000000000000..881de184d7292b --- /dev/null +++ b/Misc/NEWS.d/next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst @@ -0,0 +1 @@ +2to3 convert Cookie.Cookie properly \ No newline at end of file From f994c2b665433ca597010b7abc9664cf91d08bd4 Mon Sep 17 00:00:00 2001 From: aldwinaldwin Date: Wed, 14 Aug 2019 15:41:35 +0800 Subject: [PATCH 3/5] move news to correct directory --- .../next/Tools-Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst | 1 + .../next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Tools-Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst delete mode 100644 Misc/NEWS.d/next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst diff --git a/Misc/NEWS.d/next/Tools-Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst b/Misc/NEWS.d/next/Tools-Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst new file mode 100644 index 00000000000000..e1055a28a09a01 --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst @@ -0,0 +1 @@ +2to3 convert Cookie.Cookie properly. Patch by Aldwin Pollefeyt diff --git a/Misc/NEWS.d/next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst b/Misc/NEWS.d/next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst deleted file mode 100644 index 881de184d7292b..00000000000000 --- a/Misc/NEWS.d/next/Tools/Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst +++ /dev/null @@ -1 +0,0 @@ -2to3 convert Cookie.Cookie properly \ No newline at end of file From 67be3730092306469425f6eee42c177f4d2c9104 Mon Sep 17 00:00:00 2001 From: aldwinaldwin Date: Wed, 14 Aug 2019 15:58:46 +0800 Subject: [PATCH 4/5] moved news to Library --- .../2019-08-14-07-31-50.bpo-5664.UDZMva.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/{Tools-Demos => Library}/2019-08-14-07-31-50.bpo-5664.UDZMva.rst (100%) diff --git a/Misc/NEWS.d/next/Tools-Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst b/Misc/NEWS.d/next/Library/2019-08-14-07-31-50.bpo-5664.UDZMva.rst similarity index 100% rename from Misc/NEWS.d/next/Tools-Demos/2019-08-14-07-31-50.bpo-5664.UDZMva.rst rename to Misc/NEWS.d/next/Library/2019-08-14-07-31-50.bpo-5664.UDZMva.rst From 3f72fa0ebe0d00a13a50f97438a953397f025bf4 Mon Sep 17 00:00:00 2001 From: aldwinaldwin Date: Wed, 14 Aug 2019 16:49:36 +0800 Subject: [PATCH 5/5] add documentation --- Doc/library/2to3.rst | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Doc/library/2to3.rst b/Doc/library/2to3.rst index fa4b0a9645531c..98ea8a6b297f52 100644 --- a/Doc/library/2to3.rst +++ b/Doc/library/2to3.rst @@ -276,6 +276,13 @@ and off individually. They are described here in more detail. Handles other modules renames in the standard library. It is separate from the :2to3fixer:`imports` fixer only because of technical limitations. +.. 2to3fixer:: imports3 + + Handles other modules renames in the standard library. It is separate from + the :2to3fixer:`imports` and :2to3fixer:`imports2` fixers only because of + technical limitations. :2to3fixer:`renames` needs to run afterwards to + rename :data:`http.cookies.Cookie` to :data:`http.cookies.SimpleCookie`. + .. 2to3fixer:: input Converts ``input(prompt)`` to ``eval(input(prompt))``. @@ -389,7 +396,8 @@ and off individually. They are described here in more detail. .. 2to3fixer:: renames - Changes :data:`sys.maxint` to :data:`sys.maxsize`. + Changes :data:`sys.maxint` to :data:`sys.maxsize` and + :data:`http.cookies.Cookie` to :data:`http.cookies.SimpleCookie`. .. 2to3fixer:: repr