8000 Fixed Issue 42: Handled another type of exception and added testcase … · GitObjects/python-fire@a0791c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a0791c9

Browse files
saurabhkpateldbieber
authored andcommitted
Fixed Issue 42: Handled another type of exception and added testcase … (google#49)
Fixes issue google#42, falling back to str when json fails to handle circular references
1 parent f48f93b commit a0791c9

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

fire/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _OneLineResult(result):
247247

248248
try:
249249
return json.dumps(result)
250-
except TypeError:
250+
except (TypeError, ValueError):
251251
return str(result).replace('\n', ' ')
252252

253253

fire/core_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def testOneLineResult(self):
3333
self.assertEqual(core._OneLineResult({}), '{}') # pylint: disable=protected-access
3434
self.assertEqual(core._OneLineResult({'x': 'y'}), '{"x": "y"}') # pylint: disable=protected-access
3535

36+
37+
def testOneLineResultCircularRef(self):
38+
circular_reference = tc.CircularReference()
39+
self.assertEqual(core._OneLineResult(circular_reference.create()), # pylint: disable=protected-access
40+
"{'y': {...}}")
41+
3642
@mock.patch('fire.interact.Embed')
3743
def testInteractiveMode(self, mock_embed):
3844
core.Fire(tc.TypedProperties, 'alpha')

fire/test_components.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,11 @@ def totally_empty(self):
206206

207207
def nothing_printable(self):
208208
return {'__do_not_print_me': 1}
209+
210+
211+
class CircularReference(object):
212+
213+
def create(self):
214+
x = {}
215+
x['y'] = x
216+
return x

0 commit comments

Comments
 (0)
0