From 9d0c8cee660030391e7017674f176fa2b496adde Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 18 Jul 2002 17:08:35 +0000 Subject: Add default timeout functionality. This adds setdefaulttimeout() and getdefaulttimeout() functions to the socket and _socket modules, and appropriate tests. --- Modules/socketmodule.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) (limited to 'Modules') diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 5314dcfc21..9a3951681d 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -35,6 +35,8 @@ Module interface: - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from - socket.inet_aton(IP address) -> 32-bit packed IP representation - socket.inet_ntoa(packed IP) -> IP address string +- socket.getdefaulttimeout() -> None | float +- socket.setdefaulttimeout(None | float) - an Internet socket address is a pair (hostname, port) where hostname can be anything recognized by gethostbyname() (including the dd.dd.dd.dd notation) and port is in host byte order @@ -521,6 +523,8 @@ internal_select(PySocketSockObject *s, int writing) /* Initialize a new socket object. */ +static float defaulttimeout = -1.0; /* Default timeout for new sockets */ + static void init_sockobject(PySocketSockObject *s, SOCKET_T fd, int family, int type, int proto) @@ -532,9 +536,13 @@ init_sockobject(PySocketSockObject *s, s->sock_family = family; s->sock_type = type; s->sock_proto = proto; - s->sock_timeout = -1.0; /* Start without timeout */ + s->sock_timeout = defaulttimeout; s->errorhandler = &set_error; + + if (defaulttimeout >= 0.0) + internal_setblocking(s, 0); + #ifdef RISCOS if (taskwindow) socketioctl(s->sock_fd, 0x80046679, (u_long*)&block); @@ -2725,6 +2733,58 @@ PyDoc_STRVAR(getnameinfo_doc, \n\ Get host and port for a sockaddr."); + +/* Python API to getting and setting the default timeout value. */ + +static PyObject * +socket_getdefaulttimeout(PyObject *self) +{ + if (defaulttimeout < 0.0) { + Py_INCREF(Py_None); + return Py_None; + } + else + return PyFloat_FromDouble(defaulttimeout); +} + +PyDoc_STRVAR(getdefaulttimeout_doc, +"socket.getdefaulttimeout() -> None | float\n\ +\n\ +Returns the default timeout in floating seconds for new socket objects.\n\ +A value of None indicates that new socket objects have no timeout.\n\ +When the socket module is first imported, the default is None."); + +static PyObject * +socket_setdefaulttimeout(PyObject *self, PyObject *arg) +{ + double timeout; + + if (arg == Py_None) + timeout = -1.0; + else { + timeout = PyFloat_AsDouble(arg); + if (timeout < 0.0) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_ValueError, + "Timeout value out of range"); + return NULL; + } + } + + defaulttimeout = timeout; + + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(setdefaulttimeout_doc, +"socket.setdefaulttimeout(None | float)\n\ +\n\ +Set the default timeout in floating seconds for new socket objects.\n\ +A value of None indicates that new socket objects have no timeout.\n\ +When the socket module is first imported, the default is None."); + + /* List of functions exported by this module. */ static PyMethodDef socket_methods[] = { @@ -2760,6 +2820,10 @@ static PyMethodDef socket_methods[] = { METH_VARARGS, getaddrinfo_doc}, {"getnameinfo", socket_getnameinfo, METH_VARARGS, getnameinfo_doc}, + {"getdefaulttimeout", socket_getdefaulttimeout, + METH_NOARGS, getdefaulttimeout_doc}, + {"setdefaulttimeout", socket_setdefaulttimeout, + METH_O, setdefaulttimeout_doc}, {NULL, NULL} /* Sentinel */ }; -- cgit v1.2.1