@@ -28,6 +28,7 @@ Module interface:
2828- socket.getservbyname(servicename[, protocolname]) --> port number
2929- socket.getservbyport(portnumber[, protocolname]) --> service name
3030- socket.socket([family[, type [, proto]]]) --> new socket object
31+ - socket.socketpair([family[, type [, proto]]]) --> (socket, socket)
3132- socket.ntohs(16 bit value) --> new int object
3233- socket.ntohl(32 bit value) --> new int object
3334- socket.htons(16 bit value) --> new int object
@@ -3009,6 +3010,63 @@ PyDoc_STRVAR(getprotobyname_doc,
30093010Return the protocol number for the named protocol. (Rarely used.)" );
30103011
30113012
3013+ #ifdef HAVE_SOCKETPAIR
3014+ /* Create a pair of sockets using the socketpair() function.
3015+ Arguments as for socket(). */
3016+
3017+ /*ARGSUSED*/
3018+ static PyObject *
3019+ socket_socketpair (PyObject * self , PyObject * args )
3020+ {
3021+ PySocketSockObject * s0 = NULL , * s1 = NULL ;
3022+ SOCKET_T sv [2 ];
3023+ int family , type = SOCK_STREAM , proto = 0 ;
3024+ PyObject * res = NULL ;
3025+
3026+ #if defined(AF_UNIX )
3027+ family = AF_UNIX ;
3028+ #else
3029+ family = AF_INET ;
3030+ #endif
3031+ if (!PyArg_ParseTuple (args , "|iii:socketpair" ,
3032+ & family , & type , & proto ))
3033+ return NULL ;
3034+ /* Create a pair of socket fds */
3035+ if (socketpair (family , type , proto , sv ) < 0 )
3036+ return set_error ();
3037+ #ifdef SIGPIPE
3038+ (void ) signal (SIGPIPE , SIG_IGN );
3039+ #endif
3040+ s0 = new_sockobject (sv [0 ], family , type , proto );
3041+ if (s0 == NULL )
3042+ goto finally ;
3043+ s1 = new_sockobject (sv [1 ], family , type , proto );
3044+ if (s1 == NULL )
3045+ goto finally ;
3046+ res = PyTuple_Pack (2 , s0 , s1 );
3047+
3048+ finally :
3049+ if (res == NULL ) {
3050+ if (s0 == NULL )
3051+ SOCKETCLOSE (sv [0 ]);
3052+ if (s1 == NULL )
3053+ SOCKETCLOSE (sv [1 ]);
3054+ }
3055+ Py_XDECREF (s0 );
3056+ Py_XDECREF (s1 );
3057+ return res ;
3058+ }
3059+
3060+ PyDoc_STRVAR (socketpair_doc ,
3061+ "socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\
3062+ \n\
3063+ Create a pair of socket objects from the sockets returned by the platform\n\
3064+ socketpair() function.\n\
3065+ The arguments are the same as for socket()." );
3066+
3067+ #endif /* HAVE_SOCKETPAIR */
3068+
3069+
30123070#ifndef NO_DUP
30133071/* Create a socket object from a numeric file description.
30143072 Useful e.g. if stdin is a socket.
@@ -3607,6 +3665,10 @@ static PyMethodDef socket_methods[] = {
36073665#ifndef NO_DUP
36083666 {"fromfd" , socket_fromfd ,
36093667 METH_VARARGS , fromfd_doc },
3668+ #endif
3669+ #ifdef HAVE_SOCKETPAIR
3670+ {"socketpair" , socket_socketpair ,
3671+ METH_VARARGS , socketpair_doc },
36103672#endif
36113673 {"ntohs" , socket_ntohs ,
36123674 METH_VARARGS , ntohs_doc },
0 commit comments