diff -r 5673cf852daa Doc/library/socket.rst --- a/Doc/library/socket.rst Sat Oct 01 01:06:51 2016 -0500 +++ b/Doc/library/socket.rst Sun Oct 02 09:13:38 2016 +0300 @@ -664,6 +664,12 @@ 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. + .. function:: htonl(x) @@ -678,6 +684,12 @@ 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. + .. function:: inet_aton(ip_string) diff -r 5673cf852daa Lib/test/test_socket.py --- a/Lib/test/test_socket.py Sat Oct 01 01:06:51 2016 -0500 +++ b/Lib/test/test_socket.py Sun Oct 02 09:13:38 2016 +0300 @@ -888,18 +888,28 @@ self.assertRaises(OverflowError, func, 1<<34) def testNtoHErrors(self): - good_values = [ 1, 2, 3, 1, 2, 3 ] - bad_values = [ -1, -2, -3, -1, -2, -3 ] - for k in good_values: + import _testcapi + s_good_values = [ 0, 1, 2, 0xffff ] + l_good_values = [ 0, 1, 2, 0xffffffff ] + s_bad_values = [ -1, -2, -3, _testcapi.INT_MIN - 1, + _testcapi.INT_MAX + 1, 2 ** 1000] + l_bad_values = [ -1, -2, 2 ** 32, _testcapi.ULONG_MAX + 1, 2 ** 1000 ] + s_deprecated_values = [ 2 ** 16, _testcapi.INT_MAX ] + for k in s_good_values: + socket.ntohs(k) + socket.htons(k) + for k in l_good_values: socket.ntohl(k) - socket.ntohs(k) socket.htonl(k) - socket.htons(k) - for k in bad_values: + for k in s_bad_values: + self.assertRaises(OverflowError, socket.ntohs, k) + self.assertRaises(OverflowError, socket.htons, k) + for k in l_bad_values: self.assertRaises(OverflowError, socket.ntohl, k) - self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) - self.assertRaises(OverflowError, socket.htons, 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 -r 5673cf852daa Modules/socketmodule.c --- a/Modules/socketmodule.c Sat Oct 01 01:06:51 2016 -0500 +++ b/Modules/socketmodule.c Sun Oct 02 09:13:38 2016 +0300 @@ -5498,24 +5498,40 @@ static PyObject * socket_ntohs(PyObject *self, PyObject *args) { - int x1, x2; + int x1; + long x2; if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { return NULL; } if (x1 < 0) { PyErr_SetString(PyExc_OverflowError, - "can't convert negative number to unsigned long"); + "ntohs: can't convert negative Python int to C " + "16-bit unsigned integer"); return NULL; } - x2 = (unsigned int)ntohs((unsigned short)x1); + if (x1 > 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; + } + } + x2 = (long)ntohs((unsigned short)x1); return PyLong_FromLong(x2); } PyDoc_STRVAR(ntohs_doc, "ntohs(integer) -> integer\n\ \n\ -Convert a 16-bit integer from network to host byte order."); +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."); static PyObject * @@ -5555,24 +5571,40 @@ static PyObject * socket_htons(PyObject *self, PyObject *args) { - int x1, x2; + int x1; + long x2; if (!PyArg_ParseTuple(args, "i:htons", &x1)) { return NULL; } if (x1 < 0) { PyErr_SetString(PyExc_OverflowError, - "can't convert negative number to unsigned long"); + "htons: can't convert negative Python int to C " + "16-bit unsigned integer"); return NULL; } - x2 = (unsigned int)htons((unsigned short)x1); + if (x1 > 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; + } + } + x2 = (long)htons((unsigned short)x1); return PyLong_FromLong(x2); } PyDoc_STRVAR(htons_doc, "htons(integer) -> integer\n\ \n\ -Convert a 16-bit integer from host to network byte order."); +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."); static PyObject *