[Python-checkins] r43126 - in python/trunk: Doc/lib/libsocket.tex Lib/socket.py Lib/test/test_socket.py Misc/NEWS Modules/socketmodule.c
Guido van Rossum
guido at python.org
Mon Mar 20 23:54:23 CET 2006
Why are these accessor methods? A more Pythonic API would make them
read-only attributes, like the various attributes of file objects.
--Guido
On 3/17/06, georg.brandl <python-checkins at python.org> wrote:
> Author: georg.brandl
> Date: Fri Mar 17 20:17:34 2006
> New Revision: 43126
>> Modified:
> python/trunk/Doc/lib/libsocket.tex
> python/trunk/Lib/socket.py
> python/trunk/Lib/test/test_socket.py
> python/trunk/Misc/NEWS
> python/trunk/Modules/socketmodule.c
> Log:
> RFE #567972: Socket objects' family, type and proto properties are
> now exposed via new get...() methods.
>>> Modified: python/trunk/Doc/lib/libsocket.tex
> ==============================================================================
> --- python/trunk/Doc/lib/libsocket.tex (original)
> +++ python/trunk/Doc/lib/libsocket.tex Fri Mar 17 20:17:34 2006
> @@ -626,7 +626,7 @@
> \end{methoddesc}
>> \begin{methoddesc}[socket]{gettimeout}{}
> -Returns the timeout in floating seconds associated with socket
> +Return the timeout in floating seconds associated with socket
> operations, or \code{None} if no timeout is set. This reflects
> the last call to \method{setblocking()} or \method{settimeout()}.
> \versionadded{2.3}
> @@ -654,6 +654,21 @@
> setting, and in general it is recommended to call
> \method{settimeout()} before calling \method{connect()}.
>> +\begin{methoddesc}[socket]{getfamily}{}
> +Return the socket family, as given to the \class{socket} constructor.
> +\versionadded{2.5}
> +\end{methoddesc}
> +
> +\begin{methoddesc}[socket]{gettype}{}
> +Return the socket type, as given to the \class{socket} constructor.
> +\versionadded{2.5}
> +\end{methoddesc}
> +
> +\begin{methoddesc}[socket]{getproto}{}
> +Return the socket protocol, as given to the \class{socket} constructor.
> +\versionadded{2.5}
> +\end{methoddesc}
> +
> \begin{methoddesc}[socket]{setsockopt}{level, optname, value}
> Set the value of the given socket option (see the \UNIX{} manual page
> \manpage{setsockopt}{2}). The needed symbolic constants are defined in
>> Modified: python/trunk/Lib/socket.py
> ==============================================================================
> --- python/trunk/Lib/socket.py (original)
> +++ python/trunk/Lib/socket.py Fri Mar 17 20:17:34 2006
> @@ -183,6 +183,24 @@
> and bufsize arguments are as for the built-in open() function."""
> return _fileobject(self._sock, mode, bufsize)
>> + def getfamily(self):
> + """getfamily() -> socket family
> +
> + Return the socket family."""
> + return self._sock.family
> +
> + def gettype(self):
> + """gettype() -> socket type
> +
> + Return the socket type."""
> + return self._sock.type
> +
> + def getproto(self):
> + """getproto() -> socket protocol
> +
> + Return the socket protocol."""
> + return self._sock.proto
> +
> _s = ("def %s(self, *args): return self._sock.%s(*args)\n\n"
> "%s.__doc__ = _realsocket.%s.__doc__\n")
> for _m in _socketmethods:
>> Modified: python/trunk/Lib/test/test_socket.py
> ==============================================================================
> --- python/trunk/Lib/test/test_socket.py (original)
> +++ python/trunk/Lib/test/test_socket.py Fri Mar 17 20:17:34 2006
> @@ -469,6 +469,14 @@
> sock.close()
> self.assertRaises(socket.error, sock.send, "spam")
>> + def testNewGetMethods(self):
> + # testing getfamily(), gettype() and getprotocol()
> + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> + self.assertEqual(sock.getfamily(), socket.AF_INET)
> + self.assertEqual(sock.gettype(), socket.SOCK_STREAM)
> + self.assertEqual(sock.getproto(), 0)
> + sock.close()
> +
> class BasicTCPTest(SocketConnectedTest):
>> def __init__(self, methodName='runTest'):
>> Modified: python/trunk/Misc/NEWS
> ==============================================================================
> --- python/trunk/Misc/NEWS (original)
> +++ python/trunk/Misc/NEWS Fri Mar 17 20:17:34 2006
> @@ -291,6 +291,9 @@
> Extension Modules
> -----------------
>> +- RFE #567972: Socket objects' family, type and proto properties are
> + now exposed via new get...() methods.
> +
> - Everything under lib-old was removed. This includes the following modules:
> Para, addpack, cmp, cmpcache, codehack, dircmp, dump, find, fmt, grep,
> lockfile, newdir, ni, packmail, poly, rand, statcache, tb, tzparse,
>> Modified: python/trunk/Modules/socketmodule.c
> ==============================================================================
> --- python/trunk/Modules/socketmodule.c (original)
> +++ python/trunk/Modules/socketmodule.c Fri Mar 17 20:17:34 2006
> @@ -62,6 +62,7 @@
> */
>> #include "Python.h"
> +#include "structmember.h"
>> #undef MAX
> #define MAX(x, y) ((x) < (y) ? (y) : (x))
> @@ -2502,6 +2503,14 @@
> {NULL, NULL} /* sentinel */
> };
>> +/* SockObject members */
> +static PyMemberDef sock_memberlist[] = {
> + {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},
> + {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},
> + {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},
> + {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},
> + {0},
> +};
>> /* Deallocate a socket object in response to the last Py_DECREF().
> First close the file description. */
> @@ -2625,7 +2634,7 @@
> 0, /* tp_iter */
> 0, /* tp_iternext */
> sock_methods, /* tp_methods */
> - 0, /* tp_members */
> + sock_memberlist, /* tp_members */
> 0, /* tp_getset */
> 0, /* tp_base */
> 0, /* tp_dict */
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-checkins
mailing list