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 Matt.Dodge
Recipients Matt.Dodge
Date 2017-01-06.23:06:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1483744014.75.0.396569327718.issue29187@psf.upfronthosting.co.za>
In-reply-to
Content
When failing to pickle something (like a locally scoped class) the documentation indicates that a PicklingError should be raised.
Doc links:
 - https://docs.python.org/3/library/pickle.html?highlight=pickle#pickle-picklable
 - https://docs.python.org/3.5/library/pickle.html#what-can-be-pickled-and-unpickled

However, instead I'm seeing AttributeError get raised instead, starting in Python 3.5. In Python 3.4 PicklingErrror was raised, as expected.

To reproduce, use the following file <pickletest.py>
    def func():
        class C: pass
        return C
    import pickle
    pickle.dumps(func()())

In Python 3.4 you see:
Traceback (most recent call last):
  File "pickletest.py", line 5, in <module>
    pickle.dumps(func()())
_pickle.PicklingError: Can't pickle <class '__main__.func.<locals>.C'>: attribute lookup C on __main__ failed

But in 3.5/3.6 you see:
Traceback (most recent call last):
  File "pickletest.py", line 5, in <module>
    pickle.dumps(func()())
AttributeError: Can't pickle local object 'func.<locals>.C'

I don't necessarily mind that a different exception is being raised, but how should we be handling exceptions while pickling? Catch all exceptions? That doesn't feel right to me, but if we're trying to pickle data out of our control I'm not sure what else to do.

FYI, the UnpicklingError documentation (https://docs.python.org/3/library/pickle.html?highlight=pickle#pickle.UnpicklingError) indicates that other exceptions can possibly be raised during unpickling. I assume that is more related to the fact that the pickled data may not fit into the current class/module structure though, so I think it's unrelated.
History
Date User Action Args
2017-01-06 23:06:54Matt.Dodgesetrecipients: + Matt.Dodge
2017-01-06 23:06:54Matt.Dodgesetmessageid: <1483744014.75.0.396569327718.issue29187@psf.upfronthosting.co.za>
2017-01-06 23:06:54Matt.Dodgelinkissue29187 messages
2017-01-06 23:06:54Matt.Dodgecreate