classification
Title: Document recommended exception for objects that shouldn't be pickled
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Michael Crouch, alexandre.vassalotti, docs@python, pitrou, serhiy.storchaka, sschwarzer
Priority: normal Keywords:

Created on 2014-04-23 05:57 by sschwarzer, last changed 2015-10-28 17:10 by serhiy.storchaka.

Messages (3)
msg217054 - (view) Author: Stefan Schwarzer (sschwarzer) Date: 2014-04-23 05:57
I recently was confused whether to raise a `PicklingError` or `TypeError` in `__getstate__` if objects of my class can't and shouldn't be pickled. [1]

Terry Reedy advised I should use `TypeError`. [2]

I wonder if the `pickle` module documention should explicitly recommend using `TypeError` if a class wants to say that its objects can't be pickled. What do you think?

[1] https://mail.python.org/pipermail/python-list/2014-April/670987.html
[2] https://mail.python.org/pipermail/python-list/2014-April/671002.html
msg253606 - (view) Author: Michael Crouch (Michael Crouch) Date: 2015-10-28 16:04
When pickling an object fails on line 70 of copy_reg.py, a "TypeError" is raised. However, according to section 11.1.3 of the Standard Library documentation, when an unpicklable object is passed to the dump() method the "PicklingError" exception will be raised.
msg253608 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-10-28 17:10
PicklingError is raised in following cases:

1. Programmical Pickler error (Pickler.__init__() was not called by subclass' __init__()).

2. Due to protocol or implementation limitations: memoizing more than 2**32 objects, saving non-ASCII globals with protocol < 3, etc. Exception type is implementation depended. Sometimes OverflowError or struct.error can be raised instead.

3. When __reduce__/__reduce_ex__ returns invalid value: neither str or tuple, a tuple has wrong size, first tuple element is not a callable, first argument for __newobj__/__newobj_ex__ has no __new__ or doesn't match a type of pickled object, global lookup is failed or doesn't match pickled object, etc.

4. When extension code is not an integer or out of range.
History
Date User Action Args
2015-10-28 17:10:04serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg253608
2015-10-28 16:04:54Michael Crouchsetversions: + Python 2.7, - Python 3.5
nosy: + Michael Crouch

messages: + msg253606

components: + Library (Lib), - Documentation
2014-04-23 13:24:41berker.peksagsetnosy: + pitrou, alexandre.vassalotti
2014-04-23 05:57:14sschwarzercreate