[Python-Dev] Re: marshal / unmarshal

Fredrik Lundh fredrik at pythonware.com
Sat Apr 9 07:32:37 CEST 2005


Tim Peters wrote:

> All Python behavior in the presence of a NaN, infinity, or signed zero
> is a platform-dependent accident.  This is because C89 has no such
> concepts, and Python is written to the C89 standard.  It's not easy to
> fix across all platforms (because there is no portable way to do so in
> standard C), although it may be reasonably easy to fix if all anyone
> cares about is gcc and MSVC

which probably represents very close to 100% of all python interpreter
instances out there.  making floats behave the same on standard builds
for windows, mac os x, and linux would be a great step forward.

+1.0 from me.

>> Should loads raise an exception?
>
> Never for a quiet NaN, unless the platform doesn't support NaNs.  It's
> harder to know what to with a signaling NaN, because Python doesn't
> have any of 754's trap-enable or exception status flags either (the
> new ``decimal`` module does, but none of that is integrated with the
> _rest_ of Python yet).
>
> Should note that what the fp literal 1e10000 does across boxes is also
> an accident -- Python defers to the platform C libraries for
> string<->float conversions.

yeah, but the problem here is that MSVC cannot read its own NaN:s;
float() checks for that, but loads doesn't.  compare and contrast:

    >>> float(str(1e10000))
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: invalid literal for float(): 1.#INF

    >>> import marshal
    >>> marshal.loads(marshal.dumps(1e10000))
    1.0

on the other hand,

    >>> marshal.loads("\f\x01x")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: bad marshal data

adding basic error checking shouldn't be very hard (you could probably
call the string->float converter in the float object module, and just map any
exceptions to "bad marshal data")

</F> 





More information about the Python-Dev mailing list