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

rehn at rehn at
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:
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
    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 "", line 89, in <module>
    rf = pickle.loads(p)
  File "...\", line 1374, in loads
    return Unpickler(file).load()
  File "...\", line 858, in load
  File "...\", line 954, in load_float
ValueError: invalid literal for float(): 1.#INF

More information about the Python-list mailing list