[PATCH 06 of 10] py3: add support to pass bool type variable into pycompat.sysbytes()
Pulkit Goyal
7895pulkit at gmail.com
Wed May 31 17:47:02 EDT 2017
# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1496262135 -19800
# Thu Jun 01 01:52:15 2017 +0530
# Node ID 471f42c23f0c8793ee8bcd589940eb28a6d32106
# Parent 8a1b753a1eac83bd2b07ad1b3aa454e383794329
py3: add support to pass bool type variable into pycompat.sysbytes()
On Python 3, the way to convert a bool type variable to bytes type is to first
convert the bool type to str and then encode the str to bytes.
>>> ab = "abc"
>>> bv = bool(ab)
>>> type(bv)
<class 'bool'>
>>> "%b" % bv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: unsupported format character 'b' (0x62) at index 1
>>> "%s" % bv
'True'
>>> ("%s" % bv).encode('ascii')
b'True'
diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -159,7 +159,12 @@
This never raises UnicodeEncodeError, but only ASCII characters
can be round-trip by sysstr(sysbytes(s)).
"""
- return s.encode(u'utf-8')
+ try:
+ return s.encode(u'utf-8')
+ except AttributeError:
+ if isinstance(s, bool):
+ return (r'%s' % s).encode(u'ascii')
+ raise
def sysstr(s):
"""Return a keyword str to be passed to Python functions such as
More information about the Mercurial-devel
mailing list