[ python-Bugs-1457119 ] Unifying pickle and cPickle exception class hierarchies

SourceForge.net noreply at sourceforge.net
Thu Mar 23 18:29:52 CET 2006


Bugs item #1457119, was opened at 2006-03-23 19:29
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1457119&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Ori Peleg (oripel)
Assigned to: Nobody/Anonymous (nobody)
Summary: Unifying pickle and cPickle exception class hierarchies

Initial Comment:
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
>>>


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1457119&group_id=5470


More information about the Python-bugs-list mailing list