Jason R. Coombs <[email protected]> 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:
class D(Exception):
"""Extension with values, init called with no args."""
def __init__(self, foo):
self.foo = foo
Exception.__init__(self)
def __getinitargs__(self):
return self.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.
----------
nosy: +jaraco
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue1692335>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com