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