Share

FixedPoint

Tracker: Bugs

5 FixedPoint objects cannot be pickled - ID: 776566
Last Update: Comment added ( yacc143 )

Python 2.2.1 (#1, Sep 9 2002, 09:26:21)
[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux-i386
Type "help", "copyright", "credits" or
"license" for
more information.
>>> from fixedpoint import FixedPoint
>>> x = FixedPoint("1.234")
>>> print x
1.23
>>> import pickle
>>> f = open("foo", 'w')
>>> pickle.dump(f, x)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/pickle.py", line 973, in dump
Pickler(file, bin).dump(object)
File "/usr/lib/python2.2/pickle.py", line 110, in
__init__
self.write = file.write
AttributeError: 'FixedPoint' object has no attribute
'write'
>>> import cPickle
>>> cPickle.dump(f, x)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: argument must have 'write' attribute
>>>


Duane Voth ( duanev ) - 2003-07-23 22:22

5

Open

None

Nobody/Anonymous

None

None

Public


Comments ( 2 )




Date: 2004-05-28 14:35
Sender: yacc143

Logged In: YES
user_id=918868

To make pickle work, just add the following two methods
to the FixedPoint class:

def __getstate__(self):
return (self.n, self.p)

def __setstate__(self, v):
self.n, self.p = v




Date: 2003-07-24 18:56
Sender: duanev

Logged In: YES
user_id=87541

Augh! Never mind that above example - I got the args wrong
for dump(). Here is the correct test and error message:

Python 2.2.1 (#1, Sep 9 2002, 09:26:21)
[GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux-i386
Type "help", "copyright", "credits" or
"license" for more
information.
>>> from fixedpoint import FixedPoint
>>> import cPickle
>>> f = open('foo', 'w')
>>> x = FixedPoint(1.234)
>>> print x
1.23
>>> cPickle.dump(x, f)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/copy_reg.py", line 68, in _reduce
dict = getstate()
TypeError: a class that defines __slots__ without defining
__getstate__ cannot be pickled
>>>



Log in to comment.

Attached File

No Files Currently Attached

Change

No changes have been made to this artifact.