cpython: eab274c7d456 (original) (raw)
Mercurial > cpython
changeset 75640:eab274c7d456
deprecated the old urllib primitives in 3.3 urllib package - issue 10050 [#10050]
Senthil Kumaran senthil@uthcode.com | |
---|---|
date | Wed, 14 Mar 2012 13:43:53 -0700 |
parents | 6700fd345835 |
children | f1829281fdc8 30f13d7fecd0 |
files | Doc/library/urllib.request.rst Lib/test/test_urllib.py Lib/test/test_urllib2.py Lib/urllib/request.py |
diffstat | 4 files changed, 112 insertions(+), 51 deletions(-)[+] [-] Doc/library/urllib.request.rst 92 Lib/test/test_urllib.py 4 Lib/test/test_urllib2.py 23 Lib/urllib/request.py 44 |
line wrap: on
line diff
--- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -385,12 +385,6 @@ request. .. versionadded:: 3.3 -.. method:: Request.add_data(data) -
- Set the :class:
Request
data to data. This is ignored by all handlers except - HTTP handlers --- and there it should be a byte string, and will change the
- request to be
POST
rather thanGET
. -
.. method:: Request.get_method()
@@ -403,16 +397,6 @@ request.
get_method now looks at the value of :attr:Request.method
.
-.. method:: Request.has_data()
-
- -.. method:: Request.get_data() -
- .. method:: Request.add_header(key, val) Add another header to the request. Headers are currently ignored by all @@ -440,21 +424,6 @@ request. Return the URL given in the constructor. -.. method:: Request.get_type() -
- -.. method:: Request.get_host() -
- -.. method:: Request.get_selector() -
- .. method:: Request.set_proxy(host, type) Prepare the request by connecting to a proxy server. The host and type will @@ -462,16 +431,71 @@ request. URL given in the constructor. +.. method:: Request.add_data(data) +
- Set the :class:
Request
data to data. This is ignored by all handlers except - HTTP handlers --- and there it should be a byte string, and will change the
- request to be
POST
rather thanGET
. Deprecated in 3.3, use - :attr:
Request.data
. + - .. deprecated:: 3.3 +
+ +.. method:: Request.has_data() +
- Return whether the instance has a non-\
None
data. Deprecated in 3.3, - use :attr:
Request.data
. + - .. deprecated:: 3.3 +
+ +.. method:: Request.get_data() +
+ +.. method:: Request.get_type() +
- Return the type of the URL --- also known as the scheme. Deprecated in 3.3,
- use :attr:
Request.type
. + - .. deprecated:: 3.3 +
+ +.. method:: Request.get_host() +
- Return the host to which a connection will be made. Deprecated in 3.3, use
- :attr:
Request.host
. + - .. deprecated:: 3.3 +
+ +.. method:: Request.get_selector() +
- Return the selector --- the part of the URL that is sent to the server.
- Deprecated in 3.3, use :attr:
Request.selector
. + - .. deprecated:: 3.3 +
+ .. method:: Request.get_origin_req_host()
- Return the request-host of the origin transaction, as defined by :rfc:
2965
. - See the documentation for the :class:
Request
constructor.
- Return the request-host of the origin transaction, as defined by
- :rfc:
2965
. See the documentation for the :class:Request
constructor. - Deprecated in 3.3, use :attr:
Request.origin_req_host
. + - .. deprecated:: 3.3
.. method:: Request.is_unverifiable() Return whether the request is unverifiable, as defined by RFC 2965. See the
- documentation for the :class:
Request
constructor. Deprecated in 3.3, use - :attr:
Request.is_unverifiable
. + - .. deprecated:: 3.3
--- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -298,6 +298,10 @@ Content-Type: text/html; charset=iso-885 finally: self.unfakehttp()
- def test_URLopener_deprecation(self):
with support.check_warnings(('',DeprecationWarning)):[](#l2.8)
warn = urllib.request.URLopener()[](#l2.9)
+ class urlretrieve_FileTests(unittest.TestCase): """Test urllib.urlretrieve() on local files"""
--- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -553,10 +553,6 @@ class OpenerDirectorTests(unittest.TestC self.assertRaises(urllib.error.URLError, o.open, req) self.assertEqual(o.calls, [(handlers[0], "http_open", (req,), {})]) -## def test_error(self): -## # XXX this doesn't actually seem to be used in standard library, -## # but should really be tested anyway... - def test_http_error(self): # XXX http_error_default # http errors are a special case @@ -584,6 +580,7 @@ class OpenerDirectorTests(unittest.TestC self.assertEqual((handler, method_name), got[:2]) self.assertEqual(args, got[2]) + def test_processors(self): # *_request / *_response methods get called appropriately o = OpenerDirector() @@ -619,6 +616,24 @@ class OpenerDirectorTests(unittest.TestC self.assertTrue(args[1] is None or isinstance(args[1], MockResponse))
- def test_method_deprecations(self):
req = Request("http://www.example.com")[](#l3.27)
with support.check_warnings(('', DeprecationWarning)):[](#l3.28)
req.add_data("data")[](#l3.29)
with support.check_warnings(('', DeprecationWarning)):[](#l3.30)
req.has_data()[](#l3.31)
with support.check_warnings(('', DeprecationWarning)):[](#l3.32)
req.get_data()[](#l3.33)
with support.check_warnings(('', DeprecationWarning)):[](#l3.34)
req.get_full_url()[](#l3.35)
with support.check_warnings(('', DeprecationWarning)):[](#l3.36)
req.get_host()[](#l3.37)
with support.check_warnings(('', DeprecationWarning)):[](#l3.38)
req.get_selector()[](#l3.39)
with support.check_warnings(('', DeprecationWarning)):[](#l3.40)
req.is_unverifiable()[](#l3.41)
with support.check_warnings(('', DeprecationWarning)):[](#l3.42)
req.get_origin_req_host()[](#l3.43)
def sanepathname2url(path): try:
--- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -96,6 +96,7 @@ import time import collections import tempfile import contextlib +import warnings from urllib.error import URLError, HTTPError, ContentTooShortError @@ -291,36 +292,52 @@ class Request: else: return "GET"
- def get_full_url(self): if self.fragment: return '%s#%s' % (self.full_url, self.fragment) else: return self.full_url
- def add_data(self, data):
msg = "Request.add_data method is deprecated."[](#l4.35)
warnings.warn(msg, DeprecationWarning, stacklevel=1)[](#l4.36)
self.data = data[](#l4.37)
- def has_data(self):
msg = "Request.has_data method is deprecated."[](#l4.40)
warnings.warn(msg, DeprecationWarning, stacklevel=1)[](#l4.41)
return self.data is not None[](#l4.42)
- def get_data(self):
msg = "Request.get_data method is deprecated."[](#l4.45)
warnings.warn(msg, DeprecationWarning, stacklevel=1)[](#l4.46)
return self.data[](#l4.47)
msg = "Request.get_type method is deprecated."[](#l4.50)
warnings.warn(msg, DeprecationWarning, stacklevel=1)[](#l4.51) return self.type[](#l4.52)
msg = "Request.get_host method is deprecated."[](#l4.55)
warnings.warn(msg, DeprecationWarning, stacklevel=1)[](#l4.56) return self.host[](#l4.57)
msg = "Request.get_selector method is deprecated."[](#l4.60)
warnings.warn(msg, DeprecationWarning, stacklevel=1)[](#l4.61) return self.selector[](#l4.62)
msg = "Request.is_unverifiable method is deprecated."[](#l4.65)
warnings.warn(msg, DeprecationWarning, stacklevel=1)[](#l4.66) return self.unverifiable[](#l4.67)
def get_origin_req_host(self):
msg = "Request.get_origin_req_host method is deprecated."[](#l4.70)
warnings.warn(msg, DeprecationWarning, stacklevel=1)[](#l4.71) return self.origin_req_host[](#l4.72)
# End deprecated methods @@ -1552,6 +1569,9 @@ class URLopener: # Constructor def init(self, proxies=None, **x509):
msg = "%(class)s style of invoking requests is deprecated."\[](#l4.79)
"Use newer urlopen functions/methods" % {'class': self.__class__.__name__}[](#l4.80)
warnings.warn(msg, DeprecationWarning, stacklevel=3)[](#l4.81) if proxies is None:[](#l4.82) proxies = getproxies()[](#l4.83) assert hasattr(proxies, 'keys'), "proxies must be a mapping"[](#l4.84)
@@ -1753,7 +1773,6 @@ class URLopener: if proxy_bypass(realhost): host = realhost
#print "proxy via http:", host, selector[](#l4.89) if not host: raise IOError('http error', 'no host given')[](#l4.90)
if proxy_passwd: @@ -2554,7 +2573,6 @@ elif os.name == 'nt': test = test.replace("", r".") # change glob sequence test = test.replace("?", r".") # change glob char for val in host:
# print "%s <--> %s" %( test, val )[](#l4.97) if re.match(test, val, re.I):[](#l4.98) return 1[](#l4.99) return 0[](#l4.100)