[Python-checkins] (no subject)

Stéphane Wirtel webhook-mailer at python.org
Fri Sep 13 07:40:11 EDT 2019




To: python-checkins at python.org
Subject: bpo-12707: deprecate info(), geturl(), getcode() methods in favor of
 headers, url, and status properties for HTTPResponse and addinfourl
 (GH-11447)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/ff2e18286560e981f4e09afb0d2448ea9944=
14d8
commit: ff2e18286560e981f4e09afb0d2448ea994414d8
branch: master
author: Ashwin Ramaswami <aramaswamis at gmail.com>
committer: St=C3=A9phane Wirtel <stephane at wirtel.be>
date: 2019-09-13T12:40:07+01:00
summary:

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 at gmail.com>

files:
A Misc/NEWS.d/next/Documentation/2019-08-27-01-14-59.bpo-12707.Yj3_7_.rst
M Doc/library/http.client.rst
M Doc/library/urllib.request.rst
M Lib/test/test_urllib.py
M Lib/test/test_urllib_response.py
M Lib/urllib/request.py
M Lib/urllib/response.py

diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst
index db26d56af30f..a458ab77efd8 100644
--- a/Doc/library/http.client.rst
+++ b/Doc/library/http.client.rst
@@ -484,6 +484,14 @@ statement.
=20
    HTTP protocol version used by server.  10 for HTTP/1.0, 11 for HTTP/1.1.
=20
+.. attribute:: HTTPResponse.url
+
+   URL of the resource retrieved, commonly used to determine if a redirect w=
as followed.
+
+.. attribute:: HTTPResponse.headers
+
+   Headers of the response in the form of an :class:`email.message.EmailMess=
age` instance.
+
 .. attribute:: HTTPResponse.status
=20
    Status code returned by server.
@@ -501,6 +509,21 @@ statement.
=20
    Is ``True`` if the stream is closed.
=20
+.. method:: HTTPResponse.geturl()
+
+   .. deprecated:: 3.9
+      Deprecated in favor of :attr:`~HTTPResponse.url`.
+
+.. method:: HTTPResponse.info()
+
+   .. deprecated:: 3.9
+      Deprecated in favor of :attr:`~HTTPResponse.headers`.
+
+.. method:: HTTPResponse.getstatus()
+
+   .. deprecated:: 3.9
+      Deprecated in favor of :attr:`~HTTPResponse.status`.
+
 Examples
 --------
=20
diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst
index 448bc6785470..a903d605cf4e 100644
--- a/Doc/library/urllib.request.rst
+++ b/Doc/library/urllib.request.rst
@@ -55,16 +55,8 @@ The :mod:`urllib.request` module defines the following fun=
ctions:
    The *cadefault* parameter is ignored.
=20
    This function always returns an object which can work as a
-   :term:`context manager` and has methods such as
-
-   * :meth:`~urllib.response.addinfourl.geturl` --- return the URL of the re=
source retrieved,
-     commonly used to determine if a redirect was followed
-
-   * :meth:`~urllib.response.addinfourl.info` --- return the meta-informatio=
n of the page, such as headers,
-     in the form of an :func:`email.message_from_string` instance (see
-     `Quick Reference to HTTP Headers <http://jkorpela.fi/http.html>`_)
-
-   * :meth:`~urllib.response.addinfourl.getcode` -- return the HTTP status c=
ode of the response.
+   :term:`context manager` and has the properties *url*, *headers*, and *sta=
tus*.
+   See :class:`urllib.response.addinfourl` for more detail on these properti=
es.
=20
    For HTTP and HTTPS URLs, this function returns a
    :class:`http.client.HTTPResponse` object slightly modified. In addition
@@ -1585,9 +1577,42 @@ some point in the future.
    :synopsis: Response classes used by urllib.
=20
 The :mod:`urllib.response` module defines functions and classes which define=
 a
-minimal file like interface, including ``read()`` and ``readline()``. The
-typical response object is an addinfourl instance, which defines an ``info()=
``
-method and that returns headers and a ``geturl()`` method that returns the u=
rl.
-Functions defined by this module are used internally by the
-:mod:`urllib.request` module.
+minimal file-like interface, including ``read()`` and ``readline()``.
+Functions defined by this module are used internally by the :mod:`urllib.req=
uest` module.
+The typical response object is a :class:`urllib.response.addinfourl` instanc=
e:
+
+.. class:: addinfourl
+
+   .. attribute:: url
+
+      URL of the resource retrieved, commonly used to determine if a redirec=
t was followed.
+
+   .. attribute:: headers
+
+      Returns the headers of the response in the form of an :class:`~email.m=
essage.EmailMessage` instance.
+
+   .. attribute:: status
+
+      .. versionadded:: 3.9
+
+      Status code returned by server.
+
+   .. method:: geturl()
+
+      .. deprecated:: 3.9
+         Deprecated in favor of :attr:`~addinfourl.url`.
+
+   .. method:: info()
+
+      .. deprecated:: 3.9
+         Deprecated in favor of :attr:`~addinfourl.headers`.
+
+   .. attribute:: code
+
+      .. deprecated:: 3.9
+         Deprecated in favor of :attr:`~addinfourl.status`.
+
+   .. method:: getstatus()
=20
+      .. deprecated:: 3.9
+         Deprecated in favor of :attr:`~addinfourl.status`.
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 8895421f97fc..9a6b5f66b7a1 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -194,6 +194,15 @@ def test_close(self):
         # by the tearDown() method for the test
         self.returned_obj.close()
=20
+    def test_headers(self):
+        self.assertIsInstance(self.returned_obj.headers, email.message.Messa=
ge)
+
+    def test_url(self):
+        self.assertEqual(self.returned_obj.url, self.pathname)
+
+    def test_status(self):
+        self.assertIsNone(self.returned_obj.status)
+
     def test_info(self):
         self.assertIsInstance(self.returned_obj.info(), email.message.Messag=
e)
=20
diff --git a/Lib/test/test_urllib_response.py b/Lib/test/test_urllib_response=
.py
index 0eb59426ccb6..73d2ef0424f4 100644
--- a/Lib/test/test_urllib_response.py
+++ b/Lib/test/test_urllib_response.py
@@ -42,6 +42,7 @@ def closehook():
     def test_addinfo(self):
         info =3D urllib.response.addinfo(self.fp, self.test_headers)
         self.assertEqual(info.info(), self.test_headers)
+        self.assertEqual(info.headers, self.test_headers)
=20
     def test_addinfourl(self):
         url =3D "http://www.python.org"
@@ -51,6 +52,9 @@ def test_addinfourl(self):
         self.assertEqual(infourl.info(), self.test_headers)
         self.assertEqual(infourl.geturl(), url)
         self.assertEqual(infourl.getcode(), code)
+        self.assertEqual(infourl.headers, self.test_headers)
+        self.assertEqual(infourl.url, url)
+        self.assertEqual(infourl.status, code)
=20
     def tearDown(self):
         self.sock.close()
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index f6ce9cb6d586..267a90dd80db 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -163,18 +163,10 @@ def urlopen(url, data=3DNone, timeout=3Dsocket._GLOBAL_=
DEFAULT_TIMEOUT,
=20
     The *cadefault* parameter is ignored.
=20
-    This function always returns an object which can work as a context
-    manager and has methods such as
=20
-    * geturl() - return the URL of the resource retrieved, commonly used to
-      determine if a redirect was followed
-
-    * info() - return the meta-information of the page, such as headers, in =
the
-      form of an email.message_from_string() instance (see Quick Reference to
-      HTTP Headers)
-
-    * getcode() - return the HTTP status code of the response.  Raises URLEr=
ror
-      on errors.
+    This function always returns an object which can work as a
+    context manager and has the properties url, headers, and status.
+    See urllib.response.addinfourl for more detail on these properties.
=20
     For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse
     object slightly modified. In addition to the three new methods above, the
diff --git a/Lib/urllib/response.py b/Lib/urllib/response.py
index 4778118dbb17..5a2c3cc78c39 100644
--- a/Lib/urllib/response.py
+++ b/Lib/urllib/response.py
@@ -73,6 +73,10 @@ def __init__(self, fp, headers, url, code=3DNone):
         self.url =3D url
         self.code =3D code
=20
+    @property
+    def status(self):
+        return self.code
+
     def getcode(self):
         return self.code
=20
diff --git a/Misc/NEWS.d/next/Documentation/2019-08-27-01-14-59.bpo-12707.Yj3=
_7_.rst b/Misc/NEWS.d/next/Documentation/2019-08-27-01-14-59.bpo-12707.Yj3_7_=
.rst
new file mode 100644
index 000000000000..423e8310d737
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2019-08-27-01-14-59.bpo-12707.Yj3_7_.rst
@@ -0,0 +1 @@
+Deprecate info(), geturl(), getcode() methods in favor of the headers, url, =
and status properties, respectively, for HTTPResponse and addinfourl. Also de=
precate the code attribute of addinfourl in favor of the status attribute. Pa=
tch by Ashwin Ramaswami
\ No newline at end of file



More information about the Python-checkins mailing list