Mercurial > cpython-lru_cache-weakref
diff Modules/socketmodule.c @ 24293:640370f08e2a legacy-trunk
Add default timeout functionality. This adds setdefaulttimeout() and
getdefaulttimeout() functions to the socket and _socket modules, and
appropriate tests.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 18 Jul 2002 17:08:35 +0000 |
| parents | 1598a1e473e2 |
| children | 93434bc0899d |
line wrap: on
line diff
--- a/Modules/socketmodule.c Thu Jul 18 15:53:32 2002 +0000 +++ b/Modules/socketmodule.c Thu Jul 18 17:08:35 2002 +0000 @@ -35,6 +35,8 @@ - socket.AF_INET, socket.SOCK_STREAM, etc.: constants from <socket.h> - 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 @@ /* 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 @@ 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 @@ \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 @@ 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 */ };
