Patchwork [5,of,6,py3] py3: rewrite stdout hack of doctest by using ui.pushbuffer()

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 17, 2017, 4:19 a.m.
Message ID <98d8cb8a7e43cb9500ee.1505621944@mimosa>
Download mbox | patch
Permalink /patch/23981/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 17, 2017, 4:19 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1505619554 -32400
#      Sun Sep 17 12:39:14 2017 +0900
# Node ID 98d8cb8a7e43cb9500eef73ae3ff75308950f778
# Parent  50be8fba5378fe303aa978f16888d0d423715a3e
py3: rewrite stdout hack of doctest by using ui.pushbuffer()

We can't use pycompat.stdout here because the doctest runner replaces
sys.stdout with a string buffer.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -45,9 +45,12 @@  Doctest helper:
 ...     import sys
 ...     from . import ui as uimod
 ...     ui = uimod.ui()
-...     ui.fout = sys.stdout  # redirect to doctest
 ...     ui.verbose = verbose
-...     return fn(ui, ui.formatter(fn.__name__, opts))
+...     ui.pushbuffer()
+...     try:
+...         return fn(ui, ui.formatter(fn.__name__, opts))
+...     finally:
+...         print(pycompat.sysstr(ui.popbuffer()), end='')
 
 Basic example:
 
@@ -101,7 +104,7 @@  bar
 baz: foo, bar
 """
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 import collections
 import contextlib