1 | n/a | from test.test_json import PyTest, CTest |
---|
2 | n/a | |
---|
3 | n/a | |
---|
4 | n/a | class JSONTestObject: |
---|
5 | n/a | pass |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | class TestRecursion: |
---|
9 | n/a | def test_listrecursion(self): |
---|
10 | n/a | x = [] |
---|
11 | n/a | x.append(x) |
---|
12 | n/a | try: |
---|
13 | n/a | self.dumps(x) |
---|
14 | n/a | except ValueError: |
---|
15 | n/a | pass |
---|
16 | n/a | else: |
---|
17 | n/a | self.fail("didn't raise ValueError on list recursion") |
---|
18 | n/a | x = [] |
---|
19 | n/a | y = [x] |
---|
20 | n/a | x.append(y) |
---|
21 | n/a | try: |
---|
22 | n/a | self.dumps(x) |
---|
23 | n/a | except ValueError: |
---|
24 | n/a | pass |
---|
25 | n/a | else: |
---|
26 | n/a | self.fail("didn't raise ValueError on alternating list recursion") |
---|
27 | n/a | y = [] |
---|
28 | n/a | x = [y, y] |
---|
29 | n/a | # ensure that the marker is cleared |
---|
30 | n/a | self.dumps(x) |
---|
31 | n/a | |
---|
32 | n/a | def test_dictrecursion(self): |
---|
33 | n/a | x = {} |
---|
34 | n/a | x["test"] = x |
---|
35 | n/a | try: |
---|
36 | n/a | self.dumps(x) |
---|
37 | n/a | except ValueError: |
---|
38 | n/a | pass |
---|
39 | n/a | else: |
---|
40 | n/a | self.fail("didn't raise ValueError on dict recursion") |
---|
41 | n/a | x = {} |
---|
42 | n/a | y = {"a": x, "b": x} |
---|
43 | n/a | # ensure that the marker is cleared |
---|
44 | n/a | self.dumps(x) |
---|
45 | n/a | |
---|
46 | n/a | def test_defaultrecursion(self): |
---|
47 | n/a | class RecursiveJSONEncoder(self.json.JSONEncoder): |
---|
48 | n/a | recurse = False |
---|
49 | n/a | def default(self, o): |
---|
50 | n/a | if o is JSONTestObject: |
---|
51 | n/a | if self.recurse: |
---|
52 | n/a | return [JSONTestObject] |
---|
53 | n/a | else: |
---|
54 | n/a | return 'JSONTestObject' |
---|
55 | n/a | return pyjson.JSONEncoder.default(o) |
---|
56 | n/a | |
---|
57 | n/a | enc = RecursiveJSONEncoder() |
---|
58 | n/a | self.assertEqual(enc.encode(JSONTestObject), '"JSONTestObject"') |
---|
59 | n/a | enc.recurse = True |
---|
60 | n/a | try: |
---|
61 | n/a | enc.encode(JSONTestObject) |
---|
62 | n/a | except ValueError: |
---|
63 | n/a | pass |
---|
64 | n/a | else: |
---|
65 | n/a | self.fail("didn't raise ValueError on default recursion") |
---|
66 | n/a | |
---|
67 | n/a | |
---|
68 | n/a | def test_highly_nested_objects_decoding(self): |
---|
69 | n/a | # test that loading highly-nested objects doesn't segfault when C |
---|
70 | n/a | # accelerations are used. See #12017 |
---|
71 | n/a | with self.assertRaises(RecursionError): |
---|
72 | n/a | self.loads('{"a":' * 100000 + '1' + '}' * 100000) |
---|
73 | n/a | with self.assertRaises(RecursionError): |
---|
74 | n/a | self.loads('{"a":' * 100000 + '[1]' + '}' * 100000) |
---|
75 | n/a | with self.assertRaises(RecursionError): |
---|
76 | n/a | self.loads('[' * 100000 + '1' + ']' * 100000) |
---|
77 | n/a | |
---|
78 | n/a | def test_highly_nested_objects_encoding(self): |
---|
79 | n/a | # See #12051 |
---|
80 | n/a | l, d = [], {} |
---|
81 | n/a | for x in range(100000): |
---|
82 | n/a | l, d = [l], {'k':d} |
---|
83 | n/a | with self.assertRaises(RecursionError): |
---|
84 | n/a | self.dumps(l) |
---|
85 | n/a | with self.assertRaises(RecursionError): |
---|
86 | n/a | self.dumps(d) |
---|
87 | n/a | |
---|
88 | n/a | def test_endless_recursion(self): |
---|
89 | n/a | # See #12051 |
---|
90 | n/a | class EndlessJSONEncoder(self.json.JSONEncoder): |
---|
91 | n/a | def default(self, o): |
---|
92 | n/a | """If check_circular is False, this will keep adding another list.""" |
---|
93 | n/a | return [o] |
---|
94 | n/a | |
---|
95 | n/a | with self.assertRaises(RecursionError): |
---|
96 | n/a | EndlessJSONEncoder(check_circular=False).encode(5j) |
---|
97 | n/a | |
---|
98 | n/a | |
---|
99 | n/a | class TestPyRecursion(TestRecursion, PyTest): pass |
---|
100 | n/a | class TestCRecursion(TestRecursion, CTest): pass |
---|