Mercurial > public > mercurial-scm > hg
comparison mercurial/pycompat.py @ 35903:1a31111e6239
py3: always drop b'' prefix from repr() of bytestr
Perhaps this is what we wanted for py2-3 compatibility.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 27 Jan 2018 17:31:25 +0900 |
parents | e66d6e938d2d |
children | fc44c2657dc5 |
comparison
equal
deleted
inserted
replaced
35902:2da4144e6716 | 35903:1a31111e6239 |
---|---|
86 | 86 |
87 class bytestr(bytes): | 87 class bytestr(bytes): |
88 """A bytes which mostly acts as a Python 2 str | 88 """A bytes which mostly acts as a Python 2 str |
89 | 89 |
90 >>> bytestr(), bytestr(bytearray(b'foo')), bytestr(u'ascii'), bytestr(1) | 90 >>> bytestr(), bytestr(bytearray(b'foo')), bytestr(u'ascii'), bytestr(1) |
91 (b'', b'foo', b'ascii', b'1') | 91 ('', 'foo', 'ascii', '1') |
92 >>> s = bytestr(b'foo') | 92 >>> s = bytestr(b'foo') |
93 >>> assert s is bytestr(s) | 93 >>> assert s is bytestr(s) |
94 | 94 |
95 __bytes__() should be called if provided: | 95 __bytes__() should be called if provided: |
96 | 96 |
97 >>> class bytesable(object): | 97 >>> class bytesable(object): |
98 ... def __bytes__(self): | 98 ... def __bytes__(self): |
99 ... return b'bytes' | 99 ... return b'bytes' |
100 >>> bytestr(bytesable()) | 100 >>> bytestr(bytesable()) |
101 b'bytes' | 101 'bytes' |
102 | 102 |
103 There's no implicit conversion from non-ascii str as its encoding is | 103 There's no implicit conversion from non-ascii str as its encoding is |
104 unknown: | 104 unknown: |
105 | 105 |
106 >>> bytestr(chr(0x80)) # doctest: +ELLIPSIS | 106 >>> bytestr(chr(0x80)) # doctest: +ELLIPSIS |
151 s = bytechr(s) | 151 s = bytechr(s) |
152 return s | 152 return s |
153 | 153 |
154 def __iter__(self): | 154 def __iter__(self): |
155 return iterbytestr(bytes.__iter__(self)) | 155 return iterbytestr(bytes.__iter__(self)) |
156 | |
157 def __repr__(self): | |
158 return bytes.__repr__(self)[1:] # drop b'' | |
156 | 159 |
157 def iterbytestr(s): | 160 def iterbytestr(s): |
158 """Iterate bytes as if it were a str object of Python 2""" | 161 """Iterate bytes as if it were a str object of Python 2""" |
159 return map(bytechr, s) | 162 return map(bytechr, s) |
160 | 163 |