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 intopython:mainfrom
Closed
gh-74917: Raise TypeError in the JSON encoder if the default function returns the same type#113458serhiy-storchaka wants to merge 8 commits intopython:mainfrom
serhiy-storchaka wants to merge 8 commits intopython:mainfrom
Conversation
…d to default function
…d to default function
…d to default function
…d to default function
Member
Author
|
Based on #2857. Added test, Python implementation, docs. |
Member
Author
|
@etrepum, could you please look at this? What are your thoughts about this matter, is it worth to do? |
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 objectPerhaps a general purpose "solution" would be to catch the RecursionError and re-raise with more useful context about the value that was being serialized? |
Member
|
Fixed in #122165 instead. |
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Co-authored-by: Siddharth Velankar svelankar@users.noreply.github.com
📚 Documentation preview 📚: https://cpython-previews--113458.org.readthedocs.build/