KenKundert / nestedtext

Human readable and writable data interchange format
https://nestedtext.org
MIT License
362 stars 13 forks source link

Encoding object with cyclic references uncaught RecursionError error #30

Closed erikw closed 2 years ago

erikw commented 2 years ago

This program

import nestedtext

a = []
b = [a]
a.append(b)

nestedtext.dumps(b)

will give

RecursionError: maximum recursion depth exceeded in comparison

As I just constructed these simple objects, it is obvious what this error mean. But for a more complex object, that I did not write just now or even myself, the user of NestedText might be confused.

Solution ideas:

  1. Detect cyclic references and quit early with error message
    • While still keeping the possibility of using the same references multiple times i.e. this program should still work:
      a = []
      b = [a, a]
      nestedtext.dumps(b)
  2. Catch RecursionError and raise an error describing that the object to be dumped have cyclic references, and thus can not be dumped.
KenKundert commented 2 years ago

Good catch. I have updated the version on GitHub with a fix. Please give it a try and let me know if it works for you as expected.

erikw commented 2 years ago

Thank you for quick response! With HEAD of the master branch, the above program now yields

nestedtext.NestedTextError: 0, 0, 0: cyclic reference found: cannot be dumped.

KenKundert commented 2 years ago

I have released the updated version to pypi. Thanks for reporting this and testing the fix.