Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38b968b

Browse files
committed
deprecated the old urllib primitives in 3.3 urllib package - issue 10050
1 parent c17adf4 commit 38b968b

4 files changed

Lines changed: 100 additions & 39 deletions

File tree

Doc/library/urllib.request.rst

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,6 @@ request.
385385

386386
.. versionadded:: 3.3
387387

388-
.. method:: Request.add_data(data)
389-
390-
Set the :class:`Request` data to *data*. This is ignored by all handlers except
391-
HTTP handlers --- and there it should be a byte string, and will change the
392-
request to be ``POST`` rather than ``GET``.
393-
394388

395389
.. method:: Request.get_method()
396390

@@ -403,16 +397,6 @@ request.
403397
get_method now looks at the value of :attr:`Request.method`.
404398

405399

406-
.. method:: Request.has_data()
407-
408-
Return whether the instance has a non-\ ``None`` data.
409-
410-
411-
.. method:: Request.get_data()
412-
413-
Return the instance's data.
414-
415-
416400
.. method:: Request.add_header(key, val)
417401

418402
Add another header to the request. Headers are currently ignored by all
@@ -440,38 +424,78 @@ request.
440424
Return the URL given in the constructor.
441425

442426

427+
.. method:: Request.set_proxy(host, type)
428+
429+
Prepare the request by connecting to a proxy server. The *host* and *type* will
430+
replace those of the instance, and the instance's selector will be the original
431+
URL given in the constructor.
432+
433+
434+
.. method:: Request.add_data(data)
435+
436+
Set the :class:`Request` data to *data*. This is ignored by all handlers except
437+
HTTP handlers --- and there it should be a byte string, and will change the
438+
request to be ``POST`` rather than ``GET``. Deprecated in 3.3, use
439+
:attr:`Request.data`.
440+
441+
.. deprecated:: 3.3
442+
443+
444+
.. method:: Request.has_data()
445+
446+
Return whether the instance has a non-\ ``None`` data. Deprecated in 3.3,
447+
use :attr:`Request.data`.
448+
449+
.. deprecated:: 3.3
450+
451+
452+
.. method:: Request.get_data()
453+
454+
Return the instance's data. Deprecated in 3.3, use :attr:`Request.data`.
455+
456+
.. deprecated:: 3.3
457+
458+
443459
.. method:: Request.get_type()
444460

445-
Return the type of the URL --- also known as the scheme.
461+
Return the type of the URL --- also known as the scheme. Deprecated in 3.3,
462+
use :attr:`Request.type`.
463+
464+
.. deprecated:: 3.3
446465

447466

448467
.. method:: Request.get_host()
449468

450-
Return the host to which a connection will be made.
469+
Return the host to which a connection will be made. Deprecated in 3.3, use
470+
:attr:`Request.host`.
471+
472+
.. deprecated:: 3.3
451473

452474

453475
.. method:: Request.get_selector()
454476

455477
Return the selector --- the part of the URL that is sent to the server.
478+
Deprecated in 3.3, use :attr:`Request.selector`.
456479

457-
458-
.. method:: Request.set_proxy(host, type)
459-
460-
Prepare the request by connecting to a proxy server. The *host* and *type* will
461-
replace those of the instance, and the instance's selector will be the original
462-
URL given in the constructor.
480+
.. deprecated:: 3.3
463481

464482

465483
.. method:: Request.get_origin_req_host()
466484

467-
Return the request-host of the origin transaction, as defined by :rfc:`2965`.
468-
See the documentation for the :class:`Request` constructor.
485+
Return the request-host of the origin transaction, as defined by
486+
:rfc:`2965`. See the documentation for the :class:`Request` constructor.
487+
Deprecated in 3.3, use :attr:`Request.origin_req_host`.
488+
489+
.. deprecated:: 3.3
469490

470491

471492
.. method:: Request.is_unverifiable()
472493

473494
Return whether the request is unverifiable, as defined by RFC 2965. See the
474-
documentation for the :class:`Request` constructor.
495+
documentation for the :class:`Request` constructor. Deprecated in 3.3, use
496+
:attr:`Request.is_unverifiable`.
497+
498+
.. deprecated:: 3.3
475499

476500

477501
.. _opener-director-objects:

Lib/test/test_urllib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ def test_userpass_inurl_w_spaces(self):
298298
finally:
299299
self.unfakehttp()
300300

301+
def test_URLopener_deprecation(self):
302+
with support.check_warnings(('',DeprecationWarning)):
303+
warn = urllib.request.URLopener()
304+
301305
class urlretrieve_FileTests(unittest.TestCase):
302306
"""Test urllib.urlretrieve() on local files"""
303307

Lib/test/test_urllib2.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,6 @@ def test_raise(self):
553553
self.assertRaises(urllib.error.URLError, o.open, req)
554554
self.assertEqual(o.calls, [(handlers[0], "http_open", (req,), {})])
555555

556-
## def test_error(self):
557-
## # XXX this doesn't actually seem to be used in standard library,
558-
## # but should really be tested anyway...
559-
560556
def test_http_error(self):
561557
# XXX http_error_default
562558
# http errors are a special case
@@ -584,6 +580,7 @@ def __eq__(self, other): return True
584580
self.assertEqual((handler, method_name), got[:2])
585581
self.assertEqual(args, got[2])
586582

583+
587584
def test_processors(self):
588585
# *_request / *_response methods get called appropriately
589586
o = OpenerDirector()
@@ -619,6 +616,24 @@ def test_processors(self):
619616
self.assertTrue(args[1] is None or
620617
isinstance(args[1], MockResponse))
621618

619+
def test_method_deprecations(self):
620+
req = Request("http://www.example.com")
621+
with support.check_warnings(('', DeprecationWarning)):
622+
req.add_data("data")
623+
with support.check_warnings(('', DeprecationWarning)):
624+
req.has_data()
625+
with support.check_warnings(('', DeprecationWarning)):
626+
req.get_data()
627+
with support.check_warnings(('', DeprecationWarning)):
628+
req.get_full_url()
629+
with support.check_warnings(('', DeprecationWarning)):
630+
req.get_host()
631+
with support.check_warnings(('', DeprecationWarning)):
632+
req.get_selector()
633+
with support.check_warnings(('', DeprecationWarning)):
634+
req.is_unverifiable()
635+
with support.check_warnings(('', DeprecationWarning)):
636+
req.get_origin_req_host()
622637

623638
def sanepathname2url(path):
624639
try:

Lib/urllib/request.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import collections
9797
import tempfile
9898
import contextlib
99+
import warnings
99100

100101

101102
from urllib.error import URLError, HTTPError, ContentTooShortError
@@ -291,36 +292,52 @@ def get_method(self):
291292
else:
292293
return "GET"
293294

295+
def get_full_url(self):
296+
if self.fragment:
297+
return '%s#%s' % (self.full_url, self.fragment)
298+
else:
299+
return self.full_url
300+
294301
# Begin deprecated methods
295302

296303
def add_data(self, data):
304+
msg = "Request.add_data method is deprecated."
305+
warnings.warn(msg, DeprecationWarning, stacklevel=1)
297306
self.data = data
298307

299308
def has_data(self):
309+
msg = "Request.has_data method is deprecated."
310+
warnings.warn(msg, DeprecationWarning, stacklevel=1)
300311
return self.data is not None
301312

302313
def get_data(self):
314+
msg = "Request.get_data method is deprecated."
315+
warnings.warn(msg, DeprecationWarning, stacklevel=1)
303316
return self.data
304317

305-
def get_full_url(self):
306-
if self.fragment:
307-
return '%s#%s' % (self.full_url, self.fragment)
308-
else:
309-
return self.full_url
310-
311318
def get_type(self):
319+
msg = "Request.get_type method is deprecated."
320+
warnings.warn(msg, DeprecationWarning, stacklevel=1)
312321
return self.type
313322

314323
def get_host(self):
324+
msg = "Request.get_host method is deprecated."
325+
warnings.warn(msg, DeprecationWarning, stacklevel=1)
315326
return self.host
316327

317328
def get_selector(self):
329+
msg = "Request.get_selector method is deprecated."
330+
warnings.warn(msg, DeprecationWarning, stacklevel=1)
318331
return self.selector
319332

320333
def is_unverifiable(self):
334+
msg = "Request.is_unverifiable method is deprecated."
335+
warnings.warn(msg, DeprecationWarning, stacklevel=1)
321336
return self.unverifiable
322337

323338
def get_origin_req_host(self):
339+
msg = "Request.get_origin_req_host method is deprecated."
340+
warnings.warn(msg, DeprecationWarning, stacklevel=1)
324341
return self.origin_req_host
325342

326343
# End deprecated methods
@@ -1552,6 +1569,9 @@ class URLopener:
15521569

15531570
# Constructor
15541571
def __init__(self, proxies=None, **x509):
1572+
msg = "%(class)s style of invoking requests is deprecated."\
1573+
"Use newer urlopen functions/methods" % {'class': self.__class__.__name__}
1574+
warnings.warn(msg, DeprecationWarning, stacklevel=3)
15551575
if proxies is None:
15561576
proxies = getproxies()
15571577
assert hasattr(proxies, 'keys'), "proxies must be a mapping"
@@ -1753,7 +1773,6 @@ def _open_generic_http(self, connection_factory, url, data):
17531773
if proxy_bypass(realhost):
17541774
host = realhost
17551775

1756-
#print "proxy via http:", host, selector
17571776
if not host: raise IOError('http error', 'no host given')
17581777

17591778
if proxy_passwd:
@@ -2554,7 +2573,6 @@ def proxy_bypass_registry(host):
25542573
test = test.replace("*", r".*") # change glob sequence
25552574
test = test.replace("?", r".") # change glob char
25562575
for val in host:
2557-
# print "%s <--> %s" %( test, val )
25582576
if re.match(test, val, re.I):
25592577
return 1
25602578
return 0

0 commit comments

Comments
 (0)