Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b60fb9

Browse files
author
Ezio Melotti
committed
#5723: merge with 3.1.
1 parent 3659f27 commit 6b60fb9

19 files changed

Lines changed: 253 additions & 218 deletions

Lib/json/decoder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import struct
77

8-
from json.scanner import make_scanner
8+
from json import scanner
99
try:
1010
from _json import scanstring as c_scanstring
1111
except ImportError:
@@ -340,7 +340,7 @@ def __init__(self, object_hook=None, parse_float=None,
340340
self.parse_array = JSONArray
341341
self.parse_string = scanstring
342342
self.memo = {}
343-
self.scan_once = make_scanner(self)
343+
self.scan_once = scanner.make_scanner(self)
344344

345345

346346
def decode(self, s, _w=WHITESPACE.match):

Lib/test/json_tests/__init__.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
11
import os
22
import sys
3-
import unittest
3+
import json
44
import doctest
5+
import unittest
6+
7+
from test import support
8+
9+
# import json with and without accelerations
10+
cjson = support.import_fresh_module('json', fresh=['_json'])
11+
pyjson = support.import_fresh_module('json', blocked=['_json'])
12+
13+
# create two base classes that will be used by the other tests
14+
class PyTest(unittest.TestCase):
15+
json = pyjson
16+
loads = staticmethod(pyjson.loads)
17+
dumps = staticmethod(pyjson.dumps)
18+
19+
@unittest.skipUnless(cjson, 'requires _json')
20+
class CTest(unittest.TestCase):
21+
if cjson is not None:
22+
json = cjson
23+
loads = staticmethod(cjson.loads)
24+
dumps = staticmethod(cjson.dumps)
25+
26+
# test PyTest and CTest checking if the functions come from the right module
27+
class TestPyTest(PyTest):
28+
def test_pyjson(self):
29+
self.assertEqual(self.json.scanner.make_scanner.__module__,
30+
'json.scanner')
31+
self.assertEqual(self.json.decoder.scanstring.__module__,
32+
'json.decoder')
33+
self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
34+
'json.encoder')
35+
36+
class TestCTest(CTest):
37+
def test_cjson(self):
38+
self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
39+
self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
40+
self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
41+
self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
42+
'_json')
43+
544

645
here = os.path.dirname(__file__)
746

@@ -17,12 +56,11 @@ def test_suite():
1756
return suite
1857

1958
def additional_tests():
20-
import json
21-
import json.encoder
22-
import json.decoder
2359
suite = unittest.TestSuite()
2460
for mod in (json, json.encoder, json.decoder):
2561
suite.addTest(doctest.DocTestSuite(mod))
62+
suite.addTest(TestPyTest('test_pyjson'))
63+
suite.addTest(TestCTest('test_cjson'))
2664
return suite
2765

2866
def main():

Lib/test/json_tests/test_decode.py

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,46 @@
11
import decimal
2-
from unittest import TestCase
32
from io import StringIO
4-
from contextlib import contextmanager
5-
6-
import json
7-
import json.decoder
8-
import json.scanner
93
from collections import OrderedDict
4+
from test.json_tests import PyTest, CTest
105

116

12-
@contextmanager
13-
def use_python_scanner():
14-
py_scanner = json.scanner.py_make_scanner
15-
old_scanner = json.decoder.make_scanner
16-
json.decoder.make_scanner = py_scanner
17-
try:
18-
yield
19-
finally:
20-
json.decoder.make_scanner = old_scanner
21-
22-
23-
class TestDecode(TestCase):
7+
class TestDecode:
248
def test_decimal(self):
25-
rval = json.loads('1.1', parse_float=decimal.Decimal)
9+
rval = self.loads('1.1', parse_float=decimal.Decimal)
2610
self.assertTrue(isinstance(rval, decimal.Decimal))
2711
self.assertEqual(rval, decimal.Decimal('1.1'))
2812

2913
def test_float(self):
30-
rval = json.loads('1', parse_int=float)
14+
rval = self.loads('1', parse_int=float)
3115
self.assertTrue(isinstance(rval, float))
3216
self.assertEqual(rval, 1.0)
3317

3418
def test_empty_objects(self):
35-
self.assertEqual(json.loads('{}'), {})
36-
self.assertEqual(json.loads('[]'), [])
37-
self.assertEqual(json.loads('""'), "")
19+
self.assertEqual(self.loads('{}'), {})
20+
self.assertEqual(self.loads('[]'), [])
21+
self.assertEqual(self.loads('""'), "")
3822

3923
def test_object_pairs_hook(self):
4024
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
4125
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
4226
("qrt", 5), ("pad", 6), ("hoy", 7)]
43-
self.assertEqual(json.loads(s), eval(s))
44-
self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p)
45-
self.assertEqual(json.load(StringIO(s),
46-
object_pairs_hook=lambda x: x), p)
47-
od = json.loads(s, object_pairs_hook = OrderedDict)
27+
self.assertEqual(self.loads(s), eval(s))
28+
self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p)
29+
self.assertEqual(self.json.load(StringIO(s),
30+
object_pairs_hook=lambda x: x), p)
31+
od = self.loads(s, object_pairs_hook = OrderedDict)
4832
self.assertEqual(od, OrderedDict(p))
4933
self.assertEqual(type(od), OrderedDict)
5034
# the object_pairs_hook takes priority over the object_hook
51-
self.assertEqual(json.loads(s,
52-
object_pairs_hook = OrderedDict,
35+
self.assertEqual(self.loads(s, object_pairs_hook = OrderedDict,
5336
object_hook = lambda x: None),
5437
OrderedDict(p))
5538

5639
def test_decoder_optimizations(self):
5740
# Several optimizations were made that skip over calls to
5841
# the whitespace regex, so this test is designed to try and
5942
# exercise the uncommon cases. The array cases are already covered.
60-
rval = json.loads('{ "key" : "value" , "k":"v" }')
43+
rval = self.loads('{ "key" : "value" , "k":"v" }')
6144
self.assertEqual(rval, {"key":"value", "k":"v"})
6245

6346
def check_keys_reuse(self, source, loads):
@@ -68,7 +51,9 @@ def check_keys_reuse(self, source, loads):
6851

6952
def test_keys_reuse(self):
7053
s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'
71-
self.check_keys_reuse(s, json.loads)
72-
# Disabled: the pure Python version of json simply doesn't work
73-
with use_python_scanner():
74-
self.check_keys_reuse(s, json.decoder.JSONDecoder().decode)
54+
self.check_keys_reuse(s, self.loads)
55+
self.check_keys_reuse(s, self.json.decoder.JSONDecoder().decode)
56+
57+
58+
class TestPyDecode(TestDecode, PyTest): pass
59+
class TestCDecode(TestDecode, CTest): pass
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from unittest import TestCase
1+
from test.json_tests import PyTest, CTest
22

3-
import json
43

5-
class TestDefault(TestCase):
4+
class TestDefault:
65
def test_default(self):
76
self.assertEqual(
8-
json.dumps(type, default=repr),
9-
json.dumps(repr(type)))
7+
self.dumps(type, default=repr),
8+
self.dumps(repr(type)))
9+
10+
11+
class TestPyDefault(TestDefault, PyTest): pass
12+
class TestCDefault(TestDefault, CTest): pass

Lib/test/json_tests/test_dump.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
from unittest import TestCase
21
from io import StringIO
2+
from test.json_tests import PyTest, CTest
33

4-
import json
54

6-
class TestDump(TestCase):
5+
class TestDump:
76
def test_dump(self):
87
sio = StringIO()
9-
json.dump({}, sio)
8+
self.json.dump({}, sio)
109
self.assertEqual(sio.getvalue(), '{}')
1110

1211
def test_dumps(self):
13-
self.assertEqual(json.dumps({}), '{}')
12+
self.assertEqual(self.dumps({}), '{}')
1413

1514
def test_encode_truefalse(self):
16-
self.assertEqual(json.dumps(
15+
self.assertEqual(self.dumps(
1716
{True: False, False: True}, sort_keys=True),
1817
'{"false": true, "true": false}')
19-
self.assertEqual(json.dumps(
18+
self.assertEqual(self.dumps(
2019
{2: 3.0, 4.0: 5, False: 1, 6: True}, sort_keys=True),
2120
'{"false": 1, "2": 3.0, "4.0": 5, "6": true}')
21+
22+
23+
class TestPyDump(TestDump, PyTest): pass
24+
class TestCDump(TestDump, CTest): pass
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from unittest import TestCase
2-
3-
import json.encoder
4-
from json import dumps
51
from collections import OrderedDict
2+
from test.json_tests import PyTest, CTest
3+
64

75
CASES = [
86
('/\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\x08\x0c\n\r\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?', '"/\\\\\\"\\ucafe\\ubabe\\uab98\\ufcde\\ubcda\\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"'),
@@ -21,30 +19,26 @@
2119
('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
2220
]
2321

24-
class TestEncodeBaseStringAscii(TestCase):
25-
def test_py_encode_basestring_ascii(self):
26-
self._test_encode_basestring_ascii(json.encoder.py_encode_basestring_ascii)
27-
28-
def test_c_encode_basestring_ascii(self):
29-
if not json.encoder.c_encode_basestring_ascii:
30-
return
31-
self._test_encode_basestring_ascii(json.encoder.c_encode_basestring_ascii)
32-
33-
def _test_encode_basestring_ascii(self, encode_basestring_ascii):
34-
fname = encode_basestring_ascii.__name__
22+
class TestEncodeBasestringAscii:
23+
def test_encode_basestring_ascii(self):
24+
fname = self.json.encoder.encode_basestring_ascii.__name__
3525
for input_string, expect in CASES:
36-
result = encode_basestring_ascii(input_string)
26+
result = self.json.encoder.encode_basestring_ascii(input_string)
3727
self.assertEqual(result, expect,
3828
'{0!r} != {1!r} for {2}({3!r})'.format(
3929
result, expect, fname, input_string))
4030

4131
def test_ordered_dict(self):
4232
# See issue 6105
4333
items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
44-
s = json.dumps(OrderedDict(items))
34+
s = self.dumps(OrderedDict(items))
4535
self.assertEqual(s, '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}')
4636

4737
def test_sorted_dict(self):
4838
items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
49-
s = json.dumps(dict(items), sort_keys=True)
39+
s = self.dumps(dict(items), sort_keys=True)
5040
self.assertEqual(s, '{"five": 5, "four": 4, "one": 1, "three": 3, "two": 2}')
41+
42+
43+
class TestPyEncodeBasestringAscii(TestEncodeBasestringAscii, PyTest): pass
44+
class TestCEncodeBasestringAscii(TestEncodeBasestringAscii, CTest): pass

Lib/test/json_tests/test_fail.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from unittest import TestCase
2-
3-
import json
1+
from test.json_tests import PyTest, CTest
42

53
# Fri Dec 30 18:57:26 2005
64
JSONDOCS = [
@@ -61,15 +59,15 @@
6159
18: "spec doesn't specify any nesting limitations",
6260
}
6361

64-
class TestFail(TestCase):
62+
class TestFail:
6563
def test_failures(self):
6664
for idx, doc in enumerate(JSONDOCS):
6765
idx = idx + 1
6866
if idx in SKIPS:
69-
json.loads(doc)
67+
self.loads(doc)
7068
continue
7169
try:
72-
json.loads(doc)
70+
self.loads(doc)
7371
except ValueError:
7472
pass
7573
else:
@@ -79,7 +77,11 @@ def test_non_string_keys_dict(self):
7977
data = {'a' : 1, (1, 2) : 2}
8078

8179
#This is for c encoder
82-
self.assertRaises(TypeError, json.dumps, data)
80+
self.assertRaises(TypeError, self.dumps, data)
8381

8482
#This is for python encoder
85-
self.assertRaises(TypeError, json.dumps, data, indent=True)
83+
self.assertRaises(TypeError, self.dumps, data, indent=True)
84+
85+
86+
class TestPyFail(TestFail, PyTest): pass
87+
class TestCFail(TestFail, CTest): pass

Lib/test/json_tests/test_float.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import math
2-
from unittest import TestCase
2+
from test.json_tests import PyTest, CTest
33

4-
import json
54

6-
class TestFloat(TestCase):
5+
class TestFloat:
76
def test_floats(self):
87
for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100, 3.1]:
9-
self.assertEqual(float(json.dumps(num)), num)
10-
self.assertEqual(json.loads(json.dumps(num)), num)
8+
self.assertEqual(float(self.dumps(num)), num)
9+
self.assertEqual(self.loads(self.dumps(num)), num)
1110

1211
def test_ints(self):
1312
for num in [1, 1<<32, 1<<64]:
14-
self.assertEqual(json.dumps(num), str(num))
15-
self.assertEqual(int(json.dumps(num)), num)
13+
self.assertEqual(self.dumps(num), str(num))
14+
self.assertEqual(int(self.dumps(num)), num)
15+
16+
17+
class TestPyFloat(TestFloat, PyTest): pass
18+
class TestCFloat(TestFloat, CTest): pass

Lib/test/json_tests/test_indent.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from unittest import TestCase
2-
3-
import json
41
import textwrap
52
from io import StringIO
3+
from test.json_tests import PyTest, CTest
4+
65

7-
class TestIndent(TestCase):
6+
class TestIndent:
87
def test_indent(self):
98
h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
109
{'nifty': 87}, {'field': 'yes', 'morefield': False} ]
@@ -30,14 +29,13 @@ def test_indent(self):
3029
\t}
3130
]""")
3231

32+
d1 = self.dumps(h)
33+
d2 = self.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
34+
d3 = self.dumps(h, indent='\t', sort_keys=True, separators=(',', ': '))
3335

34-
d1 = json.dumps(h)
35-
d2 = json.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
36-
d3 = json.dumps(h, indent='\t', sort_keys=True, separators=(',', ': '))
37-
38-
h1 = json.loads(d1)
39-
h2 = json.loads(d2)
40-
h3 = json.loads(d3)
36+
h1 = self.loads(d1)
37+
h2 = self.loads(d2)
38+
h3 = self.loads(d3)
4139

4240
self.assertEqual(h1, h)
4341
self.assertEqual(h2, h)
@@ -48,14 +46,18 @@ def test_indent(self):
4846
def test_indent0(self):
4947
h = {3: 1}
5048
def check(indent, expected):
51-
d1 = json.dumps(h, indent=indent)
49+
d1 = self.dumps(h, indent=indent)
5250
self.assertEqual(d1, expected)
5351

5452
sio = StringIO()
55-
json.dump(h, sio, indent=indent)
53+
self.json.dump(h, sio, indent=indent)
5654
self.assertEqual(sio.getvalue(), expected)
5755

5856
# indent=0 should emit newlines
5957
check(0, '{\n"3": 1\n}')
6058
# indent=None is more compact
6159
check(None, '{"3": 1}')
60+
61+
62+
class TestPyIndent(TestIndent, PyTest): pass
63+
class TestCIndent(TestIndent, CTest): pass

0 commit comments

Comments
 (0)