[New-bugs-announce] [issue29187] Pickle failure is raising AttributeError and not PicklingError

Matt Dodge report at bugs.python.org
Fri Jan 6 18:06:54 EST 2017


New submission from Matt Dodge:

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.

----------
components: Library (Lib)
messages: 284869
nosy: Matt.Dodge
priority: normal
severity: normal
status: open
title: Pickle failure is raising AttributeError and not PicklingError
versions: Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29187>
_______________________________________


More information about the New-bugs-announce mailing list