diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2011-05-14 03:24:53 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2011-05-14 03:24:53 (GMT) |
commit | e3992eb7430101f5bc6725d5f74290777a2ef939 (patch) | |
tree | 8300fe6afce165f52da14b5229cdb4ec50161602 /Lib/json/tests/test_recursion.py | |
parent | 720f8dea196640a053135f31be89b652d685e55c (diff) | |
download | cpython-e3992eb7430101f5bc6725d5f74290777a2ef939.zip cpython-e3992eb7430101f5bc6725d5f74290777a2ef939.tar.gz cpython-e3992eb7430101f5bc6725d5f74290777a2ef939.tar.bz2 |
#5723: Improve json tests to be executed with and without accelerations.
Diffstat (limited to 'Lib/json/tests/test_recursion.py')
-rw-r--r-- | Lib/json/tests/test_recursion.py | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/Lib/json/tests/test_recursion.py b/Lib/json/tests/test_recursion.py index 1f2d7b3..b5221e5 100644 --- a/Lib/json/tests/test_recursion.py +++ b/Lib/json/tests/test_recursion.py @@ -1,33 +1,16 @@ -from unittest import TestCase +from json.tests import PyTest, CTest -import json class JSONTestObject: pass -class RecursiveJSONEncoder(json.JSONEncoder): - recurse = False - def default(self, o): - if o is JSONTestObject: - if self.recurse: - return [JSONTestObject] - else: - return 'JSONTestObject' - return json.JSONEncoder.default(o) - -class EndlessJSONEncoder(json.JSONEncoder): - def default(self, o): - """If check_circular is False, this will keep adding another list.""" - return [o] - - -class TestRecursion(TestCase): +class TestRecursion(object): def test_listrecursion(self): x = [] x.append(x) try: - json.dumps(x) + self.dumps(x) except ValueError: pass else: @@ -36,7 +19,7 @@ class TestRecursion(TestCase): y = [x] x.append(y) try: - json.dumps(x) + self.dumps(x) except ValueError: pass else: @@ -44,13 +27,13 @@ class TestRecursion(TestCase): y = [] x = [y, y] # ensure that the marker is cleared - json.dumps(x) + self.dumps(x) def test_dictrecursion(self): x = {} x["test"] = x try: - json.dumps(x) + self.dumps(x) except ValueError: pass else: @@ -58,9 +41,19 @@ class TestRecursion(TestCase): x = {} y = {"a": x, "b": x} # ensure that the marker is cleared - json.dumps(x) + self.dumps(x) def test_defaultrecursion(self): + class RecursiveJSONEncoder(self.json.JSONEncoder): + recurse = False + def default(self, o): + if o is JSONTestObject: + if self.recurse: + return [JSONTestObject] + else: + return 'JSONTestObject' + return pyjson.JSONEncoder.default(o) + enc = RecursiveJSONEncoder() self.assertEqual(enc.encode(JSONTestObject), '"JSONTestObject"') enc.recurse = True @@ -77,18 +70,18 @@ class TestRecursion(TestCase): # accelerations are used. See #12017 # str with self.assertRaises(RuntimeError): - json.loads('{"a":' * 100000 + '1' + '}' * 100000) + self.loads('{"a":' * 100000 + '1' + '}' * 100000) with self.assertRaises(RuntimeError): - json.loads('{"a":' * 100000 + '[1]' + '}' * 100000) + self.loads('{"a":' * 100000 + '[1]' + '}' * 100000) with self.assertRaises(RuntimeError): - json.loads('[' * 100000 + '1' + ']' * 100000) + self.loads('[' * 100000 + '1' + ']' * 100000) # unicode with self.assertRaises(RuntimeError): - json.loads(u'{"a":' * 100000 + u'1' + u'}' * 100000) + self.loads(u'{"a":' * 100000 + u'1' + u'}' * 100000) with self.assertRaises(RuntimeError): - json.loads(u'{"a":' * 100000 + u'[1]' + u'}' * 100000) + self.loads(u'{"a":' * 100000 + u'[1]' + u'}' * 100000) with self.assertRaises(RuntimeError): - json.loads(u'[' * 100000 + u'1' + u']' * 100000) + self.loads(u'[' * 100000 + u'1' + u']' * 100000) def test_highly_nested_objects_encoding(self): # See #12051 @@ -96,11 +89,20 @@ class TestRecursion(TestCase): for x in xrange(100000): l, d = [l], {'k':d} with self.assertRaises(RuntimeError): - json.dumps(l) + self.dumps(l) with self.assertRaises(RuntimeError): - json.dumps(d) + self.dumps(d) def test_endless_recursion(self): # See #12051 + class EndlessJSONEncoder(self.json.JSONEncoder): + def default(self, o): + """If check_circular is False, this will keep adding another list.""" + return [o] + with self.assertRaises(RuntimeError): EndlessJSONEncoder(check_circular=False).encode(5j) + + +class TestPyRecursion(TestRecursion, PyTest): pass +class TestCRecursion(TestRecursion, CTest): pass |