equal
deleted
inserted
replaced
200 ... def __bytes__(self): |
200 ... def __bytes__(self): |
201 ... return b'bytes' |
201 ... return b'bytes' |
202 >>> bytestr(bytesable()) |
202 >>> bytestr(bytesable()) |
203 'bytes' |
203 'bytes' |
204 |
204 |
|
205 ...unless the argument is the bytes *type* itself: it gets a |
|
206 __bytes__() method in Python 3.11, which cannot be used as in an instance |
|
207 of bytes: |
|
208 |
|
209 >>> bytestr(bytes) |
|
210 "<class 'bytes'>" |
|
211 |
205 There's no implicit conversion from non-ascii str as its encoding is |
212 There's no implicit conversion from non-ascii str as its encoding is |
206 unknown: |
213 unknown: |
207 |
214 |
208 >>> bytestr(chr(0x80)) # doctest: +ELLIPSIS |
215 >>> bytestr(chr(0x80)) # doctest: +ELLIPSIS |
209 Traceback (most recent call last): |
216 Traceback (most recent call last): |
249 pass |
256 pass |
250 |
257 |
251 def __new__(cls: Type[_Tbytestr], s: object = b'') -> _Tbytestr: |
258 def __new__(cls: Type[_Tbytestr], s: object = b'') -> _Tbytestr: |
252 if isinstance(s, bytestr): |
259 if isinstance(s, bytestr): |
253 return s |
260 return s |
254 if not isinstance( |
261 if not isinstance(s, (bytes, bytearray)) and ( |
255 s, (bytes, bytearray) |
262 isinstance(s, type) |
256 ) and not builtins.hasattr( # hasattr-py3-only |
263 or not builtins.hasattr(s, u'__bytes__') # hasattr-py3-only |
257 s, u'__bytes__' |
|
258 ): |
264 ): |
259 s = str(s).encode('ascii') |
265 s = str(s).encode('ascii') |
260 return bytes.__new__(cls, s) |
266 return bytes.__new__(cls, s) |
261 |
267 |
262 # The base class uses `int` return in py3, but the point of this class is to |
268 # The base class uses `int` return in py3, but the point of this class is to |