Applied patch #1657 epoll and kqueue wrappers for the select module

The patch adds wrappers for the Linux epoll syscalls and the BSD kqueue syscalls. Thanks to Thomas Herve and the Twisted people for their support and help.
TODO: Finish documentation documentation
This commit is contained in:
Christian Heimes 2008-03-21 23:49:44 +00:00
parent 5f79446af0
commit 0e9ab5f2f0
11 changed files with 2044 additions and 22 deletions

View File

@ -7,10 +7,12 @@
This module provides access to the :cfunc:`select` and :cfunc:`poll` functions
available in most operating systems. Note that on Windows, it only works for
sockets; on other operating systems, it also works for other file types (in
particular, on Unix, it works on pipes). It cannot be used on regular files to
determine whether a file has grown since it was last read.
available in most operating systems, :cfunc:`epoll` available on Linux 2.5+ and
:cfunc:`kqueue` available on most BSD.
Note that on Windows, it only works for sockets; on other operating systems,
it also works for other file types (in particular, on Unix, it works on pipes).
It cannot be used on regular files to determine whether a file has grown since
it was last read.
The module defines the following:
@ -22,6 +24,16 @@ The module defines the following:
string, as would be printed by the C function :cfunc:`perror`.
.. type:: epoll([sizehint=-1])
(Only supported on Linux 2.5.44 and newer.) Returns an edge polling
object, which can be used as Edge or Level Triggered interface for I/O
events; see section :ref:`epoll-objects` below for the methods supported
by epolling objects.
.. versionadded:: 2.6
.. function:: poll()
(Not supported by all operating systems.) Returns a polling object, which
@ -30,6 +42,24 @@ The module defines the following:
by polling objects.
.. type:: kqueue()
(Only supported on BSD.) Returns a kernel queue object
object; see section :ref:`kqueue-objects` below for the methods supported
by kqueue objects.
.. versionadded:: 2.6
.. type:: kqueue(ident, filter=KQ_FILTER_READ, flags=KQ_ADD, fflags=0, data=0, udata=0)
(Only supported on BSD.) Returns a kernel event object
object; see section :ref:`kevent-objects` below for the methods supported
by kqueue objects.
.. versionadded:: 2.6
.. function:: select(iwtd, owtd, ewtd[, timeout])
This is a straightforward interface to the Unix :cfunc:`select` system call.
@ -67,6 +97,81 @@ The module defines the following:
not handle file descriptors that don't originate from WinSock.
.. _epoll-objects:
Edge and Level Trigger Polling (epoll) Objects
----------------------------------------------
http://linux.die.net/man/4/epoll
*eventmask*
+-----------------------+-----------------------------------------------+
| Constant | Meaning |
+=======================+===============================================+
| :const:`EPOLLIN` | Available for read |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLOUT` | Available for write |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLPRI` | Urgent data for read |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLERR` | Error condition happend on the assoc. fd |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLHUP` | Hang up happend on the assoc. fd |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
| | Level Trigger behavior |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
| | pulled out, the fd is internally disabled |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLRDNORM` | ??? |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLRDBAND` | ??? |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLWRNORM` | ??? |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLWRBAND` | ??? |
+-----------------------+-----------------------------------------------+
| :const:`EPOLLMSG` | ??? |
+-----------------------+-----------------------------------------------+
.. method:: epoll.close()
Close the control file descriptor of the epoll object.
.. method:: epoll.fileno()
Return the file descriptor number of the control fd.
.. method:: epoll.fromfd(fd)
Create an epoll object from a given file descriptor.
.. method:: epoll.register(fd[, eventmask])
Register a fd descriptor with the epoll object.
.. method:: epoll.modify(fd, eventmask)
Modify a register file descriptor.
.. method:: epoll.unregister(fd)
Remove a registered file descriptor from the epoll object.
.. method:: epoll.poll([timeout=-1[, maxevents=-1]])
Wait for events. timeout in seconds (float)
.. _poll-objects:
Polling Objects
@ -114,6 +219,16 @@ linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
the same effect as registering the descriptor exactly once.
.. method:: poll.modify(fd, eventmask)
Modifies an already registered fd. This has the same effect as
:meth:`register(fd, eventmask)`. Attempting to modify a file descriptor
that was never registered causes an :exc:`IOError` exception with errno
:const:`ENOENT` to be raised.
.. versionadded:: 2.6
.. method:: poll.unregister(fd)
Remove a file descriptor being tracked by a polling object. Just like the
@ -137,3 +252,184 @@ linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
returning. If *timeout* is omitted, negative, or :const:`None`, the call will
block until there is an event for this poll object.
.. _kqueue-objects:
Kqueue Objects
--------------
.. method:: kqueue.close()
Close the control file descriptor of the kqueue object.
.. method:: kqueue.fileno()
Return the file descriptor number of the control fd.
.. method:: epoll.fromfd(fd)
Create a kqueue object from a given file descriptor.
.. method:: control(changelist, max_events=0[, timeout=None]) -> eventlist
Low level interface to kevent
- changelist must be an iterable of kevent object or None
- max_events must be 0 or a positive integer
- timeout in seconds (floats possible)
.. _kevent-objects:
Kevent Objects
--------------
http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
.. attribute:: ident
Value used to identify the event. The interpretation depends on the filter
but it's usually the file descriptor. In the constructor ident can either
be an int or an object with a fileno() function. kevent stores the integer
internally.
.. attribute:: filter
Name of the kernel filter
+---------------------------+---------------------------------------------+
| Constant | Meaning |
+===========================+=============================================+
| :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
| | there is data available to read |
+---------------------------+---------------------------------------------+
| :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
| | there is data available to read |
+---------------------------+---------------------------------------------+
| :const:`KQ_FILTER_AIO` | AIO requests |
+---------------------------+---------------------------------------------+
| :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
| | events watched in *fflag* occurs |
+---------------------------+---------------------------------------------+
| :const:`KQ_FILTER_PROC` | Watch for events on a process id |
+---------------------------+---------------------------------------------+
| :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
| | [not available on Mac OS X] |
+---------------------------+---------------------------------------------+
| :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
| | delivered to the process |
+---------------------------+---------------------------------------------+
| :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
+---------------------------+---------------------------------------------+
.. attribute:: flags
Filter action
+---------------------------+---------------------------------------------+
| Constant | Meaning |
+===========================+=============================================+
| :const:`KQ_EV_ADD` | Adds or modifies an event |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_DELETE` | Removes an event from the queue |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_DISABLE` | Disablesevent |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_ONESHOT` | Removes event after first occurence |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_SYSFLAGS` | internal event |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_FLAG1` | internal event |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_EOF` | Filter specific EOF condition |
+---------------------------+---------------------------------------------+
| :const:`KQ_EV_ERROR` | See return values |
+---------------------------+---------------------------------------------+
.. attribute:: fflags
Filter specific flags
*:const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags
+----------------------------+--------------------------------------------+
| Constant | Meaning |
+============================+============================================+
| :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
+----------------------------+--------------------------------------------+
*:const:`KQ_FILTER_VNODE` filter flags*
+----------------------------+--------------------------------------------+
| Constant | Meaning |
+============================+============================================+
| :const:`KQ_NOTE_DELETE` | *unlink()* was called |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_WRITE` | a write occured |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_EXTEND` | the file was extended |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_LINK` | the link count has changed |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_RENAME` | the file was renamed |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
+----------------------------+--------------------------------------------+
*:const:`KQ_FILTER_PROC` filter flags*
+----------------------------+--------------------------------------------+
| Constant | Meaning |
+============================+============================================+
| :const:`KQ_NOTE_EXIT` | the process has exited |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_FORK` | the process has called *fork()* |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_EXEC` | the process has executed a new process |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_CHILD` | returned on the child process for |
| | *NOTE_TRACK* |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
+----------------------------+--------------------------------------------+
*:const:`KQ_FILTER_NETDEV` filter flags* [not available on Mac OS X]
+----------------------------+--------------------------------------------+
| Constant | Meaning |
+============================+============================================+
| :const:`KQ_NOTE_LINKUP` | link is up |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_LINKDOWN` | link is down |
+----------------------------+--------------------------------------------+
| :const:`KQ_NOTE_LINKINV` | link state is invalid |
+----------------------------+--------------------------------------------+
.. attribute:: data
Filter specific data
.. attribute:: udata
User defined value

View File

@ -645,3 +645,58 @@ The :mod:`xmlrpclib` module contains the following notice::
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.
test_epoll
----------
The :mod:`test_epoll` contains the following notice::
Copyright (c) 2001-2006 Twisted Matrix Laboratories.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Select kqueue
-------------
The :mod:`select` and contains the following notice for the kqueue interface::
Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

View File

@ -825,10 +825,12 @@ _expectations = {
test_dl
test_fcntl
test_fork1
test_epoll
test_gdbm
test_grp
test_ioctl
test_largefile
test_kqueue
test_mhlib
test_openpty
test_ossaudiodev
@ -850,6 +852,7 @@ _expectations = {
test_curses
test_dl
test_largefile
test_kqueue
test_ossaudiodev
""",
'mac':
@ -866,10 +869,12 @@ _expectations = {
test_dl
test_fcntl
test_fork1
test_epoll
test_grp
test_ioctl
test_largefile
test_locale
test_kqueue
test_mmap
test_openpty
test_ossaudiodev
@ -890,7 +895,9 @@ _expectations = {
test_bsddb
test_bsddb185
test_dl
test_epoll
test_largefile
test_kqueue
test_minidom
test_openpty
test_pyexpat
@ -902,7 +909,9 @@ _expectations = {
test_bsddb
test_bsddb185
test_dl
test_epoll
test_largefile
test_kqueue
test_minidom
test_openpty
test_pyexpat
@ -916,9 +925,11 @@ _expectations = {
test_bsddb185
test_dl
test_fork1
test_epoll
test_gettext
test_largefile
test_locale
test_kqueue
test_minidom
test_openpty
test_pyexpat
@ -943,10 +954,12 @@ _expectations = {
test_dl
test_fcntl
test_fork1
test_epoll
test_gdbm
test_grp
test_largefile
test_locale
test_kqueue
test_mmap
test_openpty
test_poll
@ -967,9 +980,11 @@ _expectations = {
test_bsddb
test_bsddb3
test_curses
test_epoll
test_gdbm
test_largefile
test_locale
test_kqueue
test_minidom
test_ossaudiodev
test_poll
@ -980,6 +995,8 @@ _expectations = {
test_bsddb185
test_curses
test_dbm
test_epoll
test_kqueue
test_gdbm
test_gzip
test_openpty
@ -992,10 +1009,12 @@ _expectations = {
test_bsddb185
test_curses
test_dl
test_epoll
test_gdbm
test_gzip
test_largefile
test_locale
test_kqueue
test_minidom
test_openpty
test_pyexpat
@ -1009,8 +1028,10 @@ _expectations = {
test_curses
test_dl
test_gdbm
test_epoll
test_largefile
test_locale
test_kqueue
test_mhlib
test_mmap
test_poll
@ -1023,7 +1044,9 @@ _expectations = {
test_bsddb3
test_curses
test_dbm
test_epoll
test_ioctl
test_kqueue
test_largefile
test_locale
test_ossaudiodev
@ -1037,6 +1060,8 @@ _expectations = {
test_commands
test_curses
test_dl
test_epoll
test_kqueue
test_largefile
test_mhlib
test_mmap
@ -1050,6 +1075,7 @@ _expectations = {
"""
test_bsddb
test_bsddb3
test_epoll
test_gdbm
test_locale
test_ossaudiodev
@ -1068,8 +1094,10 @@ _expectations = {
test_bsddb3
test_bz2
test_dl
test_epoll
test_gdbm
test_gzip
test_kqueue
test_ossaudiodev
test_tcl
test_zipimport
@ -1081,6 +1109,7 @@ _expectations = {
test_bsddb3
test_ctypes
test_dl
test_epoll
test_gdbm
test_locale
test_normalization
@ -1096,6 +1125,7 @@ _expectations = {
test_ctypes
test_curses
test_dl
test_epoll
test_gdbm
test_locale
test_ossaudiodev

189
Lib/test/test_epoll.py Normal file
View File

@ -0,0 +1,189 @@
# Copyright (c) 2001-2006 Twisted Matrix Laboratories.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Tests for epoll wrapper.
"""
import os
import socket
import errno
import time
import select
import tempfile
import unittest
from test import test_support
if not hasattr(select, "epoll"):
raise test_support.TestSkipped("test works only on Linux 2.6")
class TestEPoll(unittest.TestCase):
def setUp(self):
self.serverSocket = socket.socket()
self.serverSocket.bind(('127.0.0.1', 0))
self.serverSocket.listen(1)
self.connections = [self.serverSocket]
def tearDown(self):
for skt in self.connections:
skt.close()
def _connected_pair(self):
client = socket.socket()
client.setblocking(False)
try:
client.connect(('127.0.0.1', self.serverSocket.getsockname()[1]))
except socket.error, e:
self.assertEquals(e.args[0], errno.EINPROGRESS)
else:
raise AssertionError("Connect should have raised EINPROGRESS")
server, addr = self.serverSocket.accept()
self.connections.extend((client, server))
return client, server
def test_create(self):
try:
ep = select.epoll(16)
except OSError, e:
raise AssertionError(str(e))
self.assert_(ep.fileno() > 0, ep.fileno())
self.assert_(not ep.closed)
ep.close()
self.assert_(ep.closed)
self.assertRaises(ValueError, ep.fileno)
def test_badcreate(self):
self.assertRaises(TypeError, select.epoll, 1, 2, 3)
self.assertRaises(TypeError, select.epoll, 'foo')
self.assertRaises(TypeError, select.epoll, None)
self.assertRaises(TypeError, select.epoll, ())
self.assertRaises(TypeError, select.epoll, ['foo'])
self.assertRaises(TypeError, select.epoll, {})
def test_add(self):
server, client = self._connected_pair()
ep = select.epoll(2)
try:
ep.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT)
ep.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT)
finally:
ep.close()
def test_fromfd(self):
server, client = self._connected_pair()
ep = select.epoll(2)
ep2 = select.epoll.fromfd(ep.fileno())
ep2.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT)
ep2.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT)
events = ep.poll(1, 4)
events2 = ep2.poll(0.9, 4)
self.assertEqual(len(events), 2)
self.assertEqual(len(events2), 2)
ep.close()
try:
ep2.poll(1, 4)
except IOError, e:
self.failUnlessEqual(e.args[0], errno.EBADF, e)
else:
self.fail("epoll on closed fd didn't raise EBADF")
def test_control_and_wait(self):
client, server = self._connected_pair()
ep = select.epoll(16)
ep.register(server.fileno(),
select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
ep.register(client.fileno(),
select.EPOLLIN | select.EPOLLOUT | select.EPOLLET)
now = time.time()
events = ep.poll(1, 4)
then = time.time()
self.failIf(then - now > 0.1, then - now)
events.sort()
expected = [(client.fileno(), select.EPOLLOUT),
(server.fileno(), select.EPOLLOUT)]
expected.sort()
self.assertEquals(events, expected)
self.failIf(then - now > 0.01, then - now)
now = time.time()
events = ep.poll(timeout=2.1, maxevents=4)
then = time.time()
self.failIf(events)
client.send("Hello!")
server.send("world!!!")
now = time.time()
events = ep.poll(1, 4)
then = time.time()
self.failIf(then - now > 0.01)
events.sort()
expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
(server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
expected.sort()
self.assertEquals(events, expected)
ep.unregister(client.fileno())
ep.modify(server.fileno(), select.EPOLLOUT)
now = time.time()
events = ep.poll(1, 4)
then = time.time()
self.failIf(then - now > 0.01)
expected = [(server.fileno(), select.EPOLLOUT)]
self.assertEquals(events, expected)
def test_errors(self):
self.assertRaises(ValueError, select.epoll, -2)
self.assertRaises(ValueError, select.epoll().register, -1,
select.EPOLLIN)
def test_unregister_closed(self):
server, client = self._connected_pair()
fd = server.fileno()
ep = select.epoll(16)
ep.register(server)
now = time.time()
events = ep.poll(1, 4)
then = time.time()
self.failIf(then - now > 0.01)
server.close()
ep.unregister(fd)
def test_main():
test_support.run_unittest(TestEPoll)
if __name__ == "__main__":
test_main()

166
Lib/test/test_kqueue.py Normal file
View File

@ -0,0 +1,166 @@
"""
Tests for kqueue wrapper.
"""
import socket
import errno
import time
import select
import sys
import unittest
from test import test_support
if not hasattr(select, "kqueue"):
raise test_support.TestSkipped("test works only on BSD")
class TestKQueue(unittest.TestCase):
def test_create_queue(self):
kq = select.kqueue()
self.assert_(kq.fileno() > 0, kq.fileno())
self.assert_(not kq.closed)
kq.close()
self.assert_(kq.closed)
self.assertRaises(ValueError, kq.fileno)
def test_create_event(self):
fd = sys.stderr.fileno()
ev = select.kevent(fd)
other = select.kevent(1000)
self.assertEqual(ev.ident, fd)
self.assertEqual(ev.filter, select.KQ_FILTER_READ)
self.assertEqual(ev.flags, select.KQ_EV_ADD)
self.assertEqual(ev.fflags, 0)
self.assertEqual(ev.data, 0)
self.assertEqual(ev.udata, 0)
self.assertEqual(ev, ev)
self.assertNotEqual(ev, other)
self.assertEqual(cmp(ev, other), -1)
self.assert_(ev < other)
self.assert_(other >= ev)
self.assertRaises(TypeError, cmp, ev, None)
self.assertRaises(TypeError, cmp, ev, 1)
self.assertRaises(TypeError, cmp, ev, "ev")
ev = select.kevent(fd, select.KQ_FILTER_WRITE)
self.assertEqual(ev.ident, fd)
self.assertEqual(ev.filter, select.KQ_FILTER_WRITE)
self.assertEqual(ev.flags, select.KQ_EV_ADD)
self.assertEqual(ev.fflags, 0)
self.assertEqual(ev.data, 0)
self.assertEqual(ev.udata, 0)
self.assertEqual(ev, ev)
self.assertNotEqual(ev, other)
ev = select.kevent(fd, select.KQ_FILTER_WRITE, select.KQ_EV_ONESHOT)
self.assertEqual(ev.ident, fd)
self.assertEqual(ev.filter, select.KQ_FILTER_WRITE)
self.assertEqual(ev.flags, select.KQ_EV_ONESHOT)
self.assertEqual(ev.fflags, 0)
self.assertEqual(ev.data, 0)
self.assertEqual(ev.udata, 0)
self.assertEqual(ev, ev)
self.assertNotEqual(ev, other)
ev = select.kevent(1, 2, 3, 4, 5, 6)
self.assertEqual(ev.ident, 1)
self.assertEqual(ev.filter, 2)
self.assertEqual(ev.flags, 3)
self.assertEqual(ev.fflags, 4)
self.assertEqual(ev.data, 5)
self.assertEqual(ev.udata, 6)
self.assertEqual(ev, ev)
self.assertNotEqual(ev, other)
def test_queue_event(self):
serverSocket = socket.socket()
serverSocket.bind(('127.0.0.1', 0))
serverSocket.listen(1)
client = socket.socket()
client.setblocking(False)
try:
client.connect(('127.0.0.1', serverSocket.getsockname()[1]))
except socket.error, e:
self.assertEquals(e.args[0], errno.EINPROGRESS)
else:
#raise AssertionError("Connect should have raised EINPROGRESS")
pass # FreeBSD doesn't raise an exception here
server, addr = serverSocket.accept()
if sys.platform.startswith("darwin"):
flags = select.KQ_EV_ADD | select.KQ_EV_ENABLE
else:
flags = 0
kq = select.kqueue()
kq2 = select.kqueue.fromfd(kq.fileno())
ev = select.kevent(server.fileno(),
select.KQ_FILTER_WRITE,
select.KQ_EV_ADD | select.KQ_EV_ENABLE)
kq.control([ev], 0)
ev = select.kevent(server.fileno(),
select.KQ_FILTER_READ,
select.KQ_EV_ADD | select.KQ_EV_ENABLE)
kq.control([ev], 0)
ev = select.kevent(client.fileno(),
select.KQ_FILTER_WRITE,
select.KQ_EV_ADD | select.KQ_EV_ENABLE)
kq2.control([ev], 0)
ev = select.kevent(client.fileno(),
select.KQ_FILTER_READ,
select.KQ_EV_ADD | select.KQ_EV_ENABLE)
kq2.control([ev], 0)
events = kq.control(None, 4, 1)
events = [(e.ident, e.filter, e.flags) for e in events]
events.sort()
self.assertEquals(events, [
(client.fileno(), select.KQ_FILTER_WRITE, flags),
(server.fileno(), select.KQ_FILTER_WRITE, flags)])
client.send("Hello!")
server.send("world!!!")
events = kq.control(None, 4, 1)
# We may need to call it several times
for i in range(5):
if len(events) == 4:
break
events = kq.control(None, 4, 1)
events = [(e.ident, e.filter, e.flags) for e in events]
events.sort()
self.assertEquals(events, [
(client.fileno(), select.KQ_FILTER_WRITE, flags),
(client.fileno(), select.KQ_FILTER_READ, flags),
(server.fileno(), select.KQ_FILTER_WRITE, flags),
(server.fileno(), select.KQ_FILTER_READ, flags)])
# Remove completely client, and server read part
ev = select.kevent(client.fileno(),
select.KQ_FILTER_WRITE,
select.KQ_EV_DELETE)
kq.control([ev], 0)
ev = select.kevent(client.fileno(),
select.KQ_FILTER_READ,
select.KQ_EV_DELETE)
kq.control([ev], 0)
ev = select.kevent(server.fileno(),
select.KQ_FILTER_READ,
select.KQ_EV_DELETE)
kq.control([ev], 0, 0)
events = kq.control([], 4, 0.99)
events = [(e.ident, e.filter, e.flags) for e in events]
events.sort()
self.assertEquals(events, [
(server.fileno(), select.KQ_FILTER_WRITE, flags)])
client.close()
server.close()
serverSocket.close()
def test_main():
test_support.run_unittest(TestKQueue)
if __name__ == "__main__":
test_main()

View File

@ -34,7 +34,8 @@ class PollTests(unittest.TestCase):
for i in range(NUM_PIPES):
rd, wr = os.pipe()
p.register(rd, select.POLLIN)
p.register(rd)
p.modify(rd, select.POLLIN)
p.register(wr, select.POLLOUT)
readers.append(rd)
writers.append(wr)

View File

@ -1363,6 +1363,8 @@ Library
Extension Modules
-----------------
- Patch #1657: added select.epoll and select.kqueue
- Patch #1506171: added operator.methodcaller().
- Patch #1826: operator.attrgetter() now supports dotted attribute paths.

File diff suppressed because it is too large Load Diff

110
configure vendored
View File

@ -5416,6 +5416,8 @@ done
@ -5424,8 +5426,8 @@ fcntl.h grp.h \
io.h langinfo.h libintl.h ncurses.h poll.h process.h pthread.h \
shadow.h signal.h stdint.h stropts.h termios.h thread.h \
unistd.h utime.h \
sys/audioio.h sys/bsdtty.h sys/file.h sys/loadavg.h sys/lock.h sys/mkdev.h \
sys/modem.h \
sys/audioio.h sys/bsdtty.h sys/epoll.h sys/event.h sys/file.h sys/loadavg.h \
sys/lock.h sys/mkdev.h sys/modem.h \
sys/param.h sys/poll.h sys/select.h sys/socket.h sys/statvfs.h sys/stat.h \
sys/time.h \
sys/times.h sys/types.h sys/un.h sys/utsname.h sys/wait.h pty.h libutil.h \
@ -15917,7 +15919,111 @@ echo "${ECHO_T}no" >&6; }
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
{ echo "$as_me:$LINENO: checking for epoll" >&5
echo $ECHO_N "checking for epoll... $ECHO_C" >&6; }
cat >conftest.$ac_ext <<_ACEOF
/* confdefs.h. */
_ACEOF
cat confdefs.h >>conftest.$ac_ext
cat >>conftest.$ac_ext <<_ACEOF
/* end confdefs.h. */
#include <sys/epoll.h>
int
main ()
{
void *x=epoll_create
;
return 0;
}
_ACEOF
rm -f conftest.$ac_objext
if { (ac_try="$ac_compile"
case "(($ac_try" in
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
*) ac_try_echo=$ac_try;;
esac
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
(eval "$ac_compile") 2>conftest.er1
ac_status=$?
grep -v '^ *+' conftest.er1 >conftest.err
rm -f conftest.er1
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } && {
test -z "$ac_c_werror_flag" ||
test ! -s conftest.err
} && test -s conftest.$ac_objext; then
cat >>confdefs.h <<\_ACEOF
#define HAVE_EPOLL 1
_ACEOF
{ echo "$as_me:$LINENO: result: yes" >&5
echo "${ECHO_T}yes" >&6; }
else
echo "$as_me: failed program was:" >&5
sed 's/^/| /' conftest.$ac_ext >&5
{ echo "$as_me:$LINENO: result: no" >&5
echo "${ECHO_T}no" >&6; }
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
{ echo "$as_me:$LINENO: checking for kqueue" >&5
echo $ECHO_N "checking for kqueue... $ECHO_C" >&6; }
cat >conftest.$ac_ext <<_ACEOF
/* confdefs.h. */
_ACEOF
cat confdefs.h >>conftest.$ac_ext
cat >>conftest.$ac_ext <<_ACEOF
/* end confdefs.h. */
#include <sys/types.h>
#include <sys/event.h>
int
main ()
{
int x=kqueue()
;
return 0;
}
_ACEOF
rm -f conftest.$ac_objext
if { (ac_try="$ac_compile"
case "(($ac_try" in
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
*) ac_try_echo=$ac_try;;
esac
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
(eval "$ac_compile") 2>conftest.er1
ac_status=$?
grep -v '^ *+' conftest.er1 >conftest.err
rm -f conftest.er1
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } && {
test -z "$ac_c_werror_flag" ||
test ! -s conftest.err
} && test -s conftest.$ac_objext; then
cat >>confdefs.h <<\_ACEOF
#define HAVE_KQUEUE 1
_ACEOF
{ echo "$as_me:$LINENO: result: yes" >&5
echo "${ECHO_T}yes" >&6; }
else
echo "$as_me: failed program was:" >&5
sed 's/^/| /' conftest.$ac_ext >&5
{ echo "$as_me:$LINENO: result: no" >&5
echo "${ECHO_T}no" >&6; }
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
# On some systems (eg. FreeBSD 5), we would find a definition of the
# functions ctermid_r, setgroups in the library, but no prototype
# (e.g. because we use _XOPEN_SOURCE). See whether we can take their

View File

@ -1102,8 +1102,8 @@ fcntl.h grp.h \
io.h langinfo.h libintl.h ncurses.h poll.h process.h pthread.h \
shadow.h signal.h stdint.h stropts.h termios.h thread.h \
unistd.h utime.h \
sys/audioio.h sys/bsdtty.h sys/file.h sys/loadavg.h sys/lock.h sys/mkdev.h \
sys/modem.h \
sys/audioio.h sys/bsdtty.h sys/epoll.h sys/event.h sys/file.h sys/loadavg.h \
sys/lock.h sys/mkdev.h sys/modem.h \
sys/param.h sys/poll.h sys/select.h sys/socket.h sys/statvfs.h sys/stat.h \
sys/time.h \
sys/times.h sys/types.h sys/un.h sys/utsname.h sys/wait.h pty.h libutil.h \
@ -2354,7 +2354,21 @@ AC_TRY_COMPILE([#include <unistd.h>], void *x=fdatasync,
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no)
)
AC_MSG_CHECKING(for epoll)
AC_TRY_COMPILE([#include <sys/epoll.h>], void *x=epoll_create,
AC_DEFINE(HAVE_EPOLL, 1, Define if you have the 'epoll' functions.)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no)
)
AC_MSG_CHECKING(for kqueue)
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/event.h>
], int x=kqueue(),
AC_DEFINE(HAVE_KQUEUE, 1, Define if you have the 'kqueue' functions.)
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no)
)
# On some systems (eg. FreeBSD 5), we would find a definition of the
# functions ctermid_r, setgroups in the library, but no prototype
# (e.g. because we use _XOPEN_SOURCE). See whether we can take their

View File

@ -147,6 +147,9 @@
/* Defined when any dynamic module loading is enabled. */
#undef HAVE_DYNAMIC_LOADING
/* Define if you have the 'epoll' functions. */
#undef HAVE_EPOLL
/* Define to 1 if you have the <errno.h> header file. */
#undef HAVE_ERRNO_H
@ -318,6 +321,9 @@
/* Define to 1 if you have the `killpg' function. */
#undef HAVE_KILLPG
/* Define if you have the 'kqueue' functions. */
#undef HAVE_KQUEUE
/* Define to 1 if you have the <langinfo.h> header file. */
#undef HAVE_LANGINFO_H
@ -627,6 +633,12 @@
*/
#undef HAVE_SYS_DIR_H
/* Define to 1 if you have the <sys/epoll.h> header file. */
#undef HAVE_SYS_EPOLL_H
/* Define to 1 if you have the <sys/event.h> header file. */
#undef HAVE_SYS_EVENT_H
/* Define to 1 if you have the <sys/file.h> header file. */
#undef HAVE_SYS_FILE_H