[Python-checkins] python/dist/src/Modules socketmodule.c, 1.298, 1.299

davecole at users.sourceforge.net davecole at users.sourceforge.net
Mon Aug 9 06:51:43 CEST 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20818/Modules

Modified Files:
	socketmodule.c 
Log Message:
Patch #1003700: Add socketpair function to socket module.


Index: socketmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/socketmodule.c,v
retrieving revision 1.298
retrieving revision 1.299
diff -C2 -d -r1.298 -r1.299
*** socketmodule.c	3 Aug 2004 08:52:45 -0000	1.298
--- socketmodule.c	9 Aug 2004 04:51:41 -0000	1.299
***************
*** 29,32 ****
--- 29,33 ----
  - socket.getservbyport(portnumber[, protocolname]) --> service name
  - socket.socket([family[, type [, proto]]]) --> new socket object
+ - socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
  - socket.ntohs(16 bit value) --> new int object
  - socket.ntohl(32 bit value) --> new int object
***************
*** 3010,3013 ****
--- 3011,3071 ----
  
  
+ #ifdef HAVE_SOCKETPAIR
+ /* Create a pair of sockets using the socketpair() function.
+    Arguments as for socket(). */
+ 
+ /*ARGSUSED*/
+ static PyObject *
+ socket_socketpair(PyObject *self, PyObject *args)
+ {
+ 	PySocketSockObject *s0 = NULL, *s1 = NULL;
+ 	SOCKET_T sv[2];
+ 	int family, type = SOCK_STREAM, proto = 0;
+ 	PyObject *res = NULL;
+ 
+ #if defined(AF_UNIX)
+ 	family = AF_UNIX;
+ #else
+ 	family = AF_INET;
+ #endif
+ 	if (!PyArg_ParseTuple(args, "|iii:socketpair",
+ 			      &family, &type, &proto))
+ 		return NULL;
+ 	/* Create a pair of socket fds */
+ 	if (socketpair(family, type, proto, sv) < 0)
+ 		return set_error();
+ #ifdef SIGPIPE
+ 	(void) signal(SIGPIPE, SIG_IGN);
+ #endif
+ 	s0 = new_sockobject(sv[0], family, type, proto);
+ 	if (s0 == NULL)
+ 		goto finally;
+ 	s1 = new_sockobject(sv[1], family, type, proto);
+ 	if (s1 == NULL)
+ 		goto finally;
+ 	res = PyTuple_Pack(2, s0, s1);
+ 
+ finally:
+ 	if (res == NULL) {
+ 		if (s0 == NULL)
+ 			SOCKETCLOSE(sv[0]);
+ 		if (s1 == NULL)
+ 			SOCKETCLOSE(sv[1]);
+ 	}
+ 	Py_XDECREF(s0);
+ 	Py_XDECREF(s1);
+ 	return res;
+ }
+ 
+ PyDoc_STRVAR(socketpair_doc,
+ "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
+ \n\
+ Create a pair of socket objects from the sockets returned by the platform\n\
+ socketpair() function.\n\
+ The arguments are the same as for socket().");
+ 
+ #endif /* HAVE_SOCKETPAIR */
+ 
+ 
  #ifndef NO_DUP
  /* Create a socket object from a numeric file description.
***************
*** 3609,3612 ****
--- 3667,3674 ----
  	 METH_VARARGS, fromfd_doc},
  #endif
+ #ifdef HAVE_SOCKETPAIR
+ 	{"socketpair",		socket_socketpair,
+ 	 METH_VARARGS, socketpair_doc},
+ #endif
  	{"ntohs",		socket_ntohs,
  	 METH_VARARGS, ntohs_doc},



More information about the Python-checkins mailing list