| 335 | |
| 336 | |
| 337 | class ZeroCopyBytearray(bytearray): |
| 338 | readonly = False |
| 339 | c_contiguous = True |
| 340 | f_contiguous = True |
| 341 | zero_copy_reconstruct = True |
| 342 | |
| 343 | def __reduce_ex__(self, protocol): |
| 344 | if protocol >= 5: |
| 345 | import pickle |
| 346 | return type(self)._reconstruct, (pickle.PickleBuffer(self),), None |
| 347 | else: |
| 348 | return type(self)._reconstruct, (bytes(self),) |
| 349 | |
| 350 | def __repr__(self): |
| 351 | return "{}({!r})".format(self.__class__.__name__, bytes(self)) |
| 352 | |
| 353 | __str__ = __repr__ |
| 354 | |
| 355 | @classmethod |
| 356 | def _reconstruct(cls, obj): |
| 357 | with memoryview(obj) as m: |
| 358 | obj = m.obj |
| 359 | if type(obj) is cls: |
| 360 | # Zero-copy |
| 361 | return obj |
| 362 | else: |
| 363 | return cls(obj) |
| 364 | |
| 365 | |
| 366 | # For test_nested_names |
no outgoing calls
searching dependent graphs…