X Tutup
The Wayback Machine - https://web.archive.org/web/20250609105836/https://github.com/python/cpython/pull/113458
Skip to content

gh-74917: Raise TypeError in the JSON encoder if the default function returns the same type #113458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking 鈥淪ign up for GitHub鈥, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Dec 24, 2023

@serhiy-storchaka
Copy link
Member Author

Based on #2857. Added test, Python implementation, docs.

@serhiy-storchaka
Copy link
Member Author

@etrepum, could you please look at this? What are your thoughts about this matter, is it worth to do?

@etrepum
Copy link
Contributor

etrepum commented Dec 25, 2023

This does catch some possible errors, but there are plenty of other ways to run into similar issues if you're not careful. For example:

>>> json.dumps(..., default=lambda x: [lambda x: x])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded while encoding a JSON object

Perhaps a general purpose "solution" would be to catch the RecursionError and re-raise with more useful context about the value that was being serialized?

@encukou
Copy link
Member

encukou commented Jan 3, 2025

Fixed in #122165 instead.

@encukou encukou closed this Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
X Tutup