[Python-Dev] PyMarshal_WriteLongToFile: writes 'long' or 32-bit integer?

Greg Stein gstein@lyra.org
Tue, 6 Jun 2000 02:58:44 -0700 (PDT)


On Tue, 6 Jun 2000, Peter Funk wrote:
> [Trent Mick]:
> [...]
> > - PyMarshal_WriteObjectToFile, when writing a PyInt and hence a C long, will
> >   *NOT* truncate the 64-bit integer but will write the whole thing
> > 
> > - On reading a .pyc the PyMarshal_ReadObjectFromFile will complain on stderr
> >   when a 64-bit integral value is read and the current platform is 32-bit.
> [...]
> > The question is: Do we want PyMarshal_WriteLongToFile *AND*
> > PyMarshal_WriteObjectToFile to raise an exception when a long value overflows
> > 32-bits? Up til now I was only proposing the former.
> 
> Yes.  This should at least be the default behaviour of Python.
> 
> The current behaviour would otherwise cause great damage in the
> following scenario, where an innocent software developer working on
> a 64-Bit system trusts on the cross platform portability of Pythons
> bytecode files and unintentional uses a huge integer literal.
> 
> If some people really don't care about portability, there could be 
> switch/option/pragma/whatever to disable this behaviour.
> 
> IMO the portability of Python bytecode files is a *very* valuable property,
> which we shouldn't give up on.

It isn't only bytecodes. As I mentioned before, marshal byte streams are
specified as portable. Therefore, they are used all the time for passing
over the network, for persistence mechanisms, etc.

Having a marshal stream that is *not* cross-platform is just plain busted.

So... you can silently truncate to 32-bits or you can raise an exception.
You cannot, however, marshal a 64-bit value when the definition of an
"marshal integer" is only 32.

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/