Skip to content

json decoder raise undocumented exception ValueError if input contains very large integer #154533

Description

@pmgast

Documentation

Hi all,

I encountered a undocumented Exception while decoding a (faulty) json string containing a very large integer:

>>> import json
>>> with open('json_file_with_large_number') as f:
...     json.load(f)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib64/python3.11/json/__init__.py", line 293, in load
    return loads(fp.read(),
           ^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
ValueError: Exceeds the limit (4300 digits) for integer string conversion: value has 20751 digits; use sys.set_int_max_str_digits() to increase the limit

the documentation only mentions that load() can raise JSONDecodeError or UnicodeDecodeError.

The data for this incident came from non-python code, but it can be reproduced with the following lines:

import sys
sys.set_int_max_str_digits(4500)
realy_large_number = str(10**4499)
json_str = '{"number" : ' + realy_large_number+ '}'
with open('json_file_with_large_number','w') as f:
    f.write(json_str)
sys.set_int_max_str_digits(4300)
# An application that processes data from external sources would contain only the following code
import json
try:
    with open('json_file_with_large_number') as f:
        data = json.load(f)
except json.JSONDecodeError as e:
    print('This is anticipated')
except ValueError as e:
    print('This is not anticipated')
    raise e

setting the max_str_digits above the data digits will solve the problem as mentioned in the stacktrace. However the undocumented raise of VauleError will still lead to a crash of the application even if a JSONDecodeError was anticipated and handled. In my opinion the ValueError should be cought and reraised as JSONDecodeError but I am not sure if this is not just a documentation thing. I would love to hear your opinion on this.
Thank you for your help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status
    No status
    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions