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

R

rehn

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
 
M

Mark Dickinson

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?

Have you tried this on the recent 2.6 alpha (Python2.6a1)? It's
available
from

http://www.python.org/download/releases/2.6/

I suspect it may already be fixed there. In any case, it would be
useful if you could file a bug report at http://bugs.python.org.
It's not absolutely clear to me that this constitutes a bug
(though I think it does), but it would be worth having people look at
it.

I'm assuming that you're on Windows? I can't reproduce the problem
on OS X.

Mark
 
P

Peter Otten

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?

You could try another protocol. Does
inf

work?

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top