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

Pierre-Yves David pierre-yves.david at ens-lyon.org
Thu Jun 4 01:57:56 UTC 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 9d7dc60dc701d327ddda449e7455e85513d5df5b
# Parent  443d4635e630cf5603afb6ba4bfcf6a0d68cd7a6
util: add a simple poll utility

We'll use it to detect when 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/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -6,10 +6,11 @@
 # GNU General Public License version 2 or any later version.
 
 from i18n import _
 import encoding
 import os, sys, errno, stat, getpass, pwd, grp, socket, tempfile, unicodedata
+import select
 import fcntl, re
 
 posixfile = open
 normpath = os.path.normpath
 samestat = os.path.samestat
@@ -592,10 +593,24 @@ def statislink(st):
 
 def statisexec(st):
     '''check whether a stat result is an executable file'''
     return st and (st.st_mode & 0100 != 0)
 
+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"""
+    try:
+        res = select.select(fds, fds, fds)
+    except ValueError: # out of range file descriptor
+        raise NotImplementedError()
+    return sorted(list(set(sum(res, []))))
+
+
 def readpipe(pipe):
     """Read all available data from a pipe."""
     # We can't fstat() a pipe because Linux will always report 0.
     # So, we set the pipe to non-blocking mode and read everything
     # that's available.
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -52,10 +52,11 @@ normcasespec = platform.normcasespec
 normcasefallback = platform.normcasefallback
 openhardlinks = platform.openhardlinks
 oslink = platform.oslink
 parsepatchoutput = platform.parsepatchoutput
 pconvert = platform.pconvert
+poll = platform.poll
 popen = platform.popen
 posixfile = platform.posixfile
 quotecommand = platform.quotecommand
 readpipe = platform.readpipe
 rename = platform.rename
diff --git a/mercurial/windows.py b/mercurial/windows.py
--- a/mercurial/windows.py
+++ b/mercurial/windows.py
@@ -369,10 +369,20 @@ def statislink(st):
 
 def statisexec(st):
     '''check whether a stat result is an executable file'''
     return False
 
+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()
+
 def readpipe(pipe):
     """Read all available data from a pipe."""
     chunks = []
     while True:
         size = win32.peekpipe(pipe)


More information about the Mercurial-devel mailing list