Permalink
Browse files

add circular reference check in dump

  • Loading branch information...
1 parent ac110e7 commit ba1c749563872b40bbb6db5a5043338d85969c99 @sunng87 committed Feb 16, 2012
Showing with 12 additions and 1 deletion.
  1. +7 −0 clj.py
  2. +5 −1 tests/clj-test.py
View
7 clj.py
@@ -220,6 +220,7 @@ class CljEncoder(object):
def __init__(self, data, fd):
self.data = data
self.fd = fd
+ self.circular = {}
def encode(self):
self.__do_encode(self.data)
@@ -247,6 +248,12 @@ def __do_encode(self, d):
t,coll = self.get_type(d)
if coll:
+ refid = id(d)
+ if refid in self.circular:
+ raise ValueError('Circular reference detected')
+ else:
+ self.circular[refid] = True
+
if t == "dict":
fd.write("{")
for k,v in d.items():
View
6 tests/clj-test.py
@@ -54,7 +54,11 @@ def test_misformed_data(self):
for d in data:
self.assertRaises(ValueError, clj.loads, d)
-
+ def test_circular_ref(self):
+ s = [1,2,3]
+ d = {"a": [], "b": s}
+ s.append(d)
+ self.assertRaises(ValueError, clj.dumps, d)
if __name__ == '__main__':
unittest.main()

0 comments on commit ba1c749

Please sign in to comment.