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

json cannot serialize circular references #42

Closed
dbieber opened this issue Mar 28, 2017 · 3 comments
Closed

json cannot serialize circular references #42

dbieber opened this issue Mar 28, 2017 · 3 comments
Labels

Comments

@dbieber
Copy link
Member

dbieber commented Mar 28, 2017

Here's a minimal example of the failure:

import fire
x = {}
x['y'] = x
fire.Fire()

Running python example.py gives this traceback:

Traceback (most recent call last):
  File "example.py", line 4, in <module>
    fire.Fire()
  File "/Users/dbieber/virtualenvs/_/lib/python2.7/site-packages/fire/core.py", line 145, in Fire
    _PrintResult(component_trace, verbose=component_trace.verbose)
  File "/Users/dbieber/virtualenvs/_/lib/python2.7/site-packages/fire/core.py", line 181, in _PrintResult
    print(_DictAsString(result, verbose))
  File "/Users/dbieber/virtualenvs/_/lib/python2.7/site-packages/fire/core.py", line 210, in _DictAsString
    key=str(key) + ':', value=_OneLineResult(value))
  File "/Users/dbieber/virtualenvs/_/lib/python2.7/site-packages/fire/core.py", line 230, in _OneLineResult
    return json.dumps(result)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
ValueError: Circular reference detected

The problem is that _DictAsString tries to serialize [each key of] the dict using json, but json cannot serialize dicts w/ circular references.

We'll need a different serialization technique to support this case.

@dbieber dbieber added the bug label Mar 28, 2017
@dbieber
Copy link
Member Author

dbieber commented Apr 3, 2017

Saurabh may take this as a first task.

@saurabhkpatel
Copy link
Contributor

I will submit the pull request soon for this bug.
My thoughts are something like this:

Handle extra type of exception and add the appropriate message in front of the particular key. In the below example, key's value is represented same like this.

fire: <module 'fire' from '/Users/srpatel/github/python-fire/fire/__init__.pyc'>
x:    {'y': {...}}

dbieber pushed a commit that referenced this issue Apr 7, 2017
#49)

Fixes issue #42, falling back to str when json fails to handle circular references
@dbieber
Copy link
Member Author

dbieber commented Apr 7, 2017

Fixed by #49, thank you Saurabh!

@dbieber dbieber closed this as completed Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants