| 306 | |
| 307 | |
| 308 | class ZeroCopyBytes(bytes): |
| 309 | readonly = True |
| 310 | c_contiguous = True |
| 311 | f_contiguous = True |
| 312 | zero_copy_reconstruct = True |
| 313 | |
| 314 | def __reduce_ex__(self, protocol): |
| 315 | if protocol >= 5: |
| 316 | import pickle |
| 317 | return type(self)._reconstruct, (pickle.PickleBuffer(self),), None |
| 318 | else: |
| 319 | return type(self)._reconstruct, (bytes(self),) |
| 320 | |
| 321 | def __repr__(self): |
| 322 | return "{}({!r})".format(self.__class__.__name__, bytes(self)) |
| 323 | |
| 324 | __str__ = __repr__ |
| 325 | |
| 326 | @classmethod |
| 327 | def _reconstruct(cls, obj): |
| 328 | with memoryview(obj) as m: |
| 329 | obj = m.obj |
| 330 | if type(obj) is cls: |
| 331 | # Zero-copy |
| 332 | return obj |
| 333 | else: |
| 334 | return cls(obj) |
| 335 | |
| 336 | |
| 337 | class ZeroCopyBytearray(bytearray): |
no outgoing calls
searching dependent graphs…