Patchwork [07,of,11] doctest: use print_function and convert bytes to unicode where needed

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 4, 2017, 3:08 p.m.
Message ID <9a8de7f63768bf73e6a6.1504537706@mimosa>
Download mbox | patch
Permalink /patch/23662/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 4, 2017, 3:08 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1504418191 -32400
#      Sun Sep 03 14:56:31 2017 +0900
# Node ID 9a8de7f63768bf73e6a6c7e66f8ca43e309a427c
# Parent  cf6af65e933cb3048568e403ca473eee34351c6b
doctest: use print_function and convert bytes to unicode where needed

Patch

diff --git a/hgext/convert/filemap.py b/hgext/convert/filemap.py
--- a/hgext/convert/filemap.py
+++ b/hgext/convert/filemap.py
@@ -3,7 +3,8 @@ 
 #
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
-from __future__ import absolute_import
+
+from __future__ import absolute_import, print_function
 
 import posixpath
 import shlex
@@ -18,7 +19,7 @@  SKIPREV = common.SKIPREV
 def rpairs(path):
     '''Yield tuples with path split at '/', starting with the full path.
     No leading, trailing or double '/', please.
-    >>> for x in rpairs(b'foo/bar/baz'): print x
+    >>> for x in rpairs(b'foo/bar/baz'): print(x)
     ('foo/bar/baz', '')
     ('foo/bar', 'baz')
     ('foo', 'bar/baz')
diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -62,7 +62,7 @@  This extension used to provide a strip c
 in the strip extension.
 '''
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 import errno
 import os
@@ -155,7 +155,7 @@  PLAINHEADERS = {
 def inserthgheader(lines, header, value):
     """Assuming lines contains a HG patch header, add a header line with value.
     >>> try: inserthgheader([], b'# Date ', b'z')
-    ... except ValueError, inst: print "oops"
+    ... except ValueError, inst: print("oops")
     oops
     >>> inserthgheader([b'# HG changeset patch'], b'# Date ', b'z')
     ['# HG changeset patch', '# Date z']
diff --git a/mercurial/dagparser.py b/mercurial/dagparser.py
--- a/mercurial/dagparser.py
+++ b/mercurial/dagparser.py
@@ -155,8 +155,9 @@  def parsedag(desc):
 
     Error:
 
+        >>> from . import pycompat
         >>> try: list(parsedag(b'+1 bad'))
-        ... except Exception, e: print(e)
+        ... except Exception, e: print(pycompat.sysstr(bytes(e)))
         invalid character in dag description: bad...
 
     '''
diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -5,7 +5,7 @@ 
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 import io
 import locale
@@ -248,60 +248,62 @@  def trim(s, width, ellipsis='', leftside
     'ellipsis' is always placed at trimmed side.
 
     >>> from .node import bin
+    >>> def bprint(s):
+    ...     print(pycompat.sysstr(s))
     >>> ellipsis = b'+++'
     >>> from . import encoding
     >>> encoding.encoding = b'utf-8'
     >>> t = b'1234567890'
-    >>> print trim(t, 12, ellipsis=ellipsis)
+    >>> bprint(trim(t, 12, ellipsis=ellipsis))
     1234567890
-    >>> print trim(t, 10, ellipsis=ellipsis)
+    >>> bprint(trim(t, 10, ellipsis=ellipsis))
     1234567890
-    >>> print trim(t, 8, ellipsis=ellipsis)
+    >>> bprint(trim(t, 8, ellipsis=ellipsis))
     12345+++
-    >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
+    >>> bprint(trim(t, 8, ellipsis=ellipsis, leftside=True))
     +++67890
-    >>> print trim(t, 8)
+    >>> bprint(trim(t, 8))
     12345678
-    >>> print trim(t, 8, leftside=True)
+    >>> bprint(trim(t, 8, leftside=True))
     34567890
-    >>> print trim(t, 3, ellipsis=ellipsis)
+    >>> bprint(trim(t, 3, ellipsis=ellipsis))
     +++
-    >>> print trim(t, 1, ellipsis=ellipsis)
+    >>> bprint(trim(t, 1, ellipsis=ellipsis))
     +
     >>> u = u'\u3042\u3044\u3046\u3048\u304a' # 2 x 5 = 10 columns
     >>> t = u.encode(pycompat.sysstr(encoding.encoding))
-    >>> print trim(t, 12, ellipsis=ellipsis)
+    >>> bprint(trim(t, 12, ellipsis=ellipsis))
     \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a
-    >>> print trim(t, 10, ellipsis=ellipsis)
+    >>> bprint(trim(t, 10, ellipsis=ellipsis))
     \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a
-    >>> print trim(t, 8, ellipsis=ellipsis)
+    >>> bprint(trim(t, 8, ellipsis=ellipsis))
     \xe3\x81\x82\xe3\x81\x84+++
-    >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
+    >>> bprint(trim(t, 8, ellipsis=ellipsis, leftside=True))
     +++\xe3\x81\x88\xe3\x81\x8a
-    >>> print trim(t, 5)
+    >>> bprint(trim(t, 5))
     \xe3\x81\x82\xe3\x81\x84
-    >>> print trim(t, 5, leftside=True)
+    >>> bprint(trim(t, 5, leftside=True))
     \xe3\x81\x88\xe3\x81\x8a
-    >>> print trim(t, 4, ellipsis=ellipsis)
+    >>> bprint(trim(t, 4, ellipsis=ellipsis))
     +++
-    >>> print trim(t, 4, ellipsis=ellipsis, leftside=True)
+    >>> bprint(trim(t, 4, ellipsis=ellipsis, leftside=True))
     +++
     >>> t = bin(b'112233445566778899aa') # invalid byte sequence
-    >>> print trim(t, 12, ellipsis=ellipsis)
+    >>> bprint(trim(t, 12, ellipsis=ellipsis))
     \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa
-    >>> print trim(t, 10, ellipsis=ellipsis)
+    >>> bprint(trim(t, 10, ellipsis=ellipsis))
     \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa
-    >>> print trim(t, 8, ellipsis=ellipsis)
+    >>> bprint(trim(t, 8, ellipsis=ellipsis))
     \x11\x22\x33\x44\x55+++
-    >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
+    >>> bprint(trim(t, 8, ellipsis=ellipsis, leftside=True))
     +++\x66\x77\x88\x99\xaa
-    >>> print trim(t, 8)
+    >>> bprint(trim(t, 8))
     \x11\x22\x33\x44\x55\x66\x77\x88
-    >>> print trim(t, 8, leftside=True)
+    >>> bprint(trim(t, 8, leftside=True))
     \x33\x44\x55\x66\x77\x88\x99\xaa
-    >>> print trim(t, 3, ellipsis=ellipsis)
+    >>> bprint(trim(t, 3, ellipsis=ellipsis))
     +++
-    >>> print trim(t, 1, ellipsis=ellipsis)
+    >>> bprint(trim(t, 1, ellipsis=ellipsis))
     +
     """
     try:
diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -5,7 +5,7 @@ 
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 import copy
 import os
@@ -580,6 +580,7 @@  class subdirmatcher(basematcher):
 
     The paths are remapped to remove/insert the path as needed:
 
+    >>> from . import pycompat
     >>> m1 = match(b'root', b'', [b'a.txt', b'sub/b.txt'])
     >>> m2 = subdirmatcher(b'sub', m1)
     >>> bool(m2(b'a.txt'))
@@ -597,7 +598,7 @@  class subdirmatcher(basematcher):
     >>> util.pconvert(m2.rel(b'b.txt'))
     'sub/b.txt'
     >>> def bad(f, msg):
-    ...     print b"%s: %s" % (f, msg)
+    ...     print(pycompat.sysstr(b"%s: %s" % (f, msg)))
     >>> m1.bad = bad
     >>> m2.bad(b'x.txt', b'No such file')
     sub/x.txt: No such file
@@ -703,21 +704,24 @@  def _patsplit(pattern, default):
 def _globre(pat):
     r'''Convert an extended glob string to a regexp string.
 
-    >>> print _globre(br'?')
+    >>> from . import pycompat
+    >>> def bprint(s):
+    ...     print(pycompat.sysstr(s))
+    >>> bprint(_globre(br'?'))
     .
-    >>> print _globre(br'*')
+    >>> bprint(_globre(br'*'))
     [^/]*
-    >>> print _globre(br'**')
+    >>> bprint(_globre(br'**'))
     .*
-    >>> print _globre(br'**/a')
+    >>> bprint(_globre(br'**/a'))
     (?:.*/)?a
-    >>> print _globre(br'a/**/b')
+    >>> bprint(_globre(br'a/**/b'))
     a\/(?:.*/)?b
-    >>> print _globre(br'[a*?!^][^b][!c]')
+    >>> bprint(_globre(br'[a*?!^][^b][!c]'))
     [a*?!^][\^b][^c]
-    >>> print _globre(br'{a,b}')
+    >>> bprint(_globre(br'{a,b}'))
     (?:a|b)
-    >>> print _globre(br'.\*\?')
+    >>> bprint(_globre(br'.\*\?'))
     \.\*\?
     '''
     i, n = 0, len(pat)
diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -16,7 +16,7 @@ 
 # an action is a tree node name, a tree label, and an optional match
 # __call__(program) parses program into a labeled tree
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 from .i18n import _
 from . import (
@@ -220,8 +220,10 @@  def prettyformat(tree, leafnodes):
 def simplifyinfixops(tree, targetnodes):
     """Flatten chained infix operations to reduce usage of Python stack
 
+    >>> from . import pycompat
     >>> def f(tree):
-    ...     print prettyformat(simplifyinfixops(tree, (b'or',)), (b'symbol',))
+    ...     s = prettyformat(simplifyinfixops(tree, (b'or',)), (b'symbol',))
+    ...     print(pycompat.sysstr(s))
     >>> f((b'or',
     ...     (b'or',
     ...       (b'symbol', b'1'),
@@ -555,6 +557,7 @@  class basealiasrules(object):
         ``args`` is a list of alias argument names, or None if the alias
         is declared as a symbol.
 
+        >>> from . import pycompat
         >>> parsemap = {
         ...     b'$1 or foo': (b'or', (b'symbol', b'$1'), (b'symbol', b'foo')),
         ...     b'$1 or $bar':
@@ -569,7 +572,8 @@  class basealiasrules(object):
         ...     _trygetfunc = staticmethod(lambda x: None)
         >>> builddefn = aliasrules._builddefn
         >>> def pprint(tree):
-        ...     print prettyformat(tree, (b'_aliasarg', b'string', b'symbol'))
+        ...     s = prettyformat(tree, (b'_aliasarg', b'string', b'symbol'))
+        ...     print(pycompat.sysstr(s))
         >>> args = [b'$1', b'$2', b'foo']
         >>> pprint(builddefn(b'$1 or foo', args))
         (or
@@ -578,7 +582,7 @@  class basealiasrules(object):
         >>> try:
         ...     builddefn(b'$1 or $bar', args)
         ... except error.ParseError as inst:
-        ...     print parseerrordetail(inst)
+        ...     print(pycompat.sysstr(parseerrordetail(inst)))
         invalid symbol '$bar'
         >>> args = [b'$1', b'$10', b'foo']
         >>> pprint(builddefn(b'$10 or baz', args))
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -6,7 +6,7 @@ 
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 import collections
 import copy
@@ -1505,7 +1505,7 @@  def reversehunks(hunks):
     ...      c.write(fp)
     >>> fp.seek(0)
     >>> reversedpatch = fp.read()
-    >>> print reversedpatch
+    >>> print(pycompat.sysstr(reversedpatch))
     diff --git a/folder1/g b/folder1/g
     --- a/folder1/g
     +++ b/folder1/g
@@ -1562,7 +1562,7 @@  def parsepatch(originalchunks, maxcontex
     ...     header.write(out)
     ...     for hunk in header.hunks:
     ...         hunk.write(out)
-    >>> print(out.getvalue())
+    >>> print(pycompat.sysstr(out.getvalue()))
     diff --git a/folder1/g b/folder1/g
     --- a/folder1/g
     +++ b/folder1/g
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -5,7 +5,7 @@ 
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 import os
 import re
@@ -192,7 +192,7 @@  def _unnesttemplatelist(tree):
     """Expand list of templates to node tuple
 
     >>> def f(tree):
-    ...     print prettyformat(_unnesttemplatelist(tree))
+    ...     print(pycompat.sysstr(prettyformat(_unnesttemplatelist(tree))))
     >>> f((b'template', []))
     (string '')
     >>> f((b'template', [(b'string', b'foo')]))
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -13,7 +13,7 @@  This contains helper routines that are i
 hide platform-specific details from the core.
 """
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 import abc
 import bz2
@@ -2843,9 +2843,9 @@  class url(object):
         'file:///tmp/foo/bar'
         >>> bytes(url(b'file:///c:/tmp/foo/bar'))
         'file:///c:/tmp/foo/bar'
-        >>> print url(br'bundle:foo\bar')
+        >>> print(url(br'bundle:foo\bar'))
         bundle:foo\bar
-        >>> print url(br'file:///D:\data\hg')
+        >>> print(url(br'file:///D:\data\hg'))
         file:///D:\data\hg
         """
         if self._localpath: