Make a new urllib package .
It consists of code from urllib, urllib2, urlparse, and robotparser.
The old modules have all been removed. The new package has five
submodules: urllib.parse, urllib.request, urllib.response,
urllib.error, and urllib.robotparser. The urllib.request.urlopen()
function uses the url opener from urllib2.
Note that the unittests have not been renamed for the
beta, but they will be renamed in the future.
Joint work with Senthil Kumaran.
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index 938ab9f..a18a4bb 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -4,10 +4,11 @@
from test import support
from test.test_urllib2 import sanepathname2url
-import socket
-import urllib2
-import sys
import os
+import socket
+import sys
+import urllib.error
+import urllib.request
def _retry_thrice(func, exc, *args, **kwargs):
@@ -28,7 +29,8 @@
# Connecting to remote hosts is flaky. Make it more robust by retrying
# the connection several times.
-_urlopen_with_retry = _wrap_with_retry_thrice(urllib2.urlopen, urllib2.URLError)
+_urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen,
+ urllib.error.URLError)
class AuthTests(unittest.TestCase):
@@ -78,16 +80,11 @@
# calling .close() on urllib2's response objects should close the
# underlying socket
- # delve deep into response to fetch socket._socketobject
response = _urlopen_with_retry("http://www.python.org/")
- abused_fileobject = response.fp
- httpresponse = abused_fileobject.raw
- self.assert_(httpresponse.__class__ is http.client.HTTPResponse)
- fileobject = httpresponse.fp
-
- self.assert_(not fileobject.closed)
+ sock = response.fp
+ self.assert_(not sock.closed)
response.close()
- self.assert_(fileobject.closed)
+ self.assert_(sock.closed)
class OtherNetworkTests(unittest.TestCase):
def setUp(self):
@@ -116,8 +113,9 @@
f.write('hi there\n')
f.close()
urls = [
- 'file:'+sanepathname2url(os.path.abspath(TESTFN)),
- ('file:///nonsensename/etc/passwd', None, urllib2.URLError),
+ 'file:' + sanepathname2url(os.path.abspath(TESTFN)),
+ ('file:///nonsensename/etc/passwd', None,
+ urllib.error.URLError),
]
self._test_urls(urls, self._extra_handlers(), retry=True)
finally:
@@ -157,9 +155,9 @@
import logging
debug = logging.getLogger("test_urllib2").debug
- urlopen = urllib2.build_opener(*handlers).open
+ urlopen = urllib.request.build_opener(*handlers).open
if retry:
- urlopen = _wrap_with_retry_thrice(urlopen, urllib2.URLError)
+ urlopen = _wrap_with_retry_thrice(urlopen, urllib.error.URLError)
for url in urls:
if isinstance(url, tuple):
@@ -186,7 +184,7 @@
def _extra_handlers(self):
handlers = []
- cfh = urllib2.CacheFTPHandler()
+ cfh = urllib.request.CacheFTPHandler()
cfh.setTimeout(1)
handlers.append(cfh)
@@ -197,7 +195,7 @@
def test_http_basic(self):
self.assertTrue(socket.getdefaulttimeout() is None)
u = _urlopen_with_retry("http://www.python.org")
- self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
+ self.assertTrue(u.fp._sock.gettimeout() is None)
def test_http_default_timeout(self):
self.assertTrue(socket.getdefaulttimeout() is None)
@@ -206,7 +204,7 @@
u = _urlopen_with_retry("http://www.python.org")
finally:
socket.setdefaulttimeout(None)
- self.assertEqual(u.fp.raw.fp._sock.gettimeout(), 60)
+ self.assertEqual(u.fp._sock.gettimeout(), 60)
def test_http_no_timeout(self):
self.assertTrue(socket.getdefaulttimeout() is None)
@@ -215,11 +213,11 @@
u = _urlopen_with_retry("http://www.python.org", timeout=None)
finally:
socket.setdefaulttimeout(None)
- self.assertTrue(u.fp.raw.fp._sock.gettimeout() is None)
+ self.assertTrue(u.fp._sock.gettimeout() is None)
def test_http_timeout(self):
u = _urlopen_with_retry("http://www.python.org", timeout=120)
- self.assertEqual(u.fp.raw.fp._sock.gettimeout(), 120)
+ self.assertEqual(u.fp._sock.gettimeout(), 120)
FTP_HOST = "ftp://ftp.mirror.nl/pub/mirror/gnu/"