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.

classification
Title: Unifying pickle and cPickle exception class hierarchies
Type: enhancement Stage: test needed
Components: Extension Modules, Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, georg.brandl, oripel, zseil
Priority: normal Keywords:

Created on 2006-03-23 17:29 by oripel, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg60891 - (view) Author: Ori Peleg (oripel) Date: 2006-03-23 17:29
Should the pickle and cPickle exception class
hierarchies be unified? Perhaps just subclass one
grandparent PickleError class? That way module copy_reg
can throw exceptions from this hierarchy.

Here's the experience that led to the thought:

(1) confusing exception types when a class can't be pickled

When an object can't be pickled, sometimes a TypeError
is raised, and sometimes an exception derived from
pickle.PickleError or cPickle.PickleError, a screenshot
is pasted below.

(2) copy_reg raises TypeError
When a pickle-related exception occurs in copy_reg, a
TypeError is raised, e.g. in line 69:

raise TypeError, "can't pickle %s objects" % base.__name__

but if copy_reg wants to raise an exception from the
pickle module's hierarchy...

(3) copy_reg doesn't know if pickle or cPickle are used

It can't choose between the two, therefore it chooses
TypeError.

=== screenshot ===
>>> import sys, pickle, cPickle
>>> try: raise RuntimeError
... except: tb = sys.exc_info()[-1]
...
>>> frame = tb.tb_frame
>>> pickle.dumps(frame)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/pickle.py", line 1386, in dumps
    Pickler(file, protocol, bin).dump(obj)
  File "/usr/lib/python2.4/pickle.py", line 231, in dump
    self.save(obj)
  File "/usr/lib/python2.4/pickle.py", line 313, in save
    rv = reduce(self.proto)
  File "/usr/lib/python2.4/copy_reg.py", line 69, in
_reduce_ex
    raise TypeError, "can't pickle %s objects" %
base.__name__
TypeError: can't pickle frame objects
>>> cPickle.dumps(frame)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/copy_reg.py", line 69, in
_reduce_ex
    raise TypeError, "can't pickle %s objects" %
base.__name__
TypeError: can't pickle frame objects
>>> pickle.dumps(tb)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/pickle.py", line 1386, in dumps
    Pickler(file, protocol, bin).dump(obj)
  File "/usr/lib/python2.4/pickle.py", line 231, in dump
    self.save(obj)
  File "/usr/lib/python2.4/pickle.py", line 319, in save
    raise PicklingError("Can't pickle %r object: %r" %
pickle.PicklingError: Can't pickle 'traceback' object:
<traceback object at 0xb7d1d324>
>>> cPickle.dumps(tb)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
cPickle.UnpickleableError: Cannot pickle <type
'traceback'> objects
>>>
msg60892 - (view) Author: Ziga Seilnacht (zseil) * (Python committer) Date: 2006-04-01 00:44
Logged In: YES 
user_id=1326842

I don't think this is possible, since other modules than
pickle and cPickle also rely on copy_reg module, e.g.
the copy module. It would be even more surprising
if a PicklingError would be raised when you try to
copy an object.
I think that this bug can be closed, and the submitter
can open a feature request if he still think that it is
worth the effort.
msg60893 - (view) Author: Ori Peleg (oripel) Date: 2006-04-01 00:56
Logged In: YES 
user_id=1131251

In that case, how about making the pickle and cPickle
exceptions subclass TypeError?

Anyone using copy_reg and expecting a TypeError will be
appeased.
msg83905 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-03-21 01:35
Almost fooled me, still as described on trunk:

>>>help(cPickle.UnpickleableError)
Help on class UnpickleableError in module cPickle:
class UnpickleableError(PicklingError)
[...]
>>> help(pickle.PicklingError)
Help on class PicklingError in module pickle:
class PicklingError(PickleError)

>>> try: cPickle.dumps(tb)
... except pickle.PicklingError: print 1
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cPickle.UnpickleableError: Cannot pickle <type 'traceback'> objects
msg85493 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-05 13:47
This is fine in Py3k, where there is only one set of pickling exceptions
that can be raised.  In 2.x it's best not to break backwards
compatibility right now.
History
Date User Action Args
2022-04-11 14:56:16adminsetgithub: 43076
2009-04-05 13:47:27georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg85493

resolution: wont fix
2009-03-21 01:35:44ajaksu2settype: enhancement
components: + Library (Lib)
versions: + Python 2.7
nosy: + ajaksu2

messages: + msg83905
stage: test needed
2006-03-23 17:29:52oripelcreate