annotate Lib/json/tests/test_recursion.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 5b0fecd2eba0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
1 from json.tests import PyTest, CTest
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
3
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
4 class JSONTestObject:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5 pass
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
6
46685
7106ca871bfd PEP 8 nits in json package
Benjamin Peterson <benjamin@python.org>
parents: 46676
diff changeset
7
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
8 class TestRecursion(object):
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
9 def test_listrecursion(self):
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
10 x = []
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
11 x.append(x)
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
12 try:
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
13 self.dumps(x)
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
14 except ValueError:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
15 pass
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16 else:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
17 self.fail("didn't raise ValueError on list recursion")
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
18 x = []
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
19 y = [x]
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
20 x.append(y)
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
21 try:
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
22 self.dumps(x)
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
23 except ValueError:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
24 pass
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
25 else:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
26 self.fail("didn't raise ValueError on alternating list recursion")
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
27 y = []
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
28 x = [y, y]
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
29 # ensure that the marker is cleared
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
30 self.dumps(x)
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
31
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
32 def test_dictrecursion(self):
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
33 x = {}
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
34 x["test"] = x
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
35 try:
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
36 self.dumps(x)
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
37 except ValueError:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
38 pass
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
39 else:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
40 self.fail("didn't raise ValueError on dict recursion")
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
41 x = {}
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
42 y = {"a": x, "b": x}
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
43 # ensure that the marker is cleared
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
44 self.dumps(x)
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
45
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
46 def test_defaultrecursion(self):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
47 class RecursiveJSONEncoder(self.json.JSONEncoder):
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
48 recurse = False
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
49 def default(self, o):
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
50 if o is JSONTestObject:
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
51 if self.recurse:
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
52 return [JSONTestObject]
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
53 else:
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
54 return 'JSONTestObject'
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
55 return pyjson.JSONEncoder.default(o)
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
56
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
57 enc = RecursiveJSONEncoder()
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 46685
diff changeset
58 self.assertEqual(enc.encode(JSONTestObject), '"JSONTestObject"')
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
59 enc.recurse = True
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
60 try:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
61 enc.encode(JSONTestObject)
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
62 except ValueError:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
63 pass
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
64 else:
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
65 self.fail("didn't raise ValueError on default recursion")
69909
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
66
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
67
70020
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
68 def test_highly_nested_objects_decoding(self):
69909
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
69 # test that loading highly-nested objects doesn't segfault when C
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
70 # accelerations are used. See #12017
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
71 # str
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
72 with self.assertRaises(RuntimeError):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
73 self.loads('{"a":' * 100000 + '1' + '}' * 100000)
69909
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
74 with self.assertRaises(RuntimeError):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
75 self.loads('{"a":' * 100000 + '[1]' + '}' * 100000)
69909
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
76 with self.assertRaises(RuntimeError):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
77 self.loads('[' * 100000 + '1' + ']' * 100000)
69909
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
78 # unicode
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
79 with self.assertRaises(RuntimeError):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
80 self.loads(u'{"a":' * 100000 + u'1' + u'}' * 100000)
69909
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
81 with self.assertRaises(RuntimeError):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
82 self.loads(u'{"a":' * 100000 + u'[1]' + u'}' * 100000)
69909
6fa20e360e5e #12017: Fix segfault in json.loads() while decoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66348
diff changeset
83 with self.assertRaises(RuntimeError):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
84 self.loads(u'[' * 100000 + u'1' + u']' * 100000)
70020
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
85
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
86 def test_highly_nested_objects_encoding(self):
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
87 # See #12051
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
88 l, d = [], {}
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
89 for x in xrange(100000):
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
90 l, d = [l], {'k':d}
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
91 with self.assertRaises(RuntimeError):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
92 self.dumps(l)
70020
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
93 with self.assertRaises(RuntimeError):
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
94 self.dumps(d)
70020
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
95
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
96 def test_endless_recursion(self):
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
97 # See #12051
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
98 class EndlessJSONEncoder(self.json.JSONEncoder):
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
99 def default(self, o):
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
100 """If check_circular is False, this will keep adding another list."""
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
101 return [o]
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
102
70020
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
103 with self.assertRaises(RuntimeError):
a21f5af476cb #12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69909
diff changeset
104 EndlessJSONEncoder(check_circular=False).encode(5j)
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
105
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
106
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
107 class TestPyRecursion(TestRecursion, PyTest): pass
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70020
diff changeset
108 class TestCRecursion(TestRecursion, CTest): pass