Skip to content

Python pickler unable to pickle object the native pickler can #120380

Closed

Description

@apmorton

Bug report

Bug description:

import io
import pickle


class ZeroCopyByteArray(bytearray):
    def __reduce_ex__(self, protocol):
        return type(self), (pickle.PickleBuffer(self),), None


data = [
    ZeroCopyByteArray(),
    ZeroCopyByteArray(),
]

# this works
f = io.BytesIO()
pickler = pickle.Pickler(f, protocol=5)
pickler.dump(data)

# this hits memo assertion
f = io.BytesIO()
pickler = pickle._Pickler(f, protocol=5)
pickler.dump(data)
Traceback (most recent call last):
  File "bug.py", line 24, in <module>
    pickler.dump(data)
  File "/pickle.py", line 487, in dump
    self.save(obj)
  File "/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
    ^^^^^^^^^^^^
  File "/pickle.py", line 932, in save_list
    self._batch_appends(obj)
  File "/pickle.py", line 956, in _batch_appends
    save(x)
  File "/pickle.py", line 603, in save
    self.save_reduce(obj=obj, *rv)
  File "/pickle.py", line 692, in save_reduce
    save(args)
  File "/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
    ^^^^^^^^^^^^
  File "/pickle.py", line 887, in save_tuple
    save(element)
  File "/pickle.py", line 560, in save
    f(self, obj)  # Call unbound method with explicit self
    ^^^^^^^^^^^^
  File "/pickle.py", line 842, in save_picklebuffer
    self.save_bytearray(m.tobytes())
  File "/pickle.py", line 821, in save_bytearray
    self.memoize(obj)
  File "/pickle.py", line 508, in memoize
    assert id(obj) not in self.memo
           ^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jun 12, 2024
  2. picnixz commented on Jun 12, 2024

    @picnixz
  3. aisk commented on Jun 12, 2024

    @aisk
  4. picnixz commented on Jun 12, 2024

    @picnixz
  5. picnixz commented on Jun 12, 2024

    @picnixz
    Member

    The ZeroCopyBytearray buffer appears to need the following implementation (it's actually in Lib/test/pickletester) but I confirmed that the issue remains but only if the buffer is empty (otherwise it's fine). The implementation is roughly

    class ZeroCopyBytearray(bytearray):
        def __reduce_ex__(self, protocol):
            if protocol >= 5:
                return type(self)._reconstruct, (pickle.PickleBuffer(self),), None
            else:
                return type(self)._reconstruct, (bytes(self),)
    
        @classmethod
        def _reconstruct(cls, obj):
            with memoryview(obj) as m:
                obj = m.obj
                if type(obj) is cls:
                    # Zero-copy
                    return obj
                else:
                    return cls(obj)

    So, the __reduce_ex__ method makes more things than what you provided in your example, but it does not fix the issue at all.

    EDIT: I'm investigating the issue in picnixz@a8c1464

  6. apmorton commented on Jun 12, 2024

    @apmorton
    Author

    Correct, I minimized the example to only the relevant parts for reproduction.

    The issue only happens if you use protocol 5 and end up with two objects having different IDs produce a PickleBuffer with the same underlying buffer ID.

  7. picnixz commented on Jun 12, 2024

    @picnixz
    Member

    Yes, the issue is that this specific test case is not covered (I've added one and I'll try to fix it), which is why we did not see the issue (by specific case, I mean the two different instances that are empty, not that it still works if I use the same non-empty buffer values).

  8. added a commit that references this issue on Jun 21, 2024
  9. added 2 commits that reference this issue on Jun 21, 2024
  10. added 2 commits that reference this issue on Jun 26, 2024
  11. encukou commented on Jun 27, 2024

    @encukou
    Member

    Thank you @apmorton for the report, and @picnixz for the fix!

  12. added a commit that references this issue on Jun 30, 2024
  13. added a commit that references this issue on Jul 11, 2024
  14. added a commit that references this issue on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions