Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09bb89b

Browse files
committed
Issue #16488: epoll() objects now support the with statement.
Patch by Serhiy Storchaka.
1 parent c48e81e commit 09bb89b

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

Doc/library/select.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ The module defines the following:
4747
to :const:`EPOLL_CLOEXEC`, which causes the epoll descriptor to be closed
4848
automatically when :func:`os.execve` is called. See section
4949
:ref:`epoll-objects` below for the methods supported by epolling objects.
50-
50+
They also support the :keyword:`with` statement.
5151

5252
.. versionchanged:: 3.3
5353
Added the *flags* parameter.
5454

55+
.. versionchanged:: 3.4
56+
Support for the :keyword:`with` statement was added.
57+
5558

5659
.. function:: poll()
5760

Lib/test/test_epoll.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ def test_badcreate(self):
8787
self.assertRaises(TypeError, select.epoll, ['foo'])
8888
self.assertRaises(TypeError, select.epoll, {})
8989

90+
def test_context_manager(self):
91+
with select.epoll(16) as ep:
92+
self.assertGreater(ep.fileno(), 0)
93+
self.assertFalse(ep.closed)
94+
self.assertTrue(ep.closed)
95+
self.assertRaises(ValueError, ep.fileno)
96+
9097
def test_add(self):
9198
server, client = self._connected_pair()
9299

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ Core and Builtins
167167
Library
168168
-------
169169

170+
- Issue #16488: epoll() objects now support the `with` statement. Patch
171+
by Serhiy Storchaka.
172+
170173
- Issue #16298: In HTTPResponse.read(), close the socket when there is no
171174
Content-Length and the incoming stream is finished. Patch by Eran
172175
Rundstein.

Modules/selectmodule.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,24 @@ Wait for events on the epoll file descriptor for a maximum time of timeout\n\
13941394
in seconds (as float). -1 makes poll wait indefinitely.\n\
13951395
Up to maxevents are returned to the caller.");
13961396

1397+
static PyObject *
1398+
pyepoll_enter(pyEpoll_Object *self, PyObject *args)
1399+
{
1400+
if (self->epfd < 0)
1401+
return pyepoll_err_closed();
1402+
1403+
Py_INCREF(self);
1404+
return (PyObject *)self;
1405+
}
1406+
1407+
static PyObject *
1408+
pyepoll_exit(PyObject *self, PyObject *args)
1409+
{
1410+
_Py_IDENTIFIER(close);
1411+
1412+
return _PyObject_CallMethodId(self, &PyId_close, NULL);
1413+
}
1414+
13971415
static PyMethodDef pyepoll_methods[] = {
13981416
{"fromfd", (PyCFunction)pyepoll_fromfd,
13991417
METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
@@ -1409,6 +1427,10 @@ static PyMethodDef pyepoll_methods[] = {
14091427
METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
14101428
{"poll", (PyCFunction)pyepoll_poll,
14111429
METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
1430+
{"__enter__", (PyCFunction)pyepoll_enter, METH_NOARGS,
1431+
NULL},
1432+
{"__exit__", (PyCFunction)pyepoll_exit, METH_VARARGS,
1433+
NULL},
14121434
{NULL, NULL},
14131435
};
14141436

0 commit comments

Comments
 (0)