diff options
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r-- | Modules/socketmodule.c | 94 |
1 files changed, 58 insertions, 36 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index f4edc062fd..8d0f9e6df8 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -1218,8 +1218,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) { if (addrlen == 0) { /* No address -- may be recvfrom() from known socket */ - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } switch (addr->sa_family) { @@ -2540,8 +2539,7 @@ static PyObject * sock_gettimeout(PySocketSockObject *s) { if (s->sock_timeout < 0) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } else { double seconds = _PyTime_AsSecondsDouble(s->sock_timeout); @@ -2701,8 +2699,7 @@ sock_bind(PySocketSockObject *s, PyObject *addro) Py_END_ALLOW_THREADS if (res < 0) return s->errorhandler(); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } PyDoc_STRVAR(bind_doc, @@ -2738,8 +2735,7 @@ sock_close(PySocketSockObject *s) return s->errorhandler(); } } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } PyDoc_STRVAR(close_doc, @@ -2996,8 +2992,7 @@ sock_listen(PySocketSockObject *s, PyObject *args) Py_END_ALLOW_THREADS if (res < 0) return s->errorhandler(); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } PyDoc_STRVAR(listen_doc, @@ -3863,11 +3858,15 @@ sock_sendto(PySocketSockObject *s, PyObject *args) arglen = PyTuple_Size(args); switch (arglen) { case 2: - PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro); + if (!PyArg_ParseTuple(args, "y*O:sendto", &pbuf, &addro)) { + return NULL; + } break; case 3: - PyArg_ParseTuple(args, "y*iO:sendto", - &pbuf, &flags, &addro); + if (!PyArg_ParseTuple(args, "y*iO:sendto", + &pbuf, &flags, &addro)) { + return NULL; + } break; default: PyErr_Format(PyExc_TypeError, @@ -3875,8 +3874,6 @@ sock_sendto(PySocketSockObject *s, PyObject *args) arglen); return NULL; } - if (PyErr_Occurred()) - return NULL; if (!IS_SELECTABLE(s)) { PyBuffer_Release(&pbuf); @@ -4361,8 +4358,7 @@ sock_shutdown(PySocketSockObject *s, PyObject *arg) Py_END_ALLOW_THREADS if (res < 0) return s->errorhandler(); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } PyDoc_STRVAR(shutdown_doc, @@ -5507,24 +5503,38 @@ AF_UNIX if defined on the platform; otherwise, the default is AF_INET."); static PyObject * socket_ntohs(PyObject *self, PyObject *args) { - int x1, x2; + int x; - if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { + if (!PyArg_ParseTuple(args, "i:ntohs", &x)) { return NULL; } - if (x1 < 0) { + if (x < 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); - return PyLong_FromLong(x2); + 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; + } + } + return PyLong_FromUnsignedLong(ntohs((unsigned short)x)); } 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 * @@ -5564,24 +5574,38 @@ Convert a 32-bit integer from network to host byte order."); static PyObject * socket_htons(PyObject *self, PyObject *args) { - int x1, x2; + int x; - if (!PyArg_ParseTuple(args, "i:htons", &x1)) { + if (!PyArg_ParseTuple(args, "i:htons", &x)) { return NULL; } - if (x1 < 0) { + if (x < 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); - return PyLong_FromLong(x2); + 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; + } + } + return PyLong_FromUnsignedLong(htons((unsigned short)x)); } 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 * @@ -5954,7 +5978,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) PyObject *hobj = NULL; PyObject *pobj = (PyObject *)NULL; char pbuf[30]; - char *hptr, *pptr; + const char *hptr, *pptr; int family, socktype, protocol, flags; int error; PyObject *all = (PyObject *)NULL; @@ -6166,8 +6190,7 @@ static PyObject * socket_getdefaulttimeout(PyObject *self) { if (defaulttimeout < 0) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } else { double seconds = _PyTime_AsSecondsDouble(defaulttimeout); @@ -6192,8 +6215,7 @@ socket_setdefaulttimeout(PyObject *self, PyObject *arg) defaulttimeout = timeout; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } PyDoc_STRVAR(setdefaulttimeout_doc, |