[PATCH 1 of 4] util: add a simple poll utility

Pierre-Yves David pierre-yves.david at ens-lyon.org
Wed Jun 3 15:26:30 CDT 2015


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1432162805 18000
#      Wed May 20 18:00:05 2015 -0500
# Node ID 0521446e1c5934bf377d8c6bd8a48baad6b57ae1
# Parent  d3b8690c4ecc2a60b8edd9f632f77824284b6027
util: add a simple poll utility

We'll use it to detect when the a sshpeer have server output to be displayed.

The implementation is super basic because all case support is not the focus of
this series.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -16,11 +16,11 @@ hide platform-specific details from the 
 import i18n
 _ = i18n._
 import error, osutil, encoding, parsers
 import errno, shutil, sys, tempfile, traceback
 import re as remod
-import os, time, datetime, calendar, textwrap, signal, collections
+import os, time, datetime, calendar, textwrap, signal, collections, sys, select
 import imp, socket, urllib
 import gc
 
 if os.name == 'nt':
     import windows as platform
@@ -327,10 +327,24 @@ class bufferedinputpipe(object):
             self._eof = True
         else:
             # inefficient add
             self._buffer.append(data)
 
+def poll(fds):
+    """block until something happened on any filedescriptors
+
+    This is a generic helper that will check for any activity
+    (read, write.  exception). return the list of touched file.
+
+    In unsupported case raise a NotImplementedError"""
+    if os.name == 'nt': # we do not support windows yet.
+        raise NotImplementedError()
+    if any(sys.maxint <= f for f in fds):
+        raise NotImplementedError()
+    res = select.select(fds, fds, fds)
+    return sorted(list(set(sum(res, []))))
+
 def popen2(cmd, env=None, newlines=False):
     # Setting bufsize to -1 lets the system decide the buffer size.
     # The default for bufsize is 0, meaning unbuffered. This leads to
     # poor performance on Mac OS X: http://bugs.python.org/issue4194
     p = subprocess.Popen(cmd, shell=True, bufsize=-1,


More information about the Mercurial-devel mailing list