[Python-checkins] bpo-36948: Fix NameError in urllib.request.URLopener.retrieve (GH-13389)

Berker Peksag webhook-mailer at python.org
Sun May 19 09:40:11 EDT 2019


https://github.com/python/cpython/commit/c661b30f89ffe7a7995538d3b1649469b184bee4
commit: c661b30f89ffe7a7995538d3b1649469b184bee4
branch: master
author: Xtreak <tir.karthi at gmail.com>
committer: Berker Peksag <berker.peksag at gmail.com>
date: 2019-05-19T16:40:05+03:00
summary:

bpo-36948: Fix NameError in urllib.request.URLopener.retrieve (GH-13389)

files:
A Misc/NEWS.d/next/Library/2019-05-17-21-42-58.bpo-36948.vnUDvk.rst
M Lib/test/test_urllib.py
M Lib/urllib/request.py

diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 7214492eca9d..74b19fbdcd8d 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -1445,7 +1445,7 @@ def test_thishost(self):
         self.assertIsInstance(urllib.request.thishost(), tuple)
 
 
-class URLopener_Tests(unittest.TestCase):
+class URLopener_Tests(FakeHTTPMixin, unittest.TestCase):
     """Testcase to test the open method of URLopener class."""
 
     def test_quoted_open(self):
@@ -1463,6 +1463,24 @@ def open_spam(self, url):
                 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
                 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
 
+    @support.ignore_warnings(category=DeprecationWarning)
+    def test_urlopener_retrieve_file(self):
+        with support.temp_dir() as tmpdir:
+            fd, tmpfile = tempfile.mkstemp(dir=tmpdir)
+            os.close(fd)
+            fileurl = "file:" + urllib.request.pathname2url(tmpfile)
+            filename, _ = urllib.request.URLopener().retrieve(fileurl)
+            self.assertEqual(filename, tmpfile)
+
+    @support.ignore_warnings(category=DeprecationWarning)
+    def test_urlopener_retrieve_remote(self):
+        url = "http://www.python.org/file.txt"
+        self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")
+        self.addCleanup(self.unfakehttp)
+        filename, _ = urllib.request.URLopener().retrieve(url)
+        self.assertEqual(os.path.splitext(filename)[1], ".txt")
+
+
 # Just commented them out.
 # Can't really tell why keep failing in windows and sparc.
 # Everywhere else they work ok, but on those machines, sometimes
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index df2ff06f0fc9..230ac390abb3 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1783,7 +1783,7 @@ def retrieve(self, url, filename=None, reporthook=None, data=None):
                 fp = self.open_local_file(url1)
                 hdrs = fp.info()
                 fp.close()
-                return url2pathname(splithost(url1)[1]), hdrs
+                return url2pathname(_splithost(url1)[1]), hdrs
             except OSError as msg:
                 pass
         fp = self.open(url, data)
@@ -1792,10 +1792,10 @@ def retrieve(self, url, filename=None, reporthook=None, data=None):
             if filename:
                 tfp = open(filename, 'wb')
             else:
-                garbage, path = splittype(url)
-                garbage, path = splithost(path or "")
-                path, garbage = splitquery(path or "")
-                path, garbage = splitattr(path or "")
+                garbage, path = _splittype(url)
+                garbage, path = _splithost(path or "")
+                path, garbage = _splitquery(path or "")
+                path, garbage = _splitattr(path or "")
                 suffix = os.path.splitext(path)[1]
                 (fd, filename) = tempfile.mkstemp(suffix)
                 self.__tempfiles.append(filename)
diff --git a/Misc/NEWS.d/next/Library/2019-05-17-21-42-58.bpo-36948.vnUDvk.rst b/Misc/NEWS.d/next/Library/2019-05-17-21-42-58.bpo-36948.vnUDvk.rst
new file mode 100644
index 000000000000..c8cfa54067fd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-05-17-21-42-58.bpo-36948.vnUDvk.rst
@@ -0,0 +1,2 @@
+Fix :exc:`NameError` in :meth:`urllib.request.URLopener.retrieve`. Patch by
+Karthikeyan Singaravelan.



More information about the Python-checkins mailing list