This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Krzysztof Nazarewski
Recipients Krzysztof Nazarewski
Date 2017-06-22.09:38:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1498124337.07.0.93184881583.issue30732@psf.upfronthosting.co.za>
In-reply-to
Content
RecursionErrors related to json.dumps' default argument give no information whatsoever about the underlying issue.

Example:

$ /usr/bin/python3 -c "import json;from decimal import Decimal;json.dumps(Decimal(),default=lambda v:round(v, 8))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "<string>", line 1, in <lambda>
  File "/usr/lib/python3.6/_pydecimal.py", line 1919, in __round__
    return self.quantize(exp)
  File "/usr/lib/python3.6/_pydecimal.py", line 2603, in quantize
    return ans._fix(context)
  File "/usr/lib/python3.6/_pydecimal.py", line 1720, in _fix
    if not self:
  File "/usr/lib/python3.6/_pydecimal.py", line 852, in __bool__
    return self._is_special or self._int != '0'
RecursionError: maximum recursion depth exceeded in comparison


Details:
I have encountered issue when porting my code from Python 2 to 3.
Traceback shows depth of 6, but says recursion was already reached.
My first idea was that recursion counter was messed up, but it was not the case.
Recursion limit was indeed reached because round() in Python 3 is returning Decimal and then applies default infinitely.
The part of recursing code was inside C so it was not displayed in the traceback.

Summing up it took me over 2 hours to determine what was wrong with the code.


Fix ideas:
- do not call default more than once on the same root object (might be problematic to implement)
- raise an error if new object is equal to the previous (partially resolving issue since custom objects might not implement equality operator)
- add a flag to raise an error when new object's class is equal to the previous (might give false positives hence the flag)
History
Date User Action Args
2017-06-22 09:38:57Krzysztof Nazarewskisetrecipients: + Krzysztof Nazarewski
2017-06-22 09:38:57Krzysztof Nazarewskisetmessageid: <1498124337.07.0.93184881583.issue30732@psf.upfronthosting.co.za>
2017-06-22 09:38:57Krzysztof Nazarewskilinkissue30732 messages
2017-06-22 09:38:56Krzysztof Nazarewskicreate