indygreg / python-zstandard

Python bindings to the Zstandard (zstd) compression library
BSD 3-Clause "New" or "Revised" License
499 stars 86 forks source link

zstandard.MAGIC_NUMBER returns wrong value on Windows #202

Open 123456abcdef opened 1 year ago

123456abcdef commented 1 year ago

I encountered the following problem.

Linux environment

Python 3.11.3 zstandard 0.21.0

>>> import zstandard
>>> hex(zstandard.MAGIC_NUMBER)
'0xfd2fb528'

Win10 environment

Python 3.9.16 zstandard 0.21.0

>>> import zstandard
>>> hex(zstandard.MAGIC_NUMBER)
'-0x2d04ad8'

It seems to be a OS specific difference not related to the Python version. Why is there a difference?

enkore commented 6 months ago

PyModule_AddIntConstant takes a long, linux is unix is LP64, so long is 64-bits wide, windows is LLP64, so long is 32-bits wide, so that highest set bit makes it negative on windows.

Fix would be using PyLong_FromLongLong and PyModule_AddObjectRef.