Skip to content

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/[email protected]/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/[email protected]/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/[email protected]/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