# HG changeset patch # User Yuya Nishihara # Date 2015-10-03 06:16:33 # Node ID 62c5e937f4774e2ab820fa2a95ba8721196a996c # Parent 1a257841868923c92c444a0f551ca56e42c13ef3 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 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()