comparison mercurial/py3kcompat.py @ 11748:37a70a784397

py3kcompat: added a "compatibility layer" for py3k This patch adds some ugly constructs. The first of them is bytesformatter, a function that formats strings like when '%' is called. The main motivation for this function is py3k's strange behavior: >>> 'foo %s' % b'bar' "foo b'bar'" >>> b'foo %s' % b'bar' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for %: 'bytes' and 'bytes' >>> b'foo %s' % 'bar' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for %: 'bytes' and 'str' In other words, if we can't format bytes with bytes, and recall that all mercurial strings will be converted by a fixer, then things will break badly if we don't take a similar approach. The other addition with this patch is that the os.environ dictionary is monkeypatched to have bytes items. Hopefully this won't be needed in the future, as python 3.2 might get a os.environb dictionary that holds bytes items.
author Renato Cunha <renatoc@gmail.com>
date Tue, 03 Aug 2010 13:52:48 -0300
parents
children 8bb1481cf08f
comparison
equal deleted inserted replaced
11747:40d5633889bb 11748:37a70a784397
1 # py3kcompat.py - compatibility definitions for running hg in py3k
2 #
3 # Copyright 2010 Renato Cunha <renatoc@gmail.com>
4 #
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.
7
8 import os, builtins
9
10 from numbers import Number
11
12 def bytesformatter(format, args):
13 '''Custom implementation of a formatter for bytestrings.
14
15 This function currently relias on the string formatter to do the
16 formatting and always returns bytes objects.
17
18 >>> bytesformatter(20, 10)
19 0
20 >>> bytesformatter('unicode %s, %s!', ('string', 'foo'))
21 b'unicode string, foo!'
22 >>> bytesformatter(b'test %s', 'me')
23 b'test me'
24 >>> bytesformatter('test %s', 'me')
25 b'test me'
26 >>> bytesformatter(b'test %s', b'me')
27 b'test me'
28 >>> bytesformatter('test %s', b'me')
29 b'test me'
30 >>> bytesformatter('test %d: %s', (1, b'result'))
31 b'test 1: result'
32 '''
33 # The current implementation just converts from bytes to unicode, do
34 # what's needed and then convert the results back to bytes.
35 # Another alternative is to use the Python C API implementation.
36 if isinstance(format, Number):
37 # If the fixer erroneously passes a number remainder operation to
38 # bytesformatter, we just return the correct operation
39 return format % args
40 if isinstance(format, bytes):
41 format = format.decode('utf-8', 'surrogateescape')
42 if isinstance(args, bytes):
43 args = args.decode('utf-8', 'surrogateescape')
44 if isinstance(args, tuple):
45 newargs = []
46 for arg in args:
47 if isinstance(arg, bytes):
48 arg = arg.decode('utf-8', 'surrogateescape')
49 newargs.append(arg)
50 args = tuple(newargs)
51 ret = format % args
52 return ret.encode('utf-8', 'surrogateescape')
53 builtins.bytesformatter = bytesformatter
54
55 # Create bytes equivalents for os.environ values
56 for key in list(os.environ.keys()):
57 # UTF-8 is fine for us
58 bkey = key.encode('utf-8', 'surrogateescape')
59 bvalue = os.environ[key].encode('utf-8', 'surrogateescape')
60 os.environ[bkey] = bvalue
61
62 if __name__ == '__main__':
63 import doctest
64 doctest.testmod()
65