[Python-checkins] python/dist/src/Modules socketmodule.c,1.230,1.231

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Thu, 18 Jul 2002 10:08:37 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv8306/Modules

Modified Files:
	socketmodule.c 
Log Message:
Add default timeout functionality.  This adds setdefaulttimeout() and
getdefaulttimeout() functions to the socket and _socket modules, and
appropriate tests.


Index: socketmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/socketmodule.c,v
retrieving revision 1.230
retrieving revision 1.231
diff -C2 -d -r1.230 -r1.231
*** socketmodule.c	17 Jul 2002 16:30:37 -0000	1.230
--- socketmodule.c	18 Jul 2002 17:08:34 -0000	1.231
***************
*** 36,39 ****
--- 36,41 ----
  - 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()
***************
*** 522,525 ****
--- 524,529 ----
  /* Initialize a new socket object. */
  
+ static float defaulttimeout = -1.0; /* Default timeout for new sockets */
+ 
  static void
  init_sockobject(PySocketSockObject *s,
***************
*** 533,539 ****
  	s->sock_type = type;
  	s->sock_proto = proto;
! 	s->sock_timeout = -1.0; /* Start without timeout */
  
  	s->errorhandler = &set_error;
  #ifdef RISCOS
  	if (taskwindow)
--- 537,547 ----
  	s->sock_type = type;
  	s->sock_proto = proto;
! 	s->sock_timeout = defaulttimeout;
  
  	s->errorhandler = &set_error;
+ 
+ 	if (defaulttimeout >= 0.0)
+ 		internal_setblocking(s, 0);
+ 
  #ifdef RISCOS
  	if (taskwindow)
***************
*** 2726,2729 ****
--- 2734,2789 ----
  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. */
  
***************
*** 2761,2764 ****
--- 2821,2828 ----
  	{"getnameinfo",		socket_getnameinfo,
  	 METH_VARARGS, getnameinfo_doc},
+ 	{"getdefaulttimeout",	socket_getdefaulttimeout,
+ 	 METH_NOARGS, getdefaulttimeout_doc},
+ 	{"setdefaulttimeout",	socket_setdefaulttimeout,
+ 	 METH_O, setdefaulttimeout_doc},
  	{NULL,			NULL}		 /* Sentinel */
  };