Message117139
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. |
|
Date |
User |
Action |
Args |
2010-09-22 13:39:22 | jaraco | set | recipients:
+ jaraco, gvanrossum, loewis, nnorwitz, brett.cannon, georg.brandl, facundobatista, jafo, ehuss, tseaver, belopolsky, zseil, alexandre.vassalotti, jarpa, kylev |
2010-09-22 13:39:21 | jaraco | set | messageid: <1285162761.99.0.137747450892.issue1692335@psf.upfronthosting.co.za> |
2010-09-22 13:39:06 | jaraco | link | issue1692335 messages |
2010-09-22 13:39:04 | jaraco | create | |
|