[Jython-checkins] jython: Restoring the original poll2 function, and making jython support both more

alan.kennedy jython-checkins at python.org
Mon Aug 27 20:52:10 CEST 2012


http://hg.python.org/jython/rev/5e12ad3013ff
changeset:   6855:5e12ad3013ff
user:        Alan Kennedy <alan at xhaus.com>
date:        Mon Aug 27 19:50:36 2012 +0100
summary:
  Restoring the original poll2 function, and making jython support both more explicit and easier to push upstream into cpython

files:
  Lib/asyncore.py |  47 ++++++++++++++++++++++++++++++++++--
  1 files changed, 44 insertions(+), 3 deletions(-)


diff --git a/Lib/asyncore.py b/Lib/asyncore.py
--- a/Lib/asyncore.py
+++ b/Lib/asyncore.py
@@ -174,6 +174,47 @@
     if timeout is not None:
         # timeout is in milliseconds
         timeout = int(timeout*1000)
+    pollster = select.poll()
+    if map:
+        for fd, obj in map.items():
+            flags = 0
+            if obj.readable():
+                flags |= select.POLLIN | select.POLLPRI
+            # accepting sockets should not be writable
+            if obj.writable() and not obj.accepting:
+                flags |= select.POLLOUT
+            if flags:
+                # Only check for exceptions if object was either readable
+                # or writable.
+                flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL
+                pollster.register(fd, flags)
+        try:
+            r = pollster.poll(timeout)
+        except select.error, err:
+            if err.args[0] != EINTR:
+                raise
+            r = []
+        for fd, flags in r:
+            obj = map.get(fd)
+            if obj is None:
+                continue
+            readwrite(obj, flags)
+
+poll3 = poll2                           # Alias for backward compatibility
+
+def jython_poll_fun(timeout=0.0, map=None):
+    # On jython, select.poll() is the mechanism to use,
+    # select.select is implemented on top of it.
+    # Also, we have to use a cache of such objects, because of problems with frequent 
+    # creation and destruction  of such objects on windows
+    # "select() crashes with IOException": http://bugs.jython.org/issue1291
+    # So this function is basically the same function as poll2 above, except
+    # with the select.poll() functionality wrapped in a try..finally clause.
+    if map is None:
+        map = socket_map
+    if timeout is not None:
+        # timeout is in milliseconds
+        timeout = int(timeout*1000)
     if map:
         try:
             pollster = select._poll_object_cache.get_poll_object()
@@ -203,8 +244,6 @@
         finally:
             select._poll_object_cache.release_poll_object(pollster)
 
-poll3 = poll2                           # Alias for backward compatibility
-
 def loop(timeout=30.0, use_poll=False, map=None, count=None):
     if map is None:
         map = socket_map
@@ -214,7 +253,7 @@
     else:
         poll_fun = poll
     if sys.platform.startswith('java'):
-        poll_fun = poll2
+        poll_fun = jython_poll_fun
 
     if count is None:
         while map:
@@ -306,6 +345,8 @@
     def set_socket(self, sock, map=None):
         self.socket = sock
 ##        self.__dict__['socket'] = sock
+        # On jython, the socket object itself is what is watchable.
+        # http://mail.python.org/pipermail/python-dev/2007-May/073443.html
         self._fileno = sock
         self.add_channel(map)
 

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list