Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"ValueError: Circular reference detected." exception on json5.dumps call #30

Closed
emocibob opened this issue Oct 23, 2019 · 4 comments
Closed

Comments

@emocibob
Copy link

When I call json5.dumps on a previously loaded and modified object, I get the following exception:

ValueError: Circular reference detected.

My environment info:

  • Ubuntu 19.04
  • Python 3.7.3
  • json5 0.8.5

Code to recreate the issue (I know there's a logical error since REGION is modified for both development and t):

import json5

def demo():
    js_object_str = """{
      development: {
        REGION: "foo",
      },
      prod: {
        REGION: "baz"
      }
    }"""

    all_configs = json5.loads(js_object_str)

    new_config = all_configs["development"]
    new_config.update({"REGION": "bar"})
    all_configs["t"] = new_config

    print(all_configs)
    print(json5.dumps(all_configs))  # this causes the error

demo()

Workaround:
Import from copy import deepcopy and change all_configs["t"] = new_config to all_configs["t"] = deepcopy(new_config).

@roosephu
Copy link
Contributor

I encountered the same issue. The reason is that we only add objects to the set seen and never remove them (https://github.com/dpranke/pyjson5/blob/master/json5/lib.py#L210-L215). When detecting circular reference, the object needs to be removed after being processed, i.e., before each return of function _dumps.

@dpranke
Copy link
Owner

dpranke commented Jan 14, 2020

I apologize for the delay in getting to this; I'll try to get a patch posted shortly.

@dpranke
Copy link
Owner

dpranke commented Jan 29, 2020

I'm looking at this now. I think @roosephu's comment is correct.

@dpranke
Copy link
Owner

dpranke commented Jan 29, 2020

I should have a release with a fix for this and a few other bugs in the next day or two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants