diff -Nurp Python-3.2.2/Doc/library/socket.rst Python-3.2.2_new/Doc/library/socket.rst --- Python-3.2.2/Doc/library/socket.rst 2011-09-03 20:16:39.000000000 +0400 +++ Python-3.2.2_new/Doc/library/socket.rst 2011-10-03 21:49:06.000000000 +0400 @@ -618,16 +618,19 @@ correspond to Unix system calls applicab the address family --- see above.) -.. method:: socket.getsockopt(level, optname[, buflen]) +.. method:: socket.getsockopt(level, optname[, buflen[, buffer]]) Return the value of the given socket option (see the Unix man page :manpage:`getsockopt(2)`). The needed symbolic constants (:const:`SO_\*` etc.) - are defined in this module. If *buflen* is absent, an integer option is assumed - and its integer value is returned by the function. If *buflen* is present, it + are defined in this module. If *buffer* is present and is a bytes object, it + is passed to getsockopt and the return value is a bytes object of the same + length. If *buffer* is absent and *buflen* is an integer, then *buflen* specifies the maximum length of the buffer used to receive the option in, and - this buffer is returned as a bytes object. It is up to the caller to decode the - contents of the buffer (see the optional built-in module :mod:`struct` for a way - to decode C structures encoded as byte strings). + this buffer is returned as a bytes object. If *buflen* is absent, an integer + option is assumed and its integer value is returned by the function. It is up + to the caller to encode and decode the contents of the buffer (see the optional + built-in module :mod:`struct` for a way to decode C structures encoded as byte + strings). .. method:: socket.gettimeout() diff -Nurp Python-3.2.2/Modules/socketmodule.c Python-3.2.2_new/Modules/socketmodule.c --- Python-3.2.2/Modules/socketmodule.c 2011-09-03 20:16:47.000000000 +0400 +++ Python-3.2.2_new/Modules/socketmodule.c 2011-10-03 22:09:51.000000000 +0400 @@ -118,7 +118,7 @@ _dup() -- return a new socket fd duplica fileno() -- return underlying file descriptor\n\ getpeername() -- return remote address [*]\n\ getsockname() -- return local address\n\ -getsockopt(level, optname[, buflen]) -- get socket options\n\ +getsockopt(level, optname[, buflen[, buffer]]) -- get socket options\n\ gettimeout() -- return timeout or None\n\ listen(n) -- start listening for incoming connections\n\ recv(buflen[, flags]) -- receive data\n\ @@ -1854,8 +1854,10 @@ The value argument can either be an inte /* s.getsockopt() method. With two arguments, retrieves an integer option. - With a third integer argument, retrieves a string buffer of that size; - use optional built-in module 'struct' to decode the string. */ + With a third integer argument, retrieves a string buffer of that size. + With a fourth argument as a bytes object, retrieves a buffer of the same + size, using the argument as an input; + use optional built-in module 'struct' to encode and decode the string. */ static PyObject * sock_getsockopt(PySocketSockObject *s, PyObject *args) @@ -1864,12 +1866,16 @@ sock_getsockopt(PySocketSockObject *s, P int optname; int res; PyObject *buf; - socklen_t buflen = 0; + socklen_t buflen = 0, inputlen = 0; + char *inputbuf = (char *)NULL; - if (!PyArg_ParseTuple(args, "ii|i:getsockopt", - &level, &optname, &buflen)) + if (!PyArg_ParseTuple(args, "ii|iy#:getsockopt", + &level, &optname, &buflen, &inputbuf, &inputlen)) return NULL; + if (inputlen != 0) + buflen = inputlen; + if (buflen == 0) { int flag = 0; socklen_t flagsize = sizeof flag; @@ -1890,7 +1896,7 @@ sock_getsockopt(PySocketSockObject *s, P "getsockopt buflen out of range"); return NULL; } - buf = PyBytes_FromStringAndSize((char *)NULL, buflen); + buf = PyBytes_FromStringAndSize(inputbuf, buflen); if (buf == NULL) return NULL; res = getsockopt(s->sock_fd, level, optname, @@ -1904,11 +1910,15 @@ sock_getsockopt(PySocketSockObject *s, P } PyDoc_STRVAR(getsockopt_doc, -"getsockopt(level, option[, buffersize]) -> value\n\ +"getsockopt(level, option[, buflen[, buffer]]) -> value\n\ \n\ Get a socket option. See the Unix manual for level and option.\n\ -If a nonzero buffersize argument is given, the return value is a\n\ -string of that length; otherwise it is an integer."); +If buffer argument is given and is a non-empty bytes object,\n\ +it is passed to getsockopt and the return value is a bytes object\n\ +of the same length.\n\ +Otherwise, if a nonzero integer buflen argument is given,\n\ +the return value is a string of that length.\n\ +Otherwise the return value is an integer."); /* s.bind(sockaddr) method */