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 <>
RecursionErrors related to json.dumps' default argument give no information whatsoever about the underlying issue.


$ /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/", line 238, in dumps
  File "/usr/lib/python3.6/json/", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.6/json/", line 257, in iterencode
    return _iterencode(o, 0)
  File "<string>", line 1, in <lambda>
  File "/usr/lib/python3.6/", line 1919, in __round__
    return self.quantize(exp)
  File "/usr/lib/python3.6/", line 2603, in quantize
    return ans._fix(context)
  File "/usr/lib/python3.6/", line 1720, in _fix
    if not self:
  File "/usr/lib/python3.6/", line 852, in __bool__
    return self._is_special or self._int != '0'
RecursionError: maximum recursion depth exceeded in comparison

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)
Date User Action Args
2017-06-22 09:38:57Krzysztof Nazarewskisetrecipients: + Krzysztof Nazarewski
2017-06-22 09:38:57Krzysztof Nazarewskisetmessageid: <>
2017-06-22 09:38:57Krzysztof Nazarewskilinkissue30732 messages
2017-06-22 09:38:56Krzysztof Nazarewskicreate