问题描述
我终于弄清楚为什么我的某个应用程序有时会在
Win32下失败,因为它在Linux下总能正常工作:在Win32下,
pickle模块只适用于浮点子集
值。特别是如果你试图转储/加载无穷大
或nan值,加载操作扼流圈:
在Linux下:
$ python
Python 2.3.4(#2,2005年2月9日,14:22:48)
[GCC 3.4.1(Mandrakelinux 10.1 3.4.1-) Linux2上的4mdk)]
I finally figured out why one of my apps sometimes fails under
Win32 when it always works fine under Linux: Under Win32, the
pickle module only works with a subset of floating point
values. In particular the if you try to dump/load an infinity
or nan value, the load operation chokes:
Under Linux:
$ python
Python 2.3.4 (#2, Feb 9 2005, 14:22:48)
[GCC 3.4.1 (Mandrakelinux 10.1 3.4.1-4mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
$ python pickletest.py
Traceback(最近一次调用最后一次):
文件" pickletest.py",第8行,在?
d = pickle.loads(s)
文件C:\ PYTHON23 \ lib \ pickle.py",第1394行,载入
返回Unpickler(文件).load()
文件C:\ PYTHON23 \lib \ pickle.py",第872行,正在加载
dispatch [key](self)
文件" C:\ PYTHON23 \lib \ pickle.py" ,第968行,在load_float中
self.append(float(self.readline()[: - 1]))
ValueError:float()的文字无效:1 。#INF
我意识到这可能是由于Win32 libc implamentation的底层破坏,但应该是pickle模块
隐藏用户的平台依赖性吗?
最好的情况是,如果pickle能够以便携的方式处理所有花车
,那就太好了。
最糟糕的情况是,不应该在pickle模块文件中提到
酸洗浮动非便携式或仅部分建议吗?
在更直接的说明中,是否有pickle钩子允许用户处理pickle无法处理的类型?
?或者,我是否需要抛出泡菜并从头开始写东西?
[NaN和Infinity完全有效(并且非常有用)
浮点值,并且不使用它们将需要巨大的
复杂性增加我的应用程序(不使用它们可能
至少三倍的代码量在某些情况下需要)。
-
格兰特爱德华兹格兰特哇!哇!
visi.com
$ python pickletest.py
Traceback (most recent call last):
File "pickletest.py", line 8, in ?
d = pickle.loads(s)
File "C:\PYTHON23\lib\pickle.py", line 1394, in loads
return Unpickler(file).load()
File "C:\PYTHON23\lib\pickle.py", line 872, in load
dispatch[key](self)
File "C:\PYTHON23\lib\pickle.py", line 968, in load_float
self.append(float(self.readline()[:-1]))
ValueError: invalid literal for float(): 1.#INF
I realize that this is probably due to underlying brokenness in
the Win32 libc implimentation, but should the pickle module
hide such platform-dependancies from the user?
Best case, it would be nice if pickle could handle all floats
in a portable way.
Worst case, shouldn''t the pickle module documentation mention
that pickling floats non-portable or only partially implimented?
On a more immediate note, are there hooks in pickle to allow
the user to handle types that pickle can''t deal with? Or, do I
have to throw out pickle and write something from scratch?
[NaN and Infinity are prefectly valid (and extremely useful)
floating point values, and not using them would require huge
complexity increases in my apps (not using them would probably
at least triple the amount of code required in some cases).]
--
Grant Edwards grante Yow! Yow!
at
visi.com
推荐答案
这篇关于pickle broken:在win32下无法处理NaN或Infinity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!