-
-
Notifications
You must be signed in to change notification settings - Fork 32k
_pickle.UnpicklingError: Memo value not found at index 1 #125756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There's an undocumented
Lines 478 to 486 in b3c6b2c
In your case, you could probably do something like this: try:
pickler.dump([i_will_be_memorized, i_cant_be_pickled])
except TypeError:
# i_will_be_memorized is now in pickler memory despite the exception
pickler.clear_memo() |
Thanks for the quick reply! I thought clear_memo was not documented because it’s dangerous to use. When you clear the memo, I don’t think that 'action' gets written to the output, so the loader won’t clear its own memo (I have't checked any code, this is just my observation). As a result, loading a stream where clear_memo was used could lead to loading the wrong objects. If clear_memo is used publicly, I think it deserves its own issue. import pickle
import io
buf = io.BytesIO()
pickler = pickle.Pickler(buf)
pickler.dump("foo")
pickler.clear_memo()
pickler.dump("bar")
pickler.dump("bar")
buf.seek(0)
loader = pickle.Unpickler(buf)
print(loader.load()) # prints foo
print(loader.load()) # prints bar
print(loader.load()) # prints foo (I wanted bar) |
This will lead to a silent degradation of pickle size and performance, but at least the result can be correctly unpickled. We can also introduce an opcode for clearing the memo in the loader, but this is a PEP-level change. I am not sure that we have enough of good use cases for such feature. cc @pitrou |
We can also combine two solutions -- disable MEMOIZE until the memo size achieve its maximal size before clearing. After that, MEMOIZE can be used. It still has impact. Alternatively, we can deprecate If creating a new In any case this is a difficult-to-use-correctly feature. |
(cherry picked from commit 2542256) Co-authored-by: Tomas R. <[email protected]>
(cherry picked from commit 2542256) Co-authored-by: Tomas R. <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
When pickling a container object. The pickler class will store child-objects to its self.memo even if the container could not be fully pickled (and not written to the output).
This can cause references to memo objects that are not in the output stream.
CPython versions tested on:
3.12
Operating systems tested on:
Windows
Linked PRs
Pickler.clear_memo
#125762The text was updated successfully, but these errors were encountered: