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

SourceForge.net noreply at sourceforge.net
Sat Apr 1 02:56:19 CEST 2006


Bugs item #1457119, was opened at 2006-03-23 19:29
Message generated for change (Comment added) made by oripel
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
>>>


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

>Comment By: Ori Peleg (oripel)
Date: 2006-04-01 03:56

Message:
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.

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

Comment By: Žiga Seilnacht (zseil)
Date: 2006-04-01 03:44

Message:
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.

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

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