New submission from Krzysztof Nazarewski:

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)

----------
components: Library (Lib)
messages: 296620
nosy: Krzysztof Nazarewski
priority: normal
severity: normal
status: open
title: json.dumps() lack of information about RecursionError when using default 
function
type: enhancement
versions: Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30732>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to