Skip to content

json.load() can raise undocumented RecursionError #135835

Closed as not planned
Closed as not planned

Description

@jwilk

json.load() documentation lists JSONDecodeError and UnicodeDecodeError as the exceptions that can be raised.
But in https://news.ycombinator.com/item?id=44344864 it was noted that RecursionError is possible too:

>>> import json
>>> json.loads('[' * 9999)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    json.loads('[' * 9999)
    ~~~~~~~~~~^^^^^^^^^^^^
  File "/usr/lib/python3.13/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^
  File "/usr/lib/python3.13/json/decoder.py", line 345, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/json/decoder.py", line 361, in raw_decode
    obj, end = self.scan_once(s, idx)
               ~~~~~~~~~~~~~~^^^^^^^^
RecursionError: maximum recursion depth exceeded while decoding a JSON array from a unicode string

Activity

  1. sobolevn commented on Jun 23, 2025

    @sobolevn
    Member

    Almost anything can raise RecursionError. It is very similar to MemoryError. I am -1 on adding this to the docs.

  2. added
    docs
    on Jun 23, 2025
  3. picnixz commented on Jun 23, 2025

    @picnixz
    Member

    The same can be said for AttributeError that can be raised if you do json.load(1). Note that json.loads also doesn't say that it can raise an explicit TypeError if JSON object is incorrect.

  4. added
    pending
    on Jun 23, 2025
  5. LamentXU123 commented on Jun 25, 2025

    @LamentXU123
    Contributor

    We usually only documented the relatively unique Errors IMO.

  6. shakna-israel commented on Jun 25, 2025

    @shakna-israel

    Hiya. I wrote the comment that prompted this.
    Five years ago, I raised this, and the team here decided it wasn't a flaw and closed it out - I'm surprised this has stayed open as long as it has.

    That being said, both RecursionError and AttributeError appearing are probably surprising to the average Python programmer. They're leaks of implementation details. Not something I'd reasonably expect the average programmer to even realise is possible.

    That surprise, is why I mentioned it on HN, and why someone else thought it worthy of "bug".

    If something can always throw whatever, then we may as well get rid of classes for exceptions and catch everything.

    But again... I've stated all this before. I expect this to simply close without changes.

  7. serhiy-storchaka commented on Jun 26, 2025

    @serhiy-storchaka
    Member

    I agree with @sobolevn. RecursionError, MemoryError and KeyboardInterrupt can be raised by almost anything. SystemExit or other exception can be added to this list if we add the default SIGTERM handler. That would be burdensome to add these exceptions to documentation of every function.

  8. vstinner commented on Jun 26, 2025

    @vstinner
    Member

    I close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docspending

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions