Patchwork [1,of,5] util.system: compare fileno to see if it needs stdout redirection

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 4, 2015, 6:08 a.m.
Message ID <6567c8f93c6258de7101.1443938882@mimosa>
Download mbox | patch
Permalink /patch/10767/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 4, 2015, 6:08 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1443851844 -32400
#      Sat Oct 03 14:57:24 2015 +0900
# Node ID 6567c8f93c6258de7101a4ebebfaf6239aa11d7c
# Parent  d8e831ff6afa2b68d6f76617a66e2aadee010986
util.system: compare fileno to see if it needs stdout redirection

Future patches will reopen stdout to be line-buffered, so sys.stdout may
be different object than sys.__stdout__.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -730,6 +730,10 @@  def _sethgexecutable(path):
     global _hgexecutable
     _hgexecutable = path
 
+def _isstdout(f):
+    fileno = getattr(f, 'fileno', None)
+    return fileno and fileno() == sys.__stdout__.fileno()
+
 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None):
     '''enhanced shell command execution.
     run with environment maybe modified, maybe in different dir.
@@ -765,7 +769,7 @@  def system(cmd, environ=None, cwd=None, 
         env = dict(os.environ)
         env.update((k, py2shell(v)) for k, v in environ.iteritems())
         env['HG'] = hgexecutable()
-        if out is None or out == sys.__stdout__:
+        if out is None or _isstdout(out):
             rc = subprocess.call(cmd, shell=True, close_fds=closefds,
                                  env=env, cwd=cwd)
         else: