Skip to content

Commit c661b30

Browse files
tirkarthiberkerpeksag
authored andcommitted
bpo-36948: Fix NameError in urllib.request.URLopener.retrieve (GH-13389)
1 parent a5119e7 commit c661b30

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

Lib/test/test_urllib.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ def test_thishost(self):
14451445
self.assertIsInstance(urllib.request.thishost(), tuple)
14461446

14471447

1448-
class URLopener_Tests(unittest.TestCase):
1448+
class URLopener_Tests(FakeHTTPMixin, unittest.TestCase):
14491449
"""Testcase to test the open method of URLopener class."""
14501450

14511451
def test_quoted_open(self):
@@ -1463,6 +1463,24 @@ def open_spam(self, url):
14631463
"spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
14641464
"//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
14651465

1466+
@support.ignore_warnings(category=DeprecationWarning)
1467+
def test_urlopener_retrieve_file(self):
1468+
with support.temp_dir() as tmpdir:
1469+
fd, tmpfile = tempfile.mkstemp(dir=tmpdir)
1470+
os.close(fd)
1471+
fileurl = "file:" + urllib.request.pathname2url(tmpfile)
1472+
filename, _ = urllib.request.URLopener().retrieve(fileurl)
1473+
self.assertEqual(filename, tmpfile)
1474+
1475+
@support.ignore_warnings(category=DeprecationWarning)
1476+
def test_urlopener_retrieve_remote(self):
1477+
url = "http://www.python.org/file.txt"
1478+
self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")
1479+
self.addCleanup(self.unfakehttp)
1480+
filename, _ = urllib.request.URLopener().retrieve(url)
1481+
self.assertEqual(os.path.splitext(filename)[1], ".txt")
1482+
1483+
14661484
# Just commented them out.
14671485
# Can't really tell why keep failing in windows and sparc.
14681486
# Everywhere else they work ok, but on those machines, sometimes

Lib/urllib/request.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ def retrieve(self, url, filename=None, reporthook=None, data=None):
17831783
fp = self.open_local_file(url1)
17841784
hdrs = fp.info()
17851785
fp.close()
1786-
return url2pathname(splithost(url1)[1]), hdrs
1786+
return url2pathname(_splithost(url1)[1]), hdrs
17871787
except OSError as msg:
17881788
pass
17891789
fp = self.open(url, data)
@@ -1792,10 +1792,10 @@ def retrieve(self, url, filename=None, reporthook=None, data=None):
17921792
if filename:
17931793
tfp = open(filename, 'wb')
17941794
else:
1795-
garbage, path = splittype(url)
1796-
garbage, path = splithost(path or "")
1797-
path, garbage = splitquery(path or "")
1798-
path, garbage = splitattr(path or "")
1795+
garbage, path = _splittype(url)
1796+
garbage, path = _splithost(path or "")
1797+
path, garbage = _splitquery(path or "")
1798+
path, garbage = _splitattr(path or "")
17991799
suffix = os.path.splitext(path)[1]
18001800
(fd, filename) = tempfile.mkstemp(suffix)
18011801
self.__tempfiles.append(filename)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :exc:`NameError` in :meth:`urllib.request.URLopener.retrieve`. Patch by
2+
Karthikeyan Singaravelan.

0 commit comments

Comments
 (0)