FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-74917: Raise TypeError in the JSON encoder if the default function returns the same type by serhiy-storchaka · Pull Request #113458 · python/cpython · GitHub

/ cpython Public

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

Closed
serhiy-storchaka wants to merge 8 commits into
python:mainfrom
serhiy-storchaka:bpo-30732
Closed

gh-74917: Raise TypeError in the JSON encoder if the default function returns the same type#113458
serhiy-storchaka wants to merge 8 commits into
python:mainfrom
serhiy-storchaka:bpo-30732

Conversation

serhiy-storchaka commented Dec 24, 2023
edited by github-actions Bot
Loading

Copy link
Copy Markdown
Member

Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
Member Author

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

etrepum commented Dec 25, 2023

Copy link
Copy Markdown
Contributor

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 commented Jan 3, 2025

Copy link
Copy Markdown
Member

Fixed in #122165 instead.

encukou closed this Jan 3, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL