Patchwork [5,of,5] pager: recreate stdout to make it line-buffered

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

Comments

Yuya Nishihara - Oct. 4, 2015, 6:08 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1443852993 -32400
#      Sat Oct 03 15:16:33 2015 +0900
# Node ID 6684b267d4849742e1d60b93d445422a209db623
# Parent  fdf2f8de666bb352eedf13df7630d4499951fc39
pager: recreate stdout to make it line-buffered

We want to see partial command results as soon as possible. But the buffering
mode of stdout (= pager's stdin) was set to fully-buffered because it isn't
associated with a tty. So, this patch recreates new stdout object to force its
buffering mode.

Because two file objects are associated with the same stdout fd and their
destructors will call close(), one of them must be closed carefully. Python
expects that the stdout fd never be closed even after sys.stdout.close() [1],
but newstdout has no such hack. So this patch calls newstdout.close()
immediately before duplicating the original stdout fd to sys.stdout.

  operation              sys.stdout  newstdout  fd
  ---------------------  ----------  ---------  --------
  newstdout.close()      open        closed     closed
  os.dup2(stdoutfd, ..)  open        closed     open
  del sys.stdout         closed      closed     open [1]

 [1]: https://hg.python.org/cpython/file/v2.7.10/Python/sysmodule.c#l1391
Pierre-Yves David - Oct. 4, 2015, 8:29 a.m.
On 10/03/2015 11:08 PM, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1443852993 -32400
> #      Sat Oct 03 15:16:33 2015 +0900
> # Node ID 6684b267d4849742e1d60b93d445422a209db623
> # Parent  fdf2f8de666bb352eedf13df7630d4499951fc39
> pager: recreate stdout to make it line-buffered

Excellent, pushed to the clowcopter with much thanks.

Patch

diff --git a/hgext/pager.py b/hgext/pager.py
--- a/hgext/pager.py
+++ b/hgext/pager.py
@@ -70,8 +70,14 @@  def _runpager(ui, p):
                              close_fds=util.closefds, stdin=subprocess.PIPE,
                              stdout=sys.stdout, stderr=sys.stderr)
 
+    # back up original file objects and descriptors
+    olduifout = ui.fout
+    oldstdout = sys.stdout
     stdoutfd = os.dup(sys.stdout.fileno())
     stderrfd = os.dup(sys.stderr.fileno())
+
+    # create new line-buffered stdout so that output can show up immediately
+    ui.fout = sys.stdout = newstdout = os.fdopen(sys.stdout.fileno(), 'wb', 1)
     os.dup2(pager.stdin.fileno(), sys.stdout.fileno())
     if ui._isatty(sys.stderr):
         os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
@@ -81,6 +87,12 @@  def _runpager(ui, p):
         if util.safehasattr(signal, "SIGINT"):
             signal.signal(signal.SIGINT, signal.SIG_IGN)
         pager.stdin.close()
+        ui.fout = olduifout
+        sys.stdout = oldstdout
+        # close new stdout while it's associated with pager; otherwise stdout
+        # fd would be closed when newstdout is deleted
+        newstdout.close()
+        # restore original fds: stdout is open again
         os.dup2(stdoutfd, sys.stdout.fileno())
         os.dup2(stderrfd, sys.stderr.fileno())
         pager.wait()