gh-148669: Clarify __reduce__() module lookup behavior#148670
Conversation
| The :mod:`!pickle` module differs from :mod:`marshal` in several significant ways: | ||
|
|
||
| * :mod:`marshal` cannot be used to serialize user-defined classes and their | ||
| instances. :mod:`!pickle` can save and restore class instances transparently, |
There was a problem hiding this comment.
Why was this removed? It's still accurate.
There was a problem hiding this comment.
I don't think the statement was true? It is perfectly fine to pickle an instance which lives in a separate module than the one storing the class definition right?
There was a problem hiding this comment.
This refers to cases where the class is no longer accessible at its assigned place. For example, if you pickle an instance of some_mod.ClassA, then do del some_mod.ClassA, you can't unpickle the instance any more.
There was a problem hiding this comment.
Seems like I deleted too much, we should just keep however the class definition must be importable. But the instance doesn't have to live in the same module of the class:
# module1.py
class Test: ...# module2.py
import pickle
from module1 import Test
t = Test()
pickle.loads(pickle.dumps(t)) # fineThere was a problem hiding this comment.
I think this is referring to cases where the class definition is moved between the pickling and unpickling code. Not a common issue but that can be relevant if e.g. a new library (or stdlib) version moves a class from one file to another, and you try to pickle under one version and unpickle under another.
There was a problem hiding this comment.
Indeed that makes sense. I think I misread when -> where.
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Documentation build overview
4 files changed± library/difflib.html± library/functions.html± library/pickle.html± library/tkinter.messagebox.html |
|
Thanks @Viicos for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
|
GH-149703 is a backport of this pull request to the 3.15 branch. |
|
GH-149704 is a backport of this pull request to the 3.14 branch. |
|
GH-149705 is a backport of this pull request to the 3.13 branch. |
__reduce__()behavior when a string is returned (singleton support). #148669📚 Documentation preview 📚: https://cpython-previews--148670.org.readthedocs.build/