Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:
I guess json module expects a return value from default function that is str, int, float, etc. that can be handled by built in encoders and returning a bytes object with repr causes it to keep calling default until a value of one of expected type is returned. With the encoded repr producing new objects circular reference cannot be detected. In the below example I keep returning encoded repr as per the original report and then return a string once length is more than 100. I think this is something similar to https://bugs.python.org/issue21213#msg217203 . # /tmp/foo.py import json; def default(o): out = repr(o).encode() print(id(out)) if len(out) > 100: print(out) return "string" return out print(json.dumps(object(), default=default)) ./python.exe /tmp/foo.py 4329334144 4328290960 4328289400 4329073088 4328910624 4329031520 4327246416 4328875288 b'b\'b\\\'b\\\\\\\'b\\\\\\\\\\\\\\\'b\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'b"b\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'<object object at 0x101da6580>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\'\\\\\\\'\\\'\'' "string" ---------- nosy: +ezio.melotti, rhettinger, xtreak _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue35901> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com