Skip to content

Commit

Permalink
Fixed Issue 42: Handled another type of exception and added testcase … (
Browse files Browse the repository at this point in the history
#49)

Fixes issue #42, falling back to str when json fails to handle circular references
  • Loading branch information
saurabhkpatel authored and dbieber committed Apr 7, 2017
1 parent f48f93b commit a0791c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def _OneLineResult(result):

try:
return json.dumps(result)
except TypeError:
except (TypeError, ValueError):
return str(result).replace('\n', ' ')


Expand Down
6 changes: 6 additions & 0 deletions fire/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def testOneLineResult(self):
self.assertEqual(core._OneLineResult({}), '{}') # pylint: disable=protected-access
self.assertEqual(core._OneLineResult({'x': 'y'}), '{"x": "y"}') # pylint: disable=protected-access


def testOneLineResultCircularRef(self):
circular_reference = tc.CircularReference()
self.assertEqual(core._OneLineResult(circular_reference.create()), # pylint: disable=protected-access
"{'y': {...}}")

@mock.patch('fire.interact.Embed')
def testInteractiveMode(self, mock_embed):
core.Fire(tc.TypedProperties, 'alpha')
Expand Down
8 changes: 8 additions & 0 deletions fire/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,11 @@ def totally_empty(self):

def nothing_printable(self):
return {'__do_not_print_me': 1}


class CircularReference(object):

def create(self):
x = {}
x['y'] = x
return x

0 comments on commit a0791c9

Please sign in to comment.