(original) (raw)

changeset: 80863:d6f61cd364d9 user: Antoine Pitrou solipsis@pitrou.net date: Sat Dec 15 21:14:21 2012 +0100 files: Doc/library/select.rst Lib/test/test_epoll.py Misc/NEWS Modules/selectmodule.c description: Issue #16488: epoll() objects now support the `with` statement. Patch by Serhiy Storchaka. diff -r 64b5c4a9bb3e -r d6f61cd364d9 Doc/library/select.rst --- a/Doc/library/select.rst Sat Dec 15 19:26:38 2012 +0100 +++ b/Doc/library/select.rst Sat Dec 15 21:14:21 2012 +0100 @@ -47,11 +47,14 @@ to :const:`EPOLL_CLOEXEC`, which causes the epoll descriptor to be closed automatically when :func:`os.execve` is called. See section :ref:`epoll-objects` below for the methods supported by epolling objects. - + They also support the :keyword:`with` statement. .. versionchanged:: 3.3 Added the *flags* parameter. + .. versionchanged:: 3.4 + Support for the :keyword:`with` statement was added. + .. function:: poll() diff -r 64b5c4a9bb3e -r d6f61cd364d9 Lib/test/test_epoll.py --- a/Lib/test/test_epoll.py Sat Dec 15 19:26:38 2012 +0100 +++ b/Lib/test/test_epoll.py Sat Dec 15 21:14:21 2012 +0100 @@ -87,6 +87,13 @@ self.assertRaises(TypeError, select.epoll, ['foo']) self.assertRaises(TypeError, select.epoll, {}) + def test_context_manager(self): + with select.epoll(16) as ep: + self.assertGreater(ep.fileno(), 0) + self.assertFalse(ep.closed) + self.assertTrue(ep.closed) + self.assertRaises(ValueError, ep.fileno) + def test_add(self): server, client = self._connected_pair() diff -r 64b5c4a9bb3e -r d6f61cd364d9 Misc/NEWS --- a/Misc/NEWS Sat Dec 15 19:26:38 2012 +0100 +++ b/Misc/NEWS Sat Dec 15 21:14:21 2012 +0100 @@ -167,6 +167,9 @@ Library ------- +- Issue #16488: epoll() objects now support the `with` statement. Patch + by Serhiy Storchaka. + - Issue #16298: In HTTPResponse.read(), close the socket when there is no Content-Length and the incoming stream is finished. Patch by Eran Rundstein. diff -r 64b5c4a9bb3e -r d6f61cd364d9 Modules/selectmodule.c --- a/Modules/selectmodule.c Sat Dec 15 19:26:38 2012 +0100 +++ b/Modules/selectmodule.c Sat Dec 15 21:14:21 2012 +0100 @@ -1394,6 +1394,24 @@ in seconds (as float). -1 makes poll wait indefinitely.\n\ Up to maxevents are returned to the caller."); +static PyObject * +pyepoll_enter(pyEpoll_Object *self, PyObject *args) +{ + if (self->epfd < 0) + return pyepoll_err_closed(); + + Py_INCREF(self); + return (PyObject *)self; +} + +static PyObject * +pyepoll_exit(PyObject *self, PyObject *args) +{ + _Py_IDENTIFIER(close); + + return _PyObject_CallMethodId(self, &PyId_close, NULL); +} + static PyMethodDef pyepoll_methods[] = { {"fromfd", (PyCFunction)pyepoll_fromfd, METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc}, @@ -1409,6 +1427,10 @@ METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc}, {"poll", (PyCFunction)pyepoll_poll, METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc}, + {"__enter__", (PyCFunction)pyepoll_enter, METH_NOARGS, + NULL}, + {"__exit__", (PyCFunction)pyepoll_exit, METH_VARARGS, + NULL}, {NULL, NULL}, }; /solipsis@pitrou.net