From e1ab1049c06ae1d587f10bf0c2bc1acba2252f83 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 17 Nov 2020 22:03:16 +0100 Subject: [PATCH 1/6] bpo-42393: Raise overflow error on overflow in socket.ntohs and socket.htons --- Doc/library/socket.rst | 18 ++++-------------- Lib/test/test_socket.py | 11 +++++------ Modules/socketmodule.c | 36 ++++++++++++------------------------ 3 files changed, 21 insertions(+), 44 deletions(-) diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index 4511ff9ea4a51a..19587d86aaf0a5 100755 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -905,13 +905,8 @@ The :mod:`socket` module also offers various network-related services: Convert 16-bit positive integers from network to host byte order. On machines where the host byte order is the same as network byte order, this is a no-op; - otherwise, it performs a 2-byte swap operation. - - .. deprecated:: 3.7 - In case *x* does not fit in 16-bit unsigned integer, but does fit in a - positive C int, it is silently truncated to 16-bit unsigned integer. - This silent truncation feature is deprecated, and will raise an - exception in future versions of Python. + otherwise, it performs a 2-byte swap operation. :exc:`OverflowError` is raised + if *x* does not fit in a 16-bit unsigned integer. .. function:: htonl(x) @@ -925,13 +920,8 @@ The :mod:`socket` module also offers various network-related services: Convert 16-bit positive integers from host to network byte order. On machines where the host byte order is the same as network byte order, this is a no-op; - otherwise, it performs a 2-byte swap operation. - - .. deprecated:: 3.7 - In case *x* does not fit in 16-bit unsigned integer, but does fit in a - positive C int, it is silently truncated to 16-bit unsigned integer. - This silent truncation feature is deprecated, and will raise an - exception in future versions of Python. + otherwise, it performs a 2-byte swap operation. :exc:`OverflowError` is raised + if *x* does not fit in a 16-bit unsigned integer. .. function:: inet_aton(ip_string) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index e4af713b4c5bf7..bc280306b15d14 100755 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1121,9 +1121,11 @@ def testNtoHErrors(self): s_good_values = [0, 1, 2, 0xffff] l_good_values = s_good_values + [0xffffffff] l_bad_values = [-1, -2, 1<<32, 1<<1000] - s_bad_values = l_bad_values + [_testcapi.INT_MIN - 1, - _testcapi.INT_MAX + 1] - s_deprecated_values = [1<<16, _testcapi.INT_MAX] + s_bad_values = ( + l_bad_values + + [_testcapi.INT_MIN-1, _testcapi.INT_MAX+1] + + [1 << 16, _testcapi.INT_MAX] + ) for k in s_good_values: socket.ntohs(k) socket.htons(k) @@ -1136,9 +1138,6 @@ def testNtoHErrors(self): for k in l_bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.htonl, k) - for k in s_deprecated_values: - self.assertWarns(DeprecationWarning, socket.ntohs, k) - self.assertWarns(DeprecationWarning, socket.htons, k) def testGetServBy(self): eq = self.assertEqual diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index bb33db0fa47b8c..293f331a4b3a9f 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6102,13 +6102,10 @@ socket_ntohs(PyObject *self, PyObject *args) return NULL; } if (x > 0xffff) { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "ntohs: Python int too large to convert to C " - "16-bit unsigned integer (The silent truncation " - "is deprecated)", - 1)) { - return NULL; - } + PyErr_SetString(PyExc_OverflowError, + "ntohs: Python int too large to convert to C " + "16-bit unsigned integer"); + return NULL; } return PyLong_FromUnsignedLong(ntohs((unsigned short)x)); } @@ -6117,11 +6114,8 @@ PyDoc_STRVAR(ntohs_doc, "ntohs(integer) -> integer\n\ \n\ Convert a 16-bit unsigned integer from network to host byte order.\n\ -Note that in case the received integer does not fit in 16-bit unsigned\n\ -integer, but does fit in a positive C int, it is silently truncated to\n\ -16-bit unsigned integer.\n\ -However, this silent truncation feature is deprecated, and will raise an\n\ -exception in future versions of Python."); +An OverflowError is raised if the received integer does not fit in\n\ +a 16-bit unsigned integer, but does fit in a positive C int."); static PyObject * @@ -6173,13 +6167,10 @@ socket_htons(PyObject *self, PyObject *args) return NULL; } if (x > 0xffff) { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "htons: Python int too large to convert to C " - "16-bit unsigned integer (The silent truncation " - "is deprecated)", - 1)) { - return NULL; - } + PyErr_SetString(PyExc_OverflowError, + "htons: Python int too large to convert to C " + "16-bit unsigned integer"); + return NULL; } return PyLong_FromUnsignedLong(htons((unsigned short)x)); } @@ -6188,11 +6179,8 @@ PyDoc_STRVAR(htons_doc, "htons(integer) -> integer\n\ \n\ Convert a 16-bit unsigned integer from host to network byte order.\n\ -Note that in case the received integer does not fit in 16-bit unsigned\n\ -integer, but does fit in a positive C int, it is silently truncated to\n\ -16-bit unsigned integer.\n\ -However, this silent truncation feature is deprecated, and will raise an\n\ -exception in future versions of Python."); +An OverflowError is raised if the received integer does not fit in\n\ +a 16-bit unsigned integer, but does fit in a positive C int."); static PyObject * From a2a439a79acee691d9b765bc344d0b6c08eb623f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 17 Nov 2020 22:06:28 +0100 Subject: [PATCH 2/6] Add NEWS --- .../next/Library/2020-11-17-22-06-15.bpo-42393.BB0oXc.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-11-17-22-06-15.bpo-42393.BB0oXc.rst diff --git a/Misc/NEWS.d/next/Library/2020-11-17-22-06-15.bpo-42393.BB0oXc.rst b/Misc/NEWS.d/next/Library/2020-11-17-22-06-15.bpo-42393.BB0oXc.rst new file mode 100644 index 00000000000000..f291123d6dea98 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-11-17-22-06-15.bpo-42393.BB0oXc.rst @@ -0,0 +1,3 @@ +Raise :exc:`OverflowError` instead of silent truncation in :meth:`socket.ntohs` +and :meth:`socket.htons`. Silent truncation was deprecated in Python 3.7. +Patch by Erlend E. Aasland From 107f2963c68732155f6b662dc274d9739bd1274f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 17 Nov 2020 22:11:43 +0100 Subject: [PATCH 3/6] Add "What's new" --- Doc/whatsnew/3.10.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index b5fb1e9a629c1c..19c8a51f175b08 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -537,6 +537,12 @@ Changes in the Python API silently in Python 3.9. (Contributed by Ken Jin in :issue:`42195`.) +* :meth:`socket.htons` and :meth:`socket.ntohs` now raise :exc:`OverflowError` + instead of :exc:`DeprecationWarning` if the given parameter will not fit in + a 16-bit unsigned integer. + (Contributed by Erlend E. Aasland in :issue:`42393`.) + + CPython bytecode changes ======================== From 49e0387f9c2db702669874e925d5135d9b4c9936 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 29 Dec 2020 23:40:55 +0100 Subject: [PATCH 4/6] Address review: Don't mention OverflowError in docstrings --- Modules/socketmodule.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 293f331a4b3a9f..c686286d779dcc 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6113,9 +6113,7 @@ socket_ntohs(PyObject *self, PyObject *args) PyDoc_STRVAR(ntohs_doc, "ntohs(integer) -> integer\n\ \n\ -Convert a 16-bit unsigned integer from network to host byte order.\n\ -An OverflowError is raised if the received integer does not fit in\n\ -a 16-bit unsigned integer, but does fit in a positive C int."); +Convert a 16-bit unsigned integer from network to host byte order."); static PyObject * @@ -6178,9 +6176,7 @@ socket_htons(PyObject *self, PyObject *args) PyDoc_STRVAR(htons_doc, "htons(integer) -> integer\n\ \n\ -Convert a 16-bit unsigned integer from host to network byte order.\n\ -An OverflowError is raised if the received integer does not fit in\n\ -a 16-bit unsigned integer, but does fit in a positive C int."); +Convert a 16-bit unsigned integer from host to network byte order."); static PyObject * From 3a8dc4e58ad3630003ec8972ec4b155f2046f4f3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 29 Dec 2020 23:43:12 +0100 Subject: [PATCH 5/6] Address review: Don't mention OverflowError in the htons/ntohs documentation --- Doc/library/socket.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index 19587d86aaf0a5..98406842cca6d6 100755 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -905,8 +905,7 @@ The :mod:`socket` module also offers various network-related services: Convert 16-bit positive integers from network to host byte order. On machines where the host byte order is the same as network byte order, this is a no-op; - otherwise, it performs a 2-byte swap operation. :exc:`OverflowError` is raised - if *x* does not fit in a 16-bit unsigned integer. + otherwise, it performs a 2-byte swap operation. .. function:: htonl(x) @@ -920,8 +919,7 @@ The :mod:`socket` module also offers various network-related services: Convert 16-bit positive integers from host to network byte order. On machines where the host byte order is the same as network byte order, this is a no-op; - otherwise, it performs a 2-byte swap operation. :exc:`OverflowError` is raised - if *x* does not fit in a 16-bit unsigned integer. + otherwise, it performs a 2-byte swap operation. .. function:: inet_aton(ip_string) From 5f898882909e3a4c293ffb4be4a298074d600507 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 29 Dec 2020 23:51:07 +0100 Subject: [PATCH 6/6] Address review: Add version changed --- Doc/library/socket.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index 98406842cca6d6..2255b827aa8eb1 100755 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -907,6 +907,10 @@ The :mod:`socket` module also offers various network-related services: where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 2-byte swap operation. + .. versionchanged:: 3.10 + Raises :exc:`OverflowError` if *x* does not fit in a 16-bit unsigned + integer. + .. function:: htonl(x) @@ -921,6 +925,10 @@ The :mod:`socket` module also offers various network-related services: where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 2-byte swap operation. + .. versionchanged:: 3.10 + Raises :exc:`OverflowError` if *x* does not fit in a 16-bit unsigned + integer. + .. function:: inet_aton(ip_string)