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

login
register
mail settings
Submitter Pierre-Yves David
Date June 4, 2015, 1:57 a.m.
Message ID <9d7dc60dc701d327ddda.1433383076@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/9473/
State Accepted
Headers show

Comments

Pierre-Yves David - June 4, 2015, 1:57 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@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.
Adrian Buehlmann - June 4, 2015, 5:37 a.m.
On 2015-06-04 03:57, Pierre-Yves David wrote:
> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david@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
..
> 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

I think you can drop the rest of the doc string. Or perhaps instead
refer to posix module (in order to avoid comment duplication).

> +
> +    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()
> +

os.name is always 'nt' inside this module, so this can be simplified to:

       raise NotImplementedError()

(Nitpick: I think the comment ("we do not support windows yet.") is
unneeded. If you want to keep it: The correct name of that OS is 'Windows')

>  def readpipe(pipe):
>      """Read all available data from a pipe."""
>      chunks = []
>      while True:
>          size = win32.peekpipe(pipe)

Patch

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)