[Python-bugs-list] [ python-Bugs-582297 ] pickle error message unhelpful

noreply@sourceforge.net noreply@sourceforge.net
Tue, 16 Jul 2002 09:50:39 -0700


Bugs item #582297, was opened at 2002-07-16 15:22
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=582297&group_id=5470

Category: Python Library
Group: Python 2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Dave Aitel (wabe0x90)
Assigned to: Nobody/Anonymous (nobody)
Summary: pickle error message unhelpful

Initial Comment:
Version: Python 2.2 (#1, Feb 24 2002, 16:21:58) 
I was trying to pickle a class which had an open
socket, the exception  I got was:
File "/usr/lib/python2.2/pickle.py", line 969, in dump
    Pickler(file, bin).dump(object)
  File "/usr/lib/python2.2/pickle.py", line 115, in dump
    self.save(object)
  File "/usr/lib/python2.2/pickle.py", line 221, in save
    f(self, object)
  File "/usr/lib/python2.2/pickle.py", line 494, in
save_inst
    save(stuff)
  File "/usr/lib/python2.2/pickle.py", line 221, in save
    f(self, object)
  File "/usr/lib/python2.2/pickle.py", line 443, in
save_dict
    save(value)
  File "/usr/lib/python2.2/pickle.py", line 221, in save
    f(self, object)
  File "/usr/lib/python2.2/pickle.py", line 494, in
save_inst
    save(stuff)
  File "/usr/lib/python2.2/pickle.py", line 221, in save
    f(self, object)
  File "/usr/lib/python2.2/pickle.py", line 443, in
save_dict
    save(value)
  File "/usr/lib/python2.2/pickle.py", line 185, in save
    tup = reduce()
  File "/usr/lib/python2.2/copy_reg.py", line 57, in
_reduce
    state = base(self)
TypeError: an integer is required

Only by adding this to copy_reg.py was I able to figure
out what was really going on:
def _reduce(self):
    for base in self.__class__.__mro__:
        if hasattr(base, '__flags__') and not
base.__flags__ & _HEAPTYPE:
            break
    else:
        base = object # not really reachable
    if base is object:
        state = None
    else:
################I added the next line
        print "dave: type of self="+str(self)
        state = base(self)
    args = (self.__class__, base, state)


It would be REALLY nice if pickle returned some sort of
better error message so I didn't have to do that,
especially for beginers like myself. :>



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

>Comment By: Jeremy Hylton (jhylton)
Date: 2002-07-16 16:50

Message:
Logged In: YES 
user_id=31392

Can you provide some sample code that cause the obscure
error message?  When I tried to reproduce this problem, I
create an instance with a socket attribute.  When I pickled
it, I get a clear error message:

>>> import socket
>>> class Foo(object):
...     def __init__(self):
...             self.sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
... 
>>> f = Foo()
>>> pickle.dumps(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.3/pickle.py", line 1055, in dumps
    Pickler(file, bin).dump(object)
  File "/usr/local/lib/python2.3/pickle.py", line 165, in dump
    self.save(object)
  File "/usr/local/lib/python2.3/pickle.py", line 275, in save
    f(self, object)
  File "/usr/local/lib/python2.3/pickle.py", line 555, in
save_inst
    save(stuff)
  File "/usr/local/lib/python2.3/pickle.py", line 275, in save
    f(self, object)
  File "/usr/local/lib/python2.3/pickle.py", line 504, in
save_dict
    save(value)
  File "/usr/local/lib/python2.3/pickle.py", line 239, in save
    tup = reduce()
  File "/usr/local/lib/python2.3/copy_reg.py", line 57, in
_reduce
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle socket objects

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

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