This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jaraco
Recipients alexandre.vassalotti, belopolsky, brett.cannon, ehuss, facundobatista, georg.brandl, gvanrossum, jafo, jaraco, jarpa, kylev, loewis, nnorwitz, tseaver, zseil
Date 2010-09-22.13:39:04
SpamBayes Score 2.9876102e-13
Marked as misclassified No
Message-id <1285162761.99.0.137747450892.issue1692335@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2010-09-22 13:39:22jaracosetrecipients: + jaraco, gvanrossum, loewis, nnorwitz, brett.cannon, georg.brandl, facundobatista, jafo, ehuss, tseaver, belopolsky, zseil, alexandre.vassalotti, jarpa, kylev
2010-09-22 13:39:21jaracosetmessageid: <1285162761.99.0.137747450892.issue1692335@psf.upfronthosting.co.za>
2010-09-22 13:39:06jaracolinkissue1692335 messages
2010-09-22 13:39:04jaracocreate