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
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
pickle.dumps(func()())
_pickle.PicklingError: Can't pickle .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
pickle.dumps(func()())
AttributeError: Can't pickle local object 'func..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
<http://bugs.python.org/issue29187>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com