jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.01k stars 59 forks source link

msgspec.json.decode parsing a float value #695

Closed Oakington closed 1 month ago

Oakington commented 1 month ago

Description

msgspec==0.18.6 python 3.8.10 Very odd behavior. Parsing of a stringified JSON with a float value results in the following

` import json import msgspec x = '{"a": 3322.1755149730398}' msgspec.json.decode(x)

{'a': 9.871031767461413e-178}

json.loads(x)

{'a': 3322.1755149730398} `

jcrist commented 1 month ago

Thanks for opening this. I'm unable to reproduce the issue, I suspect this is platform specific. Here's what I see on an x86 linux machine:

In [1]: import msgspec

In [2]: msg = '{"a": 3322.1755149730398}'

In [3]: msgspec.json.decode(msg)
Out[3]: {'a': 3322.1755149730398}

Can you provide some extra platform information (OS, hardware, etc...)? The following script should output some relevant info:

import platform
import sys

uname = platform.uname()

print(f"python: {platform.python_version()}")
for attr in ["machine", "processor", "system", "release"]:
    print(f"{attr}: {getattr(uname, attr)}")
print(f"libc: {platform.libc_ver()}")
print(f"byteorder: {sys.byteorder}")
Oakington commented 1 month ago

python: 3.8.10 machine: AMD64 processor: Intel64 Family 6 Model 165 Stepping 2, GenuineIntel system: Windows release: 10 libc: ('', '') byteorder: little

jack-mcivor commented 1 month ago

I have a similar-ish computer available and can't recreate the issue. Interested

>>> import json
>>> import msgspec
>>> x = '{"a": 3322.1755149730398}'
>>> msgspec.json.decode(x)
{'a': 3322.1755149730398}
python: 3.8.10
machine: AMD64
processor: Intel64 Family 6 Model 141 Stepping 1, GenuineIntel
system: Windows
release: 10
libc: ('', '')
byteorder: little
jcrist commented 1 month ago

Hmmm, we do test on a platform that should be similar enough to this one to catch errors. A few more questions:

Oakington commented 1 month ago

Installed via pip. I don't know if it was a prebuilt as I just updated my venv to 3.11, upgraded pip wheel and setuptools, re-installed msgspec and it's now working.....Very strange. Let me see if I can go back and get specific builtins versions....Will update shortly.

Oakington commented 1 month ago

OK, reproduced with the following details. Let me know if there's anything else I can provide to track this down.

python Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:34:34) [MSC v.1928 32 bit (Intel)] on win32 py -m pip --version pip 21.1.1 from C:\Users\<name>\Documents\deployment scripts\venv\lib\site-packages\pip (python 3.8) easy_install --version setuptools 47.1.0 from c:\program files (x86)\python37-32\lib\site-packages (Python 3.7) dist-info/WHEEL:

Wheel-Version: 1.0
Generator: bdist_wheel (0.43.0)
Root-Is-Purelib: false
Tag: cp38-cp38-win32

dist-info/RECORD:

msgspec-0.18.6.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
msgspec-0.18.6.dist-info/LICENSE,sha256=b96LZyOIXoUIgI9lrDcv65fT7uLqsrkdNNeWt1t6xzQ,1498
msgspec-0.18.6.dist-info/METADATA,sha256=g0eKS6M3Jb387PXe1j1Z60C6LZnUW5d1cCuHc72k9-k,7082
msgspec-0.18.6.dist-info/RECORD,,
msgspec-0.18.6.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
msgspec-0.18.6.dist-info/WHEEL,sha256=57hb2VMKfzdcZEFKIri3BxIFhvran9ahu4uVnDJqFIU,96
msgspec-0.18.6.dist-info/top_level.txt,sha256=8Ysnxa29QLhdPtjPGjyc1hmx3nOmKeu_OAk2682ZdvU,8
msgspec/__init__.py,sha256=4QmZjYKuUQHkpu3H1x4ts2Sg5qcdtiDeo9Gajv4CwPs,1156
msgspec/__init__.pyi,sha256=aUYA7Kox-fxletfoPoVU_HZod3D5lJxzpjLQtZFV2lM,6081
msgspec/__pycache__/__init__.cpython-38.pyc,,
msgspec/__pycache__/_json_schema.cpython-38.pyc,,
msgspec/__pycache__/_utils.cpython-38.pyc,,
msgspec/__pycache__/_version.cpython-38.pyc,,
msgspec/__pycache__/inspect.cpython-38.pyc,,
msgspec/__pycache__/json.cpython-38.pyc,,
msgspec/__pycache__/msgpack.cpython-38.pyc,,
msgspec/__pycache__/structs.cpython-38.pyc,,
msgspec/__pycache__/toml.cpython-38.pyc,,
msgspec/__pycache__/yaml.cpython-38.pyc,,
msgspec/_core.cp38-win32.pyd,sha256=KPEtoqCgZa_KJEFZTmChEcG3Ln676j394LhdYNTKH84,291328
msgspec/_json_schema.py,sha256=bKTi8sOiBuNyEUGJhrjnX4rUhYdjK09MtcDgU8mEo1E,16658
msgspec/_utils.py,sha256=HCvPg_NzLvFpx5SlwnC9SwoYt2UQhldruxG_Ss7RgQA,9048
msgspec/_version.py,sha256=a2wg-8U8xWqU9IRC7WiyOCpBPUcpPFYKTgfQEYpqbZ0,519
msgspec/inspect.py,sha256=uHY4bwjrfNE4UP6rU1PwaZ3-QeyClw-5hpWyXIuFhq4,28896
msgspec/json.py,sha256=kIzpm2NHszkAfAbNcemxq_hUHKt3qxH29yABKH0MnTs,212
msgspec/json.pyi,sha256=-TbORmV1rciB3vMPed-8OPF7Au1_e2fF1iMEQFiAkzY,2995
msgspec/msgpack.py,sha256=JSRLcEqpyCgwy_wD1loF3qHN_ytnCQLc8RX33uATbCU,154
msgspec/msgpack.pyi,sha256=_NpTS8XnjP4ELvqeY-qYWRrsAJi5cvPad7DYICGM3Dw,2566
msgspec/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
msgspec/structs.py,sha256=owU3O3w8yi4_lQYAHIRwM0Hre0Oo_puNbz4SM0GP0ew,2942
msgspec/structs.pyi,sha256=6ob7yIjD6QwMUn_vJHC0CgWgt3CtgRAU2AOUq_1EPGU,902
msgspec/toml.py,sha256=f0Vte-oW3WK5avMFHf22WuqoccaaVGGKFu98m43wu9Q,5389
msgspec/yaml.py,sha256=LT2T0HW-FtHq_wGBD3sGtpgXspN6N5SmesV1BHrSvPs,5260
jack-mcivor commented 1 month ago

Ah that's 32-bit Python on a 64-bit system I think? I won't be able to help more but for completeness, my Python installation was 64 bit.

jcrist commented 1 month ago

Yeah, and we don't ship 32-bit windows wheels, so that definitely was compiled on your machine. Not surprised there's compatibility bugs. Were you intentionally using a 32 bit python version, or is that just incidental? I'd been viewing 32-bit windows builds as a platform with low enough usage that it wasn't worth supporting (conda-forge, for example, doesn't ship any win32 builds). If I'm mistaken here we could (with some effort) probably squash these bugs and start building wheels for that platform as needed.

Oakington commented 1 month ago

No, it's not needed. It was an older install that I used for one off scripts. Anything in PROD is x64. Might be worth a warning somewhere? Not sure what's best. Just threw me off and I wouldn't have caught it if I wasn't looking at that specific value in the payload.

I really appreciate this library. It's so fast that I'm refactoring a lot of my APIs to use it as well as proselytizing to my coworkers that are using other parsing libs. So thank you for all your efforts!

jcrist commented 1 month ago

Thanks for the kind words, and glad you've figured out the issue. I've added a check for this in #696 that should prevent anyone else from trying to build msgspec on 32-bit windows in the future.