8000 bpo-12707: deprecate info(), geturl(), getcode() methods in favor of … · python/cpython@ff2e182 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff2e182

Browse files
epicfaacematrixise
authored andcommitted
bpo-12707: deprecate info(), geturl(), getcode() methods in favor of headers, url, and status properties for HTTPResponse and addinfourl (GH-11447)
Co-Authored-By: epicfaace <aramaswamis@gmail.com>
1 parent bb41147 commit ff2e182

File tree

7 files changed

+84
-26
lines changed

7 files changed

+84
-26
lines changed

Doc/library/http.client.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,14 @@ statement.
484484

485485
HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
486486

487+
.. attribute:: HTTPResponse.url
488+
489+
URL of the resource retrieved, commonly used to determine if a redirect was followed.
490+
491+
.. attribute:: HTTPResponse.headers
492+
493+
Headers of the response in the form of an :class:`email.message.EmailMessage` instance.
494+
487495
.. attribute:: HTTPResponse.status
488496

489497
Status code returned by server.
@@ -501,6 +509,21 @@ statement.
501509

502510
Is ``True`` if the stream is closed.
503511

512+
.. method:: HTTPResponse.geturl()
513+
514+
.. deprecated:: 3.9
515+
Deprecated in favor of :attr:`~HTTPResponse.url`.
516+
517+
.. method:: HTTPResponse.info()
518+
519+
.. deprecated:: 3.9
520+
Deprecated in favor of :attr:`~HTTPResponse.headers`.
521+
522+
.. method:: HTTPResponse.getstatus()
523+
524+
.. deprecated:: 3.9
525+
Deprecated in favor of :attr:`~HTTPResponse.status`.
526+
504527
Examples
505528
--------
506529

Doc/library/urllib.request.rst

Lines changed: 40 additions & 15 deletions
9E81
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,8 @@ The :mod:`urllib.request` module defines the following functions:
5555
The *cadefault* parameter is ignored.
5656

5757
This function always returns an object which can work as a
58-
:term:`context manager` and has methods such as
59-
60-
* :meth:`~urllib.response.addinfourl.geturl` --- return the URL of the resource retrieved,
61-
commonly used to determine if a redirect was followed
62-
63-
* :meth:`~urllib.response.addinfourl.info` --- return the meta-information of the page, such as headers,
64-
in the form of an :func:`email.message_from_string` instance (see
65-
`Quick Reference to HTTP Headers <http://jkorpela.fi/http.html>`_)
66-
67-
* :meth:`~urllib.response.addinfourl.getcode` -- return the HTTP status code of the response.
58+
:term:`context manager` and has the properties *url*, *headers*, and *status*.
59+
See :class:`urllib.response.addinfourl` for more detail on these properties.
6860

6961
For HTTP and HTTPS URLs, this function returns a
7062
:class:`http.client.HTTPResponse` object slightly modified. In addition
@@ -1585,9 +1577,42 @@ some point in the future.
15851577
:synopsis: Response classes used by urllib.
15861578

15871579
The :mod:`urllib.response` module defines functions and classes which define a
1588-
minimal file like interface, including ``read()`` and ``readline()``. The
1589-
typical response object is an addinfourl instance, which defines an ``info()``
1590-
method and that returns headers and a ``geturl()`` method that returns the url.
1591-
Functions defined by this module are used internally by the
1592-
:mod:`urllib.request` module.
1580+
minimal file-like interface, including ``read()`` and ``readline()``.
1581+
Functions defined by this module are used internally by the :mod:`urllib.request` module.
1582+
The typical response object is a :class:`urllib.response.addinfourl` instance:
1583+
1584+
.. class:: addinfourl
1585+
1586+
.. attribute:: url
1587+
1588+
URL of the resource retrieved, commonly used to determine if a redirect was followed.
1589+
1590+
.. attribute:: headers
1591+
1592+
Returns the headers of the response in the form of an :class:`~email.message.EmailMessage` instance.
1593+
1594+
.. attribute:: status
1595+
1596+
.. versionadded:: 3.9
1597+
1598+
Status code returned by server.
1599+
1600+
.. method:: geturl()
1601+
1602+
.. deprecated:: 3.9
1603+
Deprecated in favor of :attr:`~addinfourl.url`.
1604+
1605+
.. method:: info()
1606+
1607+
.. deprecated:: 3.9
1608+
Deprecated in favor of :attr:`~addinfourl.headers`.
1609+
1610+
.. attribute:: code
1611+
1612+
.. deprecated:: 3.9
1613+
Deprecated in favor of :attr:`~addinfourl.status`.
1614+
1615+
.. method:: getstatus()
15931616

1617+
.. deprecated:: 3.9
1618+
Deprecated in favor of :attr:`~addinfourl.status`.

Lib/test/test_urllib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ def test_close(self):
194194
# by the tearDown() method for the test
195195
self.returned_obj.close()
196196

197+
def test_headers(self):
198+
self.assertIsInstance(self.returned_obj.headers, email.message.Message)
199+
200+
def test_url(self):
201+
self.assertEqual(self.returned_obj.url, self.pathname)
202+
203+
def test_status(self):
204+
self.assertIsNone(self.returned_obj.status)
205+
197206
def test_info(self):
198207
self.assertIsInstance(self.returned_obj.info(), email.message.Message)
199208

Lib/test/test_urllib_response.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def closehook():
4242
def test_addinfo(self):
4343
info = urllib.response.addinfo(self.fp, self.test_headers)
4444
self.assertEqual(info.info(), self.test_headers)
45+
self.assertEqual(info.headers, self.test_headers)
4546

4647
def test_addinfourl(self):
4748
url = "http://www.python.org"
@@ -51,6 +52,9 @@ def test_addinfourl(self):
5152
self.assertEqual(infourl.info(), self.test_headers)
5253
self.assertEqual(infourl.geturl(), url)
5354
self.assertEqual(infourl.getcode(), code)
55+
self.assertEqual(infourl.headers, self.test_headers)
56+
self.assertEqual(infourl.url, url)
57+
self.assertEqual(infourl.status, code)
5458

5559
def tearDown(self):
5660
self.sock.close()

Lib/urllib/request.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,10 @@ def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
163163
164164
The *cadefault* parameter is ignored.
165165
166-
This function always returns an object which can work as a context
167-
manager and has methods such as
168166
169-
* geturl() - return the URL of the resource retrieved, commonly used to
170-
determine if a redirect was followed
171-
172-
* info() - return the meta-information of the page, such as headers, in the
173-
form of an email.message_from_string() instance (see Quick Reference to
174-
HTTP Headers)
175-
176-
* getcode() - return the HTTP status code of the response. Raises URLError
177-
on errors.
167+
This function always returns an object which can work as a
168+
context manager and has the properties url, headers, and status.
169+
See urllib.response.addinfourl for more detail on these properties.
178170
179171
For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse
180172
object slightly modified. In addition to the three new methods above, the

Lib/urllib/response.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def __init__(self, fp, headers, url, code=None):
7373
self.url = url
7474
self.code = code
7575

76+
@property
77+
def status(self):
78+
return self.code
79+
7680
def getcode(self):
7781
return self.code
7882

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate info(), geturl(), getcode() methods in favor of the headers, url, and status properties, respectively, for HTTPResponse and addinfourl. Also deprecate the code attribute of addinfourl in favor of the status attribute. Patch by Ashwin Ramaswami

0 commit comments

Comments
 (0)
0