[PATCH 4 of 8] util: add public isstdin/isstdout() functions

Yuya Nishihara yuya at tcha.org
Fri Mar 9 07:35:37 EST 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1520324939 21600
#      Tue Mar 06 02:28:59 2018 -0600
# Node ID 10e3ea3d8b1dc671f6302ebed4489773c7d79458
# Parent  c9ac3a3b0a31abc37a76e5ae3be1b5f14a206cf3
util: add public isstdin/isstdout() functions

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1430,13 +1430,19 @@ def _sethgexecutable(path):
     global _hgexecutable
     _hgexecutable = path
 
-def _isstdout(f):
+def _testfileno(f, stdf):
     fileno = getattr(f, 'fileno', None)
     try:
-        return fileno and fileno() == sys.__stdout__.fileno()
+        return fileno and fileno() == stdf.fileno()
     except io.UnsupportedOperation:
         return False # fileno() raised UnsupportedOperation
 
+def isstdin(f):
+    return _testfileno(f, sys.__stdin__)
+
+def isstdout(f):
+    return _testfileno(f, sys.__stdout__)
+
 def shellenviron(environ=None):
     """return environ with optional override, useful for shelling out"""
     def py2shell(val):
@@ -1464,7 +1470,7 @@ def system(cmd, environ=None, cwd=None, 
         pass
     cmd = quotecommand(cmd)
     env = shellenviron(environ)
-    if out is None or _isstdout(out):
+    if out is None or isstdout(out):
         rc = subprocess.call(cmd, shell=True, close_fds=closefds,
                              env=env, cwd=cwd)
     else:


More information about the Mercurial-devel mailing list