[issue1581183] pickle protocol 2 failure on int subclass

Alexander Belopolsky report at bugs.python.org
Tue Jun 29 19:10:56 CEST 2010


Alexander Belopolsky <belopolsky at users.sourceforge.net> added the comment:

Upon further investigation, I conclude that the problem is in the user code.  I am attaching int_subclass_pickle_problem_fixed.py which fixes the user code as follows:

     def __getnewargs__(self):
-        return (int(self), self.an_enum)
+        return (int(self), None)

Note that with this change, the object is pickled correctly because __setstate__ takes care of resetting self.an_enum.

The problem is that self-referential state should not be passed via __getnewargs__ mechanism.  This is because when pickler starts writing newargs, it is already committed to creating a new object (otherwise it would not need to serialize newargs in the first place.)  If the newargs contain the object that is being pickled, it will be serialized twice unless this situation is caught in memoize.

What can be improved, is the diagnostic and possibly documentation.  If after saving newargs, memo contains the object that is being pickled, an exception should be raised explaining that __getnewargs__() should not contain self-references.

----------
keywords:  -patch
priority: normal -> low

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1581183>
_______________________________________


More information about the Python-bugs-list mailing list