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 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): diff --git a/Misc/NEWS.d/next/Library/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 new file mode 100644 index 00000000000000..e1055a28a09a01 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-08-14-07-31-50.bpo-5664.UDZMva.rst @@ -0,0 +1 @@ +2to3 convert Cookie.Cookie properly. Patch by Aldwin Pollefeyt