ValueError in pickle module during unpickling a infinite float (python 2.5.2)

rehn at iwm.mw.tu-dresden.de rehn at iwm.mw.tu-dresden.de
Wed Mar 12 16:22:19 CET 2008


Unpickling an infinite float caused a ValueError in the pickle module.
I need to pickle and load infinite floats in my project. Do you have
any suggestions how to solve the issue?

# code describing the issue:

# define float constants with double-precision:

# copied from fpconst module: http://www.analytics.washington.edu/statcomp/projects/rzope/fpconst/
import struct
_big_endian = struct.pack('i',1)[0] != '\x01' # check endianess
if(_big_endian): # and define appropriate constants
    NaN    = struct.unpack('d', '\x7F\xF8\x00\x00\x00\x00\x00\x00')[0]
    PosInf = struct.unpack('d', '\x7F\xF0\x00\x00\x00\x00\x00\x00')[0]
    NegInf = -PosInf
else:
    NaN    = struct.unpack('d', '\x00\x00\x00\x00\x00\x00\xf8\xff')[0]
    PosInf = struct.unpack('d', '\x00\x00\x00\x00\x00\x00\xf0\x7f')[0]
    NegInf = -PosInf


# pickling and unpickling a infinite float:

f = PosInf
p = pickle.dumps(f)
rf = pickle.loads(p)
print f
print rf

# ValueError in python 2.5.2:

Traceback (most recent call last):
  File "....py", line 89, in <module>
    rf = pickle.loads(p)
  File "...\pickle.py", line 1374, in loads
    return Unpickler(file).load()
  File "...\pickle.py", line 858, in load
    dispatch[key](self)
  File "...\pickle.py", line 954, in load_float
    self.append(float(self.readline()[:-1]))
ValueError: invalid literal for float(): 1.#INF



More information about the Python-list mailing list