]> granicus.if.org Git - python/commitdiff
Issue #16488: epoll() objects now support the `with` statement.
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 15 Dec 2012 20:14:21 +0000 (21:14 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 15 Dec 2012 20:14:21 +0000 (21:14 +0100)
Patch by Serhiy Storchaka.

Doc/library/select.rst
Lib/test/test_epoll.py
Misc/NEWS
Modules/selectmodule.c

index 4e60f4ad883a3b727042e85273a3ed3e4e24a91c..b73f11e227604c7c3217d51eeef1f2ca6df44c57 100644 (file)
@@ -47,11 +47,14 @@ The module defines the following:
    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()
 
index 7f9547ff959316399344a3ca20e0b6e3c6c57798..b9b1492862d364b4eec845c881506f03c194ca0d 100644 (file)
@@ -87,6 +87,13 @@ class TestEPoll(unittest.TestCase):
         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()
 
index 7b8636a730a6ac0bc3ec9b103c3da062bf59543b..7b71524210a17ce502a92aa7fb89d838d3d55e91 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -167,6 +167,9 @@ Core and Builtins
 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.
index c0f56a79e9f23a76fc1fb53cb1a889ce55c87c12..52be4d8eea09cda25d0f025ead0d1557517a34e1 100644 (file)
@@ -1394,6 +1394,24 @@ Wait for events on the epoll file descriptor for a maximum time of timeout\n\
 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 @@ static PyMethodDef pyepoll_methods[] = {
      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},
 };