[issue1692335] Fix exception pickling: Move initial args assignment to BaseException.__new__
Jason R. Coombs
report at bugs.python.org
Wed Sep 22 15:39:21 CEST 2010
Jason R. Coombs <jaraco at jaraco.com> added the comment:
In msg108954, I believe belopolsky is mistaken in stating that "it would be easy to simply provide custom __getinitargs__ or __reduce__ to support it". It appears __getinitargs__ does not work on Python 2.5 or Python 2.7. Exceptions of the following class still raise a TypeError on unpickling:
"""Extension with values, init called with no args."""
def __init__(self, foo):
self.foo = foo
Using __reduce__ does seem to work. I suspect this is because Exceptions are extension types.
I think the fundamental problem is that pickling exceptions does not follow the principle of least surprise. In particular:
- Other built-in objects (dicts, lists, etc) don't require special handling (replace Exception with dict in the above example and it works).
- It's not obvious how to write an Exception subclass that takes additional arguments and make it pickleable.
- Neither the pickle documentation nor the Exception documentation describe how pickling is implemented in Exceptions.
Eric has provided some good use cases. Furthermore, it seems counter-intuitive to me to pass a subclass' custom arguments to the parent class. Indeed, even the official tutorial defines exception classes that are unpickleable (http://docs.python.org/tutorial/errors.html#tut-userexceptions).
If the use case is obvious enough that it shows up in the hello world tutorial, I don't think there should be any argument that it's not a common use case.
At the very least, there should be a section in the pickle documentation or Exception documentation describing how one should make a pickleable subclass. What would be better is for Exceptions to behave like most other classes when being pickled.
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list