##// END OF EJS Templates
doctest: use print_function and convert bytes to unicode where needed
Yuya Nishihara -
r34139:a8994d08 default
parent child Browse files
Show More
@@ -3,7 +3,8 b''
3 #
3 #
4 # This software may be used and distributed according to the terms of the
4 # This software may be used and distributed according to the terms of the
5 # GNU General Public License version 2 or any later version.
5 # GNU General Public License version 2 or any later version.
6 from __future__ import absolute_import
6
7 from __future__ import absolute_import, print_function
7
8
8 import posixpath
9 import posixpath
9 import shlex
10 import shlex
@@ -18,7 +19,7 b' SKIPREV = common.SKIPREV'
18 def rpairs(path):
19 def rpairs(path):
19 '''Yield tuples with path split at '/', starting with the full path.
20 '''Yield tuples with path split at '/', starting with the full path.
20 No leading, trailing or double '/', please.
21 No leading, trailing or double '/', please.
21 >>> for x in rpairs(b'foo/bar/baz'): print x
22 >>> for x in rpairs(b'foo/bar/baz'): print(x)
22 ('foo/bar/baz', '')
23 ('foo/bar/baz', '')
23 ('foo/bar', 'baz')
24 ('foo/bar', 'baz')
24 ('foo', 'bar/baz')
25 ('foo', 'bar/baz')
@@ -62,7 +62,7 b' This extension used to provide a strip c'
62 in the strip extension.
62 in the strip extension.
63 '''
63 '''
64
64
65 from __future__ import absolute_import
65 from __future__ import absolute_import, print_function
66
66
67 import errno
67 import errno
68 import os
68 import os
@@ -155,7 +155,7 b' PLAINHEADERS = {'
155 def inserthgheader(lines, header, value):
155 def inserthgheader(lines, header, value):
156 """Assuming lines contains a HG patch header, add a header line with value.
156 """Assuming lines contains a HG patch header, add a header line with value.
157 >>> try: inserthgheader([], b'# Date ', b'z')
157 >>> try: inserthgheader([], b'# Date ', b'z')
158 ... except ValueError, inst: print "oops"
158 ... except ValueError, inst: print("oops")
159 oops
159 oops
160 >>> inserthgheader([b'# HG changeset patch'], b'# Date ', b'z')
160 >>> inserthgheader([b'# HG changeset patch'], b'# Date ', b'z')
161 ['# HG changeset patch', '# Date z']
161 ['# HG changeset patch', '# Date z']
@@ -155,8 +155,9 b' def parsedag(desc):'
155
155
156 Error:
156 Error:
157
157
158 >>> from . import pycompat
158 >>> try: list(parsedag(b'+1 bad'))
159 >>> try: list(parsedag(b'+1 bad'))
159 ... except Exception, e: print(e)
160 ... except Exception, e: print(pycompat.sysstr(bytes(e)))
160 invalid character in dag description: bad...
161 invalid character in dag description: bad...
161
162
162 '''
163 '''
@@ -5,7 +5,7 b''
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import, print_function
9
9
10 import io
10 import io
11 import locale
11 import locale
@@ -248,60 +248,62 b" def trim(s, width, ellipsis='', leftside"
248 'ellipsis' is always placed at trimmed side.
248 'ellipsis' is always placed at trimmed side.
249
249
250 >>> from .node import bin
250 >>> from .node import bin
251 >>> def bprint(s):
252 ... print(pycompat.sysstr(s))
251 >>> ellipsis = b'+++'
253 >>> ellipsis = b'+++'
252 >>> from . import encoding
254 >>> from . import encoding
253 >>> encoding.encoding = b'utf-8'
255 >>> encoding.encoding = b'utf-8'
254 >>> t = b'1234567890'
256 >>> t = b'1234567890'
255 >>> print trim(t, 12, ellipsis=ellipsis)
257 >>> bprint(trim(t, 12, ellipsis=ellipsis))
256 1234567890
258 1234567890
257 >>> print trim(t, 10, ellipsis=ellipsis)
259 >>> bprint(trim(t, 10, ellipsis=ellipsis))
258 1234567890
260 1234567890
259 >>> print trim(t, 8, ellipsis=ellipsis)
261 >>> bprint(trim(t, 8, ellipsis=ellipsis))
260 12345+++
262 12345+++
261 >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
263 >>> bprint(trim(t, 8, ellipsis=ellipsis, leftside=True))
262 +++67890
264 +++67890
263 >>> print trim(t, 8)
265 >>> bprint(trim(t, 8))
264 12345678
266 12345678
265 >>> print trim(t, 8, leftside=True)
267 >>> bprint(trim(t, 8, leftside=True))
266 34567890
268 34567890
267 >>> print trim(t, 3, ellipsis=ellipsis)
269 >>> bprint(trim(t, 3, ellipsis=ellipsis))
268 +++
270 +++
269 >>> print trim(t, 1, ellipsis=ellipsis)
271 >>> bprint(trim(t, 1, ellipsis=ellipsis))
270 +
272 +
271 >>> u = u'\u3042\u3044\u3046\u3048\u304a' # 2 x 5 = 10 columns
273 >>> u = u'\u3042\u3044\u3046\u3048\u304a' # 2 x 5 = 10 columns
272 >>> t = u.encode(pycompat.sysstr(encoding.encoding))
274 >>> t = u.encode(pycompat.sysstr(encoding.encoding))
273 >>> print trim(t, 12, ellipsis=ellipsis)
275 >>> bprint(trim(t, 12, ellipsis=ellipsis))
274 \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a
276 \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a
275 >>> print trim(t, 10, ellipsis=ellipsis)
277 >>> bprint(trim(t, 10, ellipsis=ellipsis))
276 \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a
278 \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a
277 >>> print trim(t, 8, ellipsis=ellipsis)
279 >>> bprint(trim(t, 8, ellipsis=ellipsis))
278 \xe3\x81\x82\xe3\x81\x84+++
280 \xe3\x81\x82\xe3\x81\x84+++
279 >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
281 >>> bprint(trim(t, 8, ellipsis=ellipsis, leftside=True))
280 +++\xe3\x81\x88\xe3\x81\x8a
282 +++\xe3\x81\x88\xe3\x81\x8a
281 >>> print trim(t, 5)
283 >>> bprint(trim(t, 5))
282 \xe3\x81\x82\xe3\x81\x84
284 \xe3\x81\x82\xe3\x81\x84
283 >>> print trim(t, 5, leftside=True)
285 >>> bprint(trim(t, 5, leftside=True))
284 \xe3\x81\x88\xe3\x81\x8a
286 \xe3\x81\x88\xe3\x81\x8a
285 >>> print trim(t, 4, ellipsis=ellipsis)
287 >>> bprint(trim(t, 4, ellipsis=ellipsis))
286 +++
288 +++
287 >>> print trim(t, 4, ellipsis=ellipsis, leftside=True)
289 >>> bprint(trim(t, 4, ellipsis=ellipsis, leftside=True))
288 +++
290 +++
289 >>> t = bin(b'112233445566778899aa') # invalid byte sequence
291 >>> t = bin(b'112233445566778899aa') # invalid byte sequence
290 >>> print trim(t, 12, ellipsis=ellipsis)
292 >>> bprint(trim(t, 12, ellipsis=ellipsis))
291 \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa
293 \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa
292 >>> print trim(t, 10, ellipsis=ellipsis)
294 >>> bprint(trim(t, 10, ellipsis=ellipsis))
293 \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa
295 \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa
294 >>> print trim(t, 8, ellipsis=ellipsis)
296 >>> bprint(trim(t, 8, ellipsis=ellipsis))
295 \x11\x22\x33\x44\x55+++
297 \x11\x22\x33\x44\x55+++
296 >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
298 >>> bprint(trim(t, 8, ellipsis=ellipsis, leftside=True))
297 +++\x66\x77\x88\x99\xaa
299 +++\x66\x77\x88\x99\xaa
298 >>> print trim(t, 8)
300 >>> bprint(trim(t, 8))
299 \x11\x22\x33\x44\x55\x66\x77\x88
301 \x11\x22\x33\x44\x55\x66\x77\x88
300 >>> print trim(t, 8, leftside=True)
302 >>> bprint(trim(t, 8, leftside=True))
301 \x33\x44\x55\x66\x77\x88\x99\xaa
303 \x33\x44\x55\x66\x77\x88\x99\xaa
302 >>> print trim(t, 3, ellipsis=ellipsis)
304 >>> bprint(trim(t, 3, ellipsis=ellipsis))
303 +++
305 +++
304 >>> print trim(t, 1, ellipsis=ellipsis)
306 >>> bprint(trim(t, 1, ellipsis=ellipsis))
305 +
307 +
306 """
308 """
307 try:
309 try:
@@ -5,7 +5,7 b''
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import, print_function
9
9
10 import copy
10 import copy
11 import os
11 import os
@@ -580,6 +580,7 b' class subdirmatcher(basematcher):'
580
580
581 The paths are remapped to remove/insert the path as needed:
581 The paths are remapped to remove/insert the path as needed:
582
582
583 >>> from . import pycompat
583 >>> m1 = match(b'root', b'', [b'a.txt', b'sub/b.txt'])
584 >>> m1 = match(b'root', b'', [b'a.txt', b'sub/b.txt'])
584 >>> m2 = subdirmatcher(b'sub', m1)
585 >>> m2 = subdirmatcher(b'sub', m1)
585 >>> bool(m2(b'a.txt'))
586 >>> bool(m2(b'a.txt'))
@@ -597,7 +598,7 b' class subdirmatcher(basematcher):'
597 >>> util.pconvert(m2.rel(b'b.txt'))
598 >>> util.pconvert(m2.rel(b'b.txt'))
598 'sub/b.txt'
599 'sub/b.txt'
599 >>> def bad(f, msg):
600 >>> def bad(f, msg):
600 ... print b"%s: %s" % (f, msg)
601 ... print(pycompat.sysstr(b"%s: %s" % (f, msg)))
601 >>> m1.bad = bad
602 >>> m1.bad = bad
602 >>> m2.bad(b'x.txt', b'No such file')
603 >>> m2.bad(b'x.txt', b'No such file')
603 sub/x.txt: No such file
604 sub/x.txt: No such file
@@ -703,21 +704,24 b' def _patsplit(pattern, default):'
703 def _globre(pat):
704 def _globre(pat):
704 r'''Convert an extended glob string to a regexp string.
705 r'''Convert an extended glob string to a regexp string.
705
706
706 >>> print _globre(br'?')
707 >>> from . import pycompat
708 >>> def bprint(s):
709 ... print(pycompat.sysstr(s))
710 >>> bprint(_globre(br'?'))
707 .
711 .
708 >>> print _globre(br'*')
712 >>> bprint(_globre(br'*'))
709 [^/]*
713 [^/]*
710 >>> print _globre(br'**')
714 >>> bprint(_globre(br'**'))
711 .*
715 .*
712 >>> print _globre(br'**/a')
716 >>> bprint(_globre(br'**/a'))
713 (?:.*/)?a
717 (?:.*/)?a
714 >>> print _globre(br'a/**/b')
718 >>> bprint(_globre(br'a/**/b'))
715 a\/(?:.*/)?b
719 a\/(?:.*/)?b
716 >>> print _globre(br'[a*?!^][^b][!c]')
720 >>> bprint(_globre(br'[a*?!^][^b][!c]'))
717 [a*?!^][\^b][^c]
721 [a*?!^][\^b][^c]
718 >>> print _globre(br'{a,b}')
722 >>> bprint(_globre(br'{a,b}'))
719 (?:a|b)
723 (?:a|b)
720 >>> print _globre(br'.\*\?')
724 >>> bprint(_globre(br'.\*\?'))
721 \.\*\?
725 \.\*\?
722 '''
726 '''
723 i, n = 0, len(pat)
727 i, n = 0, len(pat)
@@ -16,7 +16,7 b''
16 # an action is a tree node name, a tree label, and an optional match
16 # an action is a tree node name, a tree label, and an optional match
17 # __call__(program) parses program into a labeled tree
17 # __call__(program) parses program into a labeled tree
18
18
19 from __future__ import absolute_import
19 from __future__ import absolute_import, print_function
20
20
21 from .i18n import _
21 from .i18n import _
22 from . import (
22 from . import (
@@ -220,8 +220,10 b' def prettyformat(tree, leafnodes):'
220 def simplifyinfixops(tree, targetnodes):
220 def simplifyinfixops(tree, targetnodes):
221 """Flatten chained infix operations to reduce usage of Python stack
221 """Flatten chained infix operations to reduce usage of Python stack
222
222
223 >>> from . import pycompat
223 >>> def f(tree):
224 >>> def f(tree):
224 ... print prettyformat(simplifyinfixops(tree, (b'or',)), (b'symbol',))
225 ... s = prettyformat(simplifyinfixops(tree, (b'or',)), (b'symbol',))
226 ... print(pycompat.sysstr(s))
225 >>> f((b'or',
227 >>> f((b'or',
226 ... (b'or',
228 ... (b'or',
227 ... (b'symbol', b'1'),
229 ... (b'symbol', b'1'),
@@ -555,6 +557,7 b' class basealiasrules(object):'
555 ``args`` is a list of alias argument names, or None if the alias
557 ``args`` is a list of alias argument names, or None if the alias
556 is declared as a symbol.
558 is declared as a symbol.
557
559
560 >>> from . import pycompat
558 >>> parsemap = {
561 >>> parsemap = {
559 ... b'$1 or foo': (b'or', (b'symbol', b'$1'), (b'symbol', b'foo')),
562 ... b'$1 or foo': (b'or', (b'symbol', b'$1'), (b'symbol', b'foo')),
560 ... b'$1 or $bar':
563 ... b'$1 or $bar':
@@ -569,7 +572,8 b' class basealiasrules(object):'
569 ... _trygetfunc = staticmethod(lambda x: None)
572 ... _trygetfunc = staticmethod(lambda x: None)
570 >>> builddefn = aliasrules._builddefn
573 >>> builddefn = aliasrules._builddefn
571 >>> def pprint(tree):
574 >>> def pprint(tree):
572 ... print prettyformat(tree, (b'_aliasarg', b'string', b'symbol'))
575 ... s = prettyformat(tree, (b'_aliasarg', b'string', b'symbol'))
576 ... print(pycompat.sysstr(s))
573 >>> args = [b'$1', b'$2', b'foo']
577 >>> args = [b'$1', b'$2', b'foo']
574 >>> pprint(builddefn(b'$1 or foo', args))
578 >>> pprint(builddefn(b'$1 or foo', args))
575 (or
579 (or
@@ -578,7 +582,7 b' class basealiasrules(object):'
578 >>> try:
582 >>> try:
579 ... builddefn(b'$1 or $bar', args)
583 ... builddefn(b'$1 or $bar', args)
580 ... except error.ParseError as inst:
584 ... except error.ParseError as inst:
581 ... print parseerrordetail(inst)
585 ... print(pycompat.sysstr(parseerrordetail(inst)))
582 invalid symbol '$bar'
586 invalid symbol '$bar'
583 >>> args = [b'$1', b'$10', b'foo']
587 >>> args = [b'$1', b'$10', b'foo']
584 >>> pprint(builddefn(b'$10 or baz', args))
588 >>> pprint(builddefn(b'$10 or baz', args))
@@ -6,7 +6,7 b''
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9 from __future__ import absolute_import
9 from __future__ import absolute_import, print_function
10
10
11 import collections
11 import collections
12 import copy
12 import copy
@@ -1505,7 +1505,7 b' def reversehunks(hunks):'
1505 ... c.write(fp)
1505 ... c.write(fp)
1506 >>> fp.seek(0)
1506 >>> fp.seek(0)
1507 >>> reversedpatch = fp.read()
1507 >>> reversedpatch = fp.read()
1508 >>> print reversedpatch
1508 >>> print(pycompat.sysstr(reversedpatch))
1509 diff --git a/folder1/g b/folder1/g
1509 diff --git a/folder1/g b/folder1/g
1510 --- a/folder1/g
1510 --- a/folder1/g
1511 +++ b/folder1/g
1511 +++ b/folder1/g
@@ -1562,7 +1562,7 b' def parsepatch(originalchunks, maxcontex'
1562 ... header.write(out)
1562 ... header.write(out)
1563 ... for hunk in header.hunks:
1563 ... for hunk in header.hunks:
1564 ... hunk.write(out)
1564 ... hunk.write(out)
1565 >>> print(out.getvalue())
1565 >>> print(pycompat.sysstr(out.getvalue()))
1566 diff --git a/folder1/g b/folder1/g
1566 diff --git a/folder1/g b/folder1/g
1567 --- a/folder1/g
1567 --- a/folder1/g
1568 +++ b/folder1/g
1568 +++ b/folder1/g
@@ -5,7 +5,7 b''
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import, print_function
9
9
10 import os
10 import os
11 import re
11 import re
@@ -192,7 +192,7 b' def _unnesttemplatelist(tree):'
192 """Expand list of templates to node tuple
192 """Expand list of templates to node tuple
193
193
194 >>> def f(tree):
194 >>> def f(tree):
195 ... print prettyformat(_unnesttemplatelist(tree))
195 ... print(pycompat.sysstr(prettyformat(_unnesttemplatelist(tree))))
196 >>> f((b'template', []))
196 >>> f((b'template', []))
197 (string '')
197 (string '')
198 >>> f((b'template', [(b'string', b'foo')]))
198 >>> f((b'template', [(b'string', b'foo')]))
@@ -13,7 +13,7 b' This contains helper routines that are i'
13 hide platform-specific details from the core.
13 hide platform-specific details from the core.
14 """
14 """
15
15
16 from __future__ import absolute_import
16 from __future__ import absolute_import, print_function
17
17
18 import abc
18 import abc
19 import bz2
19 import bz2
@@ -2836,9 +2836,9 b' class url(object):'
2836 'file:///tmp/foo/bar'
2836 'file:///tmp/foo/bar'
2837 >>> bytes(url(b'file:///c:/tmp/foo/bar'))
2837 >>> bytes(url(b'file:///c:/tmp/foo/bar'))
2838 'file:///c:/tmp/foo/bar'
2838 'file:///c:/tmp/foo/bar'
2839 >>> print url(br'bundle:foo\bar')
2839 >>> print(url(br'bundle:foo\bar'))
2840 bundle:foo\bar
2840 bundle:foo\bar
2841 >>> print url(br'file:///D:\data\hg')
2841 >>> print(url(br'file:///D:\data\hg'))
2842 file:///D:\data\hg
2842 file:///D:\data\hg
2843 """
2843 """
2844 if self._localpath:
2844 if self._localpath:
General Comments 0
You need to be logged in to leave comments. Login now