Skip to content

Commit 0cdf5f4

Browse files
authored
bpo-32568: make select.epoll() and its docs consistent (#7840)
* `flags` is indeed deprecated, but there is a validation on its value for backwards compatibility reasons. This adds mention of this in the docs. * The docs say that `sizehint` is deprecated and ignored, but it is still used when `epoll_create1()` is unavailable. This adds mention of this in the docs. * `sizehint=-1` is acceptable again, and is replaced with `FD_SETSIZE-1`. This is needed to have a default value available at the Python level, since `FD_SETSIZE` is not exposed to Python. (see: bpo-31938) * Reject `sizehint=0` since it is invalid to pass on to `epoll_create()`. The relevant tests have also been updated.
1 parent 5bb5bbf commit 0cdf5f4

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

Doc/library/select.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ The module defines the following:
5757

5858
(Only supported on Linux 2.5.44 and newer.) Return an edge polling object,
5959
which can be used as Edge or Level Triggered interface for I/O
60-
events. *sizehint* and *flags* are deprecated and completely ignored.
60+
events.
61+
62+
*sizehint* informs epoll about the expected number of events to be
63+
registered. It must be positive, or `-1` to use the default. It is only
64+
used on older systems where :c:func:`epoll_create1` is not available;
65+
otherwise it has no effect (though its value is still checked).
66+
67+
*flags* is deprecated and completely ignored. However, when supplied, its
68+
value must be ``0`` or ``select.EPOLL_CLOEXEC``, otherwise ``OSError`` is
69+
raised.
6170

6271
See the :ref:`epoll-objects` section below for the methods supported by
6372
epolling objects.

Lib/test/test_epoll.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ def test_create(self):
7474
ep.close()
7575
self.assertTrue(ep.closed)
7676
self.assertRaises(ValueError, ep.fileno)
77+
7778
if hasattr(select, "EPOLL_CLOEXEC"):
78-
select.epoll(select.EPOLL_CLOEXEC).close()
79+
select.epoll(-1, select.EPOLL_CLOEXEC).close()
7980
select.epoll(flags=select.EPOLL_CLOEXEC).close()
8081
select.epoll(flags=0).close()
81-
self.assertRaises(OSError, select.epoll, flags=12356)
8282

8383
def test_badcreate(self):
8484
self.assertRaises(TypeError, select.epoll, 1, 2, 3)
@@ -88,6 +88,13 @@ def test_badcreate(self):
8888
self.assertRaises(TypeError, select.epoll, ['foo'])
8989
self.assertRaises(TypeError, select.epoll, {})
9090

91+
self.assertRaises(ValueError, select.epoll, 0)
92+
self.assertRaises(ValueError, select.epoll, -2)
93+
self.assertRaises(ValueError, select.epoll, sizehint=-2)
94+
95+
if hasattr(select, "EPOLL_CLOEXEC"):
96+
self.assertRaises(OSError, select.epoll, flags=12356)
97+
9198
def test_context_manager(self):
9299
with select.epoll(16) as ep:
93100
self.assertGreater(ep.fileno(), 0)
@@ -117,19 +124,19 @@ def test_add(self):
117124
try:
118125
# TypeError: argument must be an int, or have a fileno() method.
119126
self.assertRaises(TypeError, ep.register, object(),
120-
select.EPOLLIN | select.EPOLLOUT)
127+
select.EPOLLIN | select.EPOLLOUT)
121128
self.assertRaises(TypeError, ep.register, None,
122-
select.EPOLLIN | select.EPOLLOUT)
129+
select.EPOLLIN | select.EPOLLOUT)
123130
# ValueError: file descriptor cannot be a negative integer (-1)
124131
self.assertRaises(ValueError, ep.register, -1,
125-
select.EPOLLIN | select.EPOLLOUT)
132+
select.EPOLLIN | select.EPOLLOUT)
126133
# OSError: [Errno 9] Bad file descriptor
127134
self.assertRaises(OSError, ep.register, 10000,
128-
select.EPOLLIN | select.EPOLLOUT)
135+
select.EPOLLIN | select.EPOLLOUT)
129136
# registering twice also raises an exception
130137
ep.register(server, select.EPOLLIN | select.EPOLLOUT)
131138
self.assertRaises(OSError, ep.register, server,
132-
select.EPOLLIN | select.EPOLLOUT)
139+
select.EPOLLIN | select.EPOLLOUT)
133140
finally:
134141
ep.close()
135142

@@ -160,9 +167,9 @@ def test_control_and_wait(self):
160167

161168
ep = select.epoll(16)
162169
ep.register(server.fileno(),
163-
select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
170+
select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
164171
ep.register(client.fileno(),
165-
select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
172+
select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
166173

167174
now = time.monotonic()
168175
events = ep.poll(1, 4)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make select.epoll() and its documentation consistent regarding *sizehint* and
2+
*flags*.

Modules/selectmodule.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,14 +1297,17 @@ newPyEpoll_Object(PyTypeObject *type, int sizehint, SOCKET fd)
12971297
static PyObject *
12981298
pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
12991299
{
1300-
int flags = 0, sizehint = FD_SETSIZE - 1;
1300+
int flags = 0, sizehint = -1;
13011301
static char *kwlist[] = {"sizehint", "flags", NULL};
13021302

13031303
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
13041304
&sizehint, &flags))
13051305
return NULL;
1306-
if (sizehint < 0) {
1307-
PyErr_SetString(PyExc_ValueError, "negative sizehint");
1306+
if (sizehint == -1) {
1307+
sizehint = FD_SETSIZE - 1;
1308+
}
1309+
else if (sizehint <= 0) {
1310+
PyErr_SetString(PyExc_ValueError, "sizehint must be positive or -1");
13081311
return NULL;
13091312
}
13101313

@@ -1314,7 +1317,6 @@ pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13141317
return NULL;
13151318
}
13161319
#endif
1317-
13181320
return newPyEpoll_Object(type, sizehint, -1);
13191321
}
13201322

0 commit comments

Comments
 (0)