From: Alexander Eisenhuth on
Hello together,

python: 2.5.1
palttform: winXP

I'm using pickle.dump and pickle.load with data that is created in a wrapped
(boost.python) piece of C++ code. pickle.dump works fine. pickle.load creates
the following exception:

[...]
data = pickle.load(input)
File "C:\Python25\lib\pickle.py", line 1370, in load
return Unpickler(file).load()
File "C:\Python25\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python25\lib\pickle.py", line 954, in load_float
self.append(float(self.readline()[:-1]))
ValueError: invalid literal for float(): -1.#IND

- I'm not sure what -1.#IND means. Can somebody assist?

- As pickle write the data I'm a bit confused, that is can't be unpickled it. Is
that a bug or a feature?

BTW: I'm tied to version 2.5 of python

Thank and regards
Alexander


From: Grant Edwards on
On 2010-07-12, Alexander Eisenhuth <newsuser(a)stacom-software.de> wrote:

> python: 2.5.1
> palttform: winXP
>
> I'm using pickle.dump and pickle.load with data that is created in a
> wrapped (boost.python) piece of C++ code. pickle.dump works fine.
> pickle.load creates the following exception:
>
> [...]
> data = pickle.load(input)
> File "C:\Python25\lib\pickle.py", line 1370, in load
> return Unpickler(file).load()
> File "C:\Python25\lib\pickle.py", line 858, in load
> dispatch[key](self)
> File "C:\Python25\lib\pickle.py", line 954, in load_float
> self.append(float(self.readline()[:-1]))
> ValueError: invalid literal for float(): -1.#IND
>
> - I'm not sure what -1.#IND means.

That's an infinity.

> Can somebody assist?

In Python 2.x, pickle doesn't handle infinities and NaNs.

> - As pickle write the data I'm a bit confused, that is can't be
> unpickled it. Is that a bug or a feature?

IMO, it's a bug. It's been fixed in 3.x.

> BTW: I'm tied to version 2.5 of python

If you want to pickle floating point data that includes infinities and
NaNs, you need to write your own handler for floating point values.
When pickling you need to check for infinities and NaNs and output
some defined strings. When unpickling, you need to check for those
strings and generate infinities and NaNs as appropriate.

The CPython 2.x version of pickle just uses the underlying C library
routines to print and parse floating point values. If those libraries
are broken in the way that they represent infinity and NaN, then
pickle is broken in the same manner. Windows standard libraries are
broken. When formatting floating point values, it produces strings
that it can't parse as input.

IIRC, Linux works better -- but if you want something that you know is
going to work (and work cross-platform), then you need to write your
own floating point pickle/unpickle methods, and hook them into the
pickle module.

--
Grant Edwards grant.b.edwards Yow! Uh-oh!! I'm having
at TOO MUCH FUN!!
gmail.com
From: Grant Edwards on
On 2010-07-12, Grant Edwards <invalid(a)invalid.invalid> wrote:
> On 2010-07-12, Alexander Eisenhuth <newsuser(a)stacom-software.de> wrote:
>
>> python: 2.5.1
>> palttform: winXP
>>
>> I'm using pickle.dump and pickle.load with data that is created in a
>> wrapped (boost.python) piece of C++ code. pickle.dump works fine.
>> pickle.load creates the following exception:
>>
>> [...]
>> data = pickle.load(input)
>> File "C:\Python25\lib\pickle.py", line 1370, in load
>> return Unpickler(file).load()
>> File "C:\Python25\lib\pickle.py", line 858, in load
>> dispatch[key](self)
>> File "C:\Python25\lib\pickle.py", line 954, in load_float
>> self.append(float(self.readline()[:-1]))
>> ValueError: invalid literal for float(): -1.#IND
>>
>> - I'm not sure what -1.#IND means.
>
> That's an infinity.

Oops, I just noticed that I misread that. 1.#INF is an infinity,
1.#IND is an "indefinite", or what everybody outside of Microsoft
calls a NaN (Not-a-Number).

--
Grant Edwards grant.b.edwards Yow! ONE LIFE TO LIVE for
at ALL MY CHILDREN in ANOTHER
gmail.com WORLD all THE DAYS OF
OUR LIVES.
From: Mark Dickinson on
Alexander Eisenhuth <newsuser <at> stacom-software.de> writes:

> File "C:\Python25\lib\pickle.py", line 954, in load_float
> self.append(float(self.readline()[:-1]))
> ValueError: invalid literal for float(): -1.#IND
>
> - I'm not sure what -1.#IND means. Can somebody assist?

It's the Windows way of representing a NaN (not a number).
A NaN is typically what you get when you try to perform an
invalid floating-point operation, like taking the square root
of a negative number, or dividing 0.0 by 0.0. Some people
also use NaNs to represent uninitialized values, or as
placeholders for missing data.

> - As pickle write the data I'm a bit confused, that is can't
> be unpickled it. Is that a bug or a feature?

Well, it's certainly not ideal. It shouldn't be a problem in
Python 2.6 or 2.7, though; unfortunately, Python 2.5 is no
longer receiving bugfixes, so it's not going to change there.

> BTW: I'm tied to version 2.5 of python

Have you tried using pickle protocol 1 or 2, instead of pickle
protocol 0? That may well solve your problem. (Those
protocols write out the binary form of a float directly, instead
of reading and writing a string representation.)

--
Mark


From: Alexander Eisenhuth on
Mark Dickinson schrieb:

>
>> BTW: I'm tied to version 2.5 of python
>
> Have you tried using pickle protocol 1 or 2, instead of pickle
> protocol 0? That may well solve your problem. (Those
> protocols write out the binary form of a float directly, instead
> of reading and writing a string representation.)
>

Thanks a lot, it looks like it solves the problem using another version of the
protocol

Regards