diff options
author | Senthil Kumaran <senthil@uthcode.com> | 2012-03-14 20:43:53 (GMT) |
---|---|---|
committer | Senthil Kumaran <senthil@uthcode.com> | 2012-03-14 20:43:53 (GMT) |
commit | 38b968b9131820d089e4dc68b11675c3bc98a631 (patch) | |
tree | 0595d65dfc835b6599bbb5ad9e116c4c03a53a48 /Lib | |
parent | c17adf41511f7165935074bc505e09e0574f59be (diff) | |
download | cpython-38b968b9131820d089e4dc68b11675c3bc98a631.zip cpython-38b968b9131820d089e4dc68b11675c3bc98a631.tar.gz cpython-38b968b9131820d089e4dc68b11675c3bc98a631.tar.bz2 |
deprecated the old urllib primitives in 3.3 urllib package - issue 10050
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_urllib.py | 4 | ||||
-rw-r--r-- | Lib/test/test_urllib2.py | 23 | ||||
-rw-r--r-- | Lib/urllib/request.py | 34 |
3 files changed, 49 insertions, 12 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 85f8f84..22ada56 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -298,6 +298,10 @@ Content-Type: text/html; charset=iso-8859-1 finally: self.unfakehttp() + def test_URLopener_deprecation(self): + with support.check_warnings(('',DeprecationWarning)): + warn = urllib.request.URLopener() + class urlretrieve_FileTests(unittest.TestCase): """Test urllib.urlretrieve() on local files""" diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 0ff3181..2161690 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -553,10 +553,6 @@ class OpenerDirectorTests(unittest.TestCase): 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.TestCase): 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.TestCase): self.assertTrue(args[1] is None or isinstance(args[1], MockResponse)) + def test_method_deprecations(self): + req = Request("http://www.example.com") + with support.check_warnings(('', DeprecationWarning)): + req.add_data("data") + with support.check_warnings(('', DeprecationWarning)): + req.has_data() + with support.check_warnings(('', DeprecationWarning)): + req.get_data() + with support.check_warnings(('', DeprecationWarning)): + req.get_full_url() + with support.check_warnings(('', DeprecationWarning)): + req.get_host() + with support.check_warnings(('', DeprecationWarning)): + req.get_selector() + with support.check_warnings(('', DeprecationWarning)): + req.is_unverifiable() + with support.check_warnings(('', DeprecationWarning)): + req.get_origin_req_host() def sanepathname2url(path): try: diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index c220a7d..7a5b0e2 100644 --- 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 + # Begin deprecated methods def add_data(self, data): + msg = "Request.add_data method is deprecated." + warnings.warn(msg, DeprecationWarning, stacklevel=1) self.data = data def has_data(self): + msg = "Request.has_data method is deprecated." + warnings.warn(msg, DeprecationWarning, stacklevel=1) return self.data is not None def get_data(self): + msg = "Request.get_data method is deprecated." + warnings.warn(msg, DeprecationWarning, stacklevel=1) return self.data - def get_full_url(self): - if self.fragment: - return '%s#%s' % (self.full_url, self.fragment) - else: - return self.full_url - def get_type(self): + msg = "Request.get_type method is deprecated." + warnings.warn(msg, DeprecationWarning, stacklevel=1) return self.type def get_host(self): + msg = "Request.get_host method is deprecated." + warnings.warn(msg, DeprecationWarning, stacklevel=1) return self.host def get_selector(self): + msg = "Request.get_selector method is deprecated." + warnings.warn(msg, DeprecationWarning, stacklevel=1) return self.selector def is_unverifiable(self): + msg = "Request.is_unverifiable method is deprecated." + warnings.warn(msg, DeprecationWarning, stacklevel=1) return self.unverifiable def get_origin_req_host(self): + msg = "Request.get_origin_req_host method is deprecated." + warnings.warn(msg, DeprecationWarning, stacklevel=1) return self.origin_req_host # 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."\ + "Use newer urlopen functions/methods" % {'class': self.__class__.__name__} + warnings.warn(msg, DeprecationWarning, stacklevel=3) if proxies is None: proxies = getproxies() assert hasattr(proxies, 'keys'), "proxies must be a mapping" @@ -1753,7 +1773,6 @@ class URLopener: if proxy_bypass(realhost): host = realhost - #print "proxy via http:", host, selector if not host: raise IOError('http error', 'no host given') 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 ) if re.match(test, val, re.I): return 1 return 0 |