AppPkg/Applications/Python: Add Python 2.7.2 sources since the release of Python 2.7.3 made them unavailable from the python.org web site.
These files are a subset of the python-2.7.2.tgz distribution from python.org. Changed files from PyMod-2.7.2 have been copied into the corresponding directories of this tree, replacing the original files in the distribution. Signed-off-by: daryl.mcdaniel@intel.com git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13197 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
@ -0,0 +1,73 @@
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import doctest
|
||||
import unittest
|
||||
|
||||
from test import test_support
|
||||
|
||||
# import json with and without accelerations
|
||||
cjson = test_support.import_fresh_module('json', fresh=['_json'])
|
||||
pyjson = test_support.import_fresh_module('json', blocked=['_json'])
|
||||
|
||||
# create two base classes that will be used by the other tests
|
||||
class PyTest(unittest.TestCase):
|
||||
json = pyjson
|
||||
loads = staticmethod(pyjson.loads)
|
||||
dumps = staticmethod(pyjson.dumps)
|
||||
|
||||
@unittest.skipUnless(cjson, 'requires _json')
|
||||
class CTest(unittest.TestCase):
|
||||
if cjson is not None:
|
||||
json = cjson
|
||||
loads = staticmethod(cjson.loads)
|
||||
dumps = staticmethod(cjson.dumps)
|
||||
|
||||
# test PyTest and CTest checking if the functions come from the right module
|
||||
class TestPyTest(PyTest):
|
||||
def test_pyjson(self):
|
||||
self.assertEqual(self.json.scanner.make_scanner.__module__,
|
||||
'json.scanner')
|
||||
self.assertEqual(self.json.decoder.scanstring.__module__,
|
||||
'json.decoder')
|
||||
self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
|
||||
'json.encoder')
|
||||
|
||||
class TestCTest(CTest):
|
||||
def test_cjson(self):
|
||||
self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
|
||||
self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
|
||||
self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
|
||||
self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
|
||||
'_json')
|
||||
|
||||
|
||||
here = os.path.dirname(__file__)
|
||||
|
||||
def test_suite():
|
||||
suite = additional_tests()
|
||||
loader = unittest.TestLoader()
|
||||
for fn in os.listdir(here):
|
||||
if fn.startswith("test") and fn.endswith(".py"):
|
||||
modname = "json.tests." + fn[:-3]
|
||||
__import__(modname)
|
||||
module = sys.modules[modname]
|
||||
suite.addTests(loader.loadTestsFromModule(module))
|
||||
return suite
|
||||
|
||||
def additional_tests():
|
||||
suite = unittest.TestSuite()
|
||||
for mod in (json, json.encoder, json.decoder):
|
||||
suite.addTest(doctest.DocTestSuite(mod))
|
||||
suite.addTest(TestPyTest('test_pyjson'))
|
||||
suite.addTest(TestCTest('test_cjson'))
|
||||
return suite
|
||||
|
||||
def main():
|
||||
suite = test_suite()
|
||||
runner = unittest.TextTestRunner()
|
||||
runner.run(suite)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
main()
|
@ -0,0 +1,34 @@
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
def default_iterable(obj):
|
||||
return list(obj)
|
||||
|
||||
class TestCheckCircular(object):
|
||||
def test_circular_dict(self):
|
||||
dct = {}
|
||||
dct['a'] = dct
|
||||
self.assertRaises(ValueError, self.dumps, dct)
|
||||
|
||||
def test_circular_list(self):
|
||||
lst = []
|
||||
lst.append(lst)
|
||||
self.assertRaises(ValueError, self.dumps, lst)
|
||||
|
||||
def test_circular_composite(self):
|
||||
dct2 = {}
|
||||
dct2['a'] = []
|
||||
dct2['a'].append(dct2)
|
||||
self.assertRaises(ValueError, self.dumps, dct2)
|
||||
|
||||
def test_circular_default(self):
|
||||
self.dumps([set()], default=default_iterable)
|
||||
self.assertRaises(TypeError, self.dumps, [set()])
|
||||
|
||||
def test_circular_off_default(self):
|
||||
self.dumps([set()], default=default_iterable, check_circular=False)
|
||||
self.assertRaises(TypeError, self.dumps, [set()], check_circular=False)
|
||||
|
||||
|
||||
class TestPyCheckCircular(TestCheckCircular, PyTest): pass
|
||||
class TestCCheckCircular(TestCheckCircular, CTest): pass
|
@ -0,0 +1,50 @@
|
||||
import decimal
|
||||
from StringIO import StringIO
|
||||
from collections import OrderedDict
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class TestDecode(object):
|
||||
def test_decimal(self):
|
||||
rval = self.loads('1.1', parse_float=decimal.Decimal)
|
||||
self.assertTrue(isinstance(rval, decimal.Decimal))
|
||||
self.assertEqual(rval, decimal.Decimal('1.1'))
|
||||
|
||||
def test_float(self):
|
||||
rval = self.loads('1', parse_int=float)
|
||||
self.assertTrue(isinstance(rval, float))
|
||||
self.assertEqual(rval, 1.0)
|
||||
|
||||
def test_decoder_optimizations(self):
|
||||
# Several optimizations were made that skip over calls to
|
||||
# the whitespace regex, so this test is designed to try and
|
||||
# exercise the uncommon cases. The array cases are already covered.
|
||||
rval = self.loads('{ "key" : "value" , "k":"v" }')
|
||||
self.assertEqual(rval, {"key":"value", "k":"v"})
|
||||
|
||||
def test_empty_objects(self):
|
||||
self.assertEqual(self.loads('{}'), {})
|
||||
self.assertEqual(self.loads('[]'), [])
|
||||
self.assertEqual(self.loads('""'), u"")
|
||||
self.assertIsInstance(self.loads('""'), unicode)
|
||||
|
||||
def test_object_pairs_hook(self):
|
||||
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
|
||||
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
|
||||
("qrt", 5), ("pad", 6), ("hoy", 7)]
|
||||
self.assertEqual(self.loads(s), eval(s))
|
||||
self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p)
|
||||
self.assertEqual(self.json.load(StringIO(s),
|
||||
object_pairs_hook=lambda x: x), p)
|
||||
od = self.loads(s, object_pairs_hook=OrderedDict)
|
||||
self.assertEqual(od, OrderedDict(p))
|
||||
self.assertEqual(type(od), OrderedDict)
|
||||
# the object_pairs_hook takes priority over the object_hook
|
||||
self.assertEqual(self.loads(s,
|
||||
object_pairs_hook=OrderedDict,
|
||||
object_hook=lambda x: None),
|
||||
OrderedDict(p))
|
||||
|
||||
|
||||
class TestPyDecode(TestDecode, PyTest): pass
|
||||
class TestCDecode(TestDecode, CTest): pass
|
@ -0,0 +1,12 @@
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class TestDefault(object):
|
||||
def test_default(self):
|
||||
self.assertEqual(
|
||||
self.dumps(type, default=repr),
|
||||
self.dumps(repr(type)))
|
||||
|
||||
|
||||
class TestPyDefault(TestDefault, PyTest): pass
|
||||
class TestCDefault(TestDefault, CTest): pass
|
@ -0,0 +1,23 @@
|
||||
from cStringIO import StringIO
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class TestDump(object):
|
||||
def test_dump(self):
|
||||
sio = StringIO()
|
||||
self.json.dump({}, sio)
|
||||
self.assertEqual(sio.getvalue(), '{}')
|
||||
|
||||
def test_dumps(self):
|
||||
self.assertEqual(self.dumps({}), '{}')
|
||||
|
||||
def test_encode_truefalse(self):
|
||||
self.assertEqual(self.dumps(
|
||||
{True: False, False: True}, sort_keys=True),
|
||||
'{"false": true, "true": false}')
|
||||
self.assertEqual(self.dumps(
|
||||
{2: 3.0, 4.0: 5L, False: 1, 6L: True}, sort_keys=True),
|
||||
'{"false": 1, "2": 3.0, "4.0": 5, "6": true}')
|
||||
|
||||
class TestPyDump(TestDump, PyTest): pass
|
||||
class TestCDump(TestDump, CTest): pass
|
@ -0,0 +1,41 @@
|
||||
from collections import OrderedDict
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
CASES = [
|
||||
(u'/\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\x08\x0c\n\r\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?', '"/\\\\\\"\\ucafe\\ubabe\\uab98\\ufcde\\ubcda\\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"'),
|
||||
(u'\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
|
||||
(u'controls', '"controls"'),
|
||||
(u'\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'),
|
||||
(u'{"object with 1 member":["array with 1 element"]}', '"{\\"object with 1 member\\":[\\"array with 1 element\\"]}"'),
|
||||
(u' s p a c e d ', '" s p a c e d "'),
|
||||
(u'\U0001d120', '"\\ud834\\udd20"'),
|
||||
(u'\u03b1\u03a9', '"\\u03b1\\u03a9"'),
|
||||
('\xce\xb1\xce\xa9', '"\\u03b1\\u03a9"'),
|
||||
(u'\u03b1\u03a9', '"\\u03b1\\u03a9"'),
|
||||
('\xce\xb1\xce\xa9', '"\\u03b1\\u03a9"'),
|
||||
(u'\u03b1\u03a9', '"\\u03b1\\u03a9"'),
|
||||
(u'\u03b1\u03a9', '"\\u03b1\\u03a9"'),
|
||||
(u"`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'),
|
||||
(u'\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'),
|
||||
(u'\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
|
||||
]
|
||||
|
||||
class TestEncodeBasestringAscii(object):
|
||||
def test_encode_basestring_ascii(self):
|
||||
fname = self.json.encoder.encode_basestring_ascii.__name__
|
||||
for input_string, expect in CASES:
|
||||
result = self.json.encoder.encode_basestring_ascii(input_string)
|
||||
self.assertEqual(result, expect,
|
||||
'{0!r} != {1!r} for {2}({3!r})'.format(
|
||||
result, expect, fname, input_string))
|
||||
|
||||
def test_ordered_dict(self):
|
||||
# See issue 6105
|
||||
items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
|
||||
s = self.dumps(OrderedDict(items))
|
||||
self.assertEqual(s, '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}')
|
||||
|
||||
|
||||
class TestPyEncodeBasestringAscii(TestEncodeBasestringAscii, PyTest): pass
|
||||
class TestCEncodeBasestringAscii(TestEncodeBasestringAscii, CTest): pass
|
@ -0,0 +1,87 @@
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
# Fri Dec 30 18:57:26 2005
|
||||
JSONDOCS = [
|
||||
# http://json.org/JSON_checker/test/fail1.json
|
||||
'"A JSON payload should be an object or array, not a string."',
|
||||
# http://json.org/JSON_checker/test/fail2.json
|
||||
'["Unclosed array"',
|
||||
# http://json.org/JSON_checker/test/fail3.json
|
||||
'{unquoted_key: "keys must be quoted}',
|
||||
# http://json.org/JSON_checker/test/fail4.json
|
||||
'["extra comma",]',
|
||||
# http://json.org/JSON_checker/test/fail5.json
|
||||
'["double extra comma",,]',
|
||||
# http://json.org/JSON_checker/test/fail6.json
|
||||
'[ , "<-- missing value"]',
|
||||
# http://json.org/JSON_checker/test/fail7.json
|
||||
'["Comma after the close"],',
|
||||
# http://json.org/JSON_checker/test/fail8.json
|
||||
'["Extra close"]]',
|
||||
# http://json.org/JSON_checker/test/fail9.json
|
||||
'{"Extra comma": true,}',
|
||||
# http://json.org/JSON_checker/test/fail10.json
|
||||
'{"Extra value after close": true} "misplaced quoted value"',
|
||||
# http://json.org/JSON_checker/test/fail11.json
|
||||
'{"Illegal expression": 1 + 2}',
|
||||
# http://json.org/JSON_checker/test/fail12.json
|
||||
'{"Illegal invocation": alert()}',
|
||||
# http://json.org/JSON_checker/test/fail13.json
|
||||
'{"Numbers cannot have leading zeroes": 013}',
|
||||
# http://json.org/JSON_checker/test/fail14.json
|
||||
'{"Numbers cannot be hex": 0x14}',
|
||||
# http://json.org/JSON_checker/test/fail15.json
|
||||
'["Illegal backslash escape: \\x15"]',
|
||||
# http://json.org/JSON_checker/test/fail16.json
|
||||
'["Illegal backslash escape: \\\'"]',
|
||||
# http://json.org/JSON_checker/test/fail17.json
|
||||
'["Illegal backslash escape: \\017"]',
|
||||
# http://json.org/JSON_checker/test/fail18.json
|
||||
'[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]',
|
||||
# http://json.org/JSON_checker/test/fail19.json
|
||||
'{"Missing colon" null}',
|
||||
# http://json.org/JSON_checker/test/fail20.json
|
||||
'{"Double colon":: null}',
|
||||
# http://json.org/JSON_checker/test/fail21.json
|
||||
'{"Comma instead of colon", null}',
|
||||
# http://json.org/JSON_checker/test/fail22.json
|
||||
'["Colon instead of comma": false]',
|
||||
# http://json.org/JSON_checker/test/fail23.json
|
||||
'["Bad value", truth]',
|
||||
# http://json.org/JSON_checker/test/fail24.json
|
||||
"['single quote']",
|
||||
# http://code.google.com/p/simplejson/issues/detail?id=3
|
||||
u'["A\u001FZ control characters in string"]',
|
||||
]
|
||||
|
||||
SKIPS = {
|
||||
1: "why not have a string payload?",
|
||||
18: "spec doesn't specify any nesting limitations",
|
||||
}
|
||||
|
||||
class TestFail(object):
|
||||
def test_failures(self):
|
||||
for idx, doc in enumerate(JSONDOCS):
|
||||
idx = idx + 1
|
||||
if idx in SKIPS:
|
||||
self.loads(doc)
|
||||
continue
|
||||
try:
|
||||
self.loads(doc)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
|
||||
|
||||
def test_non_string_keys_dict(self):
|
||||
data = {'a' : 1, (1, 2) : 2}
|
||||
|
||||
#This is for c encoder
|
||||
self.assertRaises(TypeError, self.dumps, data)
|
||||
|
||||
#This is for python encoder
|
||||
self.assertRaises(TypeError, self.dumps, data, indent=True)
|
||||
|
||||
|
||||
class TestPyFail(TestFail, PyTest): pass
|
||||
class TestCFail(TestFail, CTest): pass
|
@ -0,0 +1,22 @@
|
||||
import math
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class TestFloat(object):
|
||||
def test_floats(self):
|
||||
for num in [1617161771.7650001, math.pi, math.pi**100,
|
||||
math.pi**-100, 3.1]:
|
||||
self.assertEqual(float(self.dumps(num)), num)
|
||||
self.assertEqual(self.loads(self.dumps(num)), num)
|
||||
self.assertEqual(self.loads(unicode(self.dumps(num))), num)
|
||||
|
||||
def test_ints(self):
|
||||
for num in [1, 1L, 1<<32, 1<<64]:
|
||||
self.assertEqual(self.dumps(num), str(num))
|
||||
self.assertEqual(int(self.dumps(num)), num)
|
||||
self.assertEqual(self.loads(self.dumps(num)), num)
|
||||
self.assertEqual(self.loads(unicode(self.dumps(num))), num)
|
||||
|
||||
|
||||
class TestPyFloat(TestFloat, PyTest): pass
|
||||
class TestCFloat(TestFloat, CTest): pass
|
@ -0,0 +1,60 @@
|
||||
import textwrap
|
||||
from StringIO import StringIO
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class TestIndent(object):
|
||||
def test_indent(self):
|
||||
h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
|
||||
{'nifty': 87}, {'field': 'yes', 'morefield': False} ]
|
||||
|
||||
expect = textwrap.dedent("""\
|
||||
[
|
||||
[
|
||||
"blorpie"
|
||||
],
|
||||
[
|
||||
"whoops"
|
||||
],
|
||||
[],
|
||||
"d-shtaeou",
|
||||
"d-nthiouh",
|
||||
"i-vhbjkhnth",
|
||||
{
|
||||
"nifty": 87
|
||||
},
|
||||
{
|
||||
"field": "yes",
|
||||
"morefield": false
|
||||
}
|
||||
]""")
|
||||
|
||||
|
||||
d1 = self.dumps(h)
|
||||
d2 = self.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
|
||||
|
||||
h1 = self.loads(d1)
|
||||
h2 = self.loads(d2)
|
||||
|
||||
self.assertEqual(h1, h)
|
||||
self.assertEqual(h2, h)
|
||||
self.assertEqual(d2, expect)
|
||||
|
||||
def test_indent0(self):
|
||||
h = {3: 1}
|
||||
def check(indent, expected):
|
||||
d1 = self.dumps(h, indent=indent)
|
||||
self.assertEqual(d1, expected)
|
||||
|
||||
sio = StringIO()
|
||||
self.json.dump(h, sio, indent=indent)
|
||||
self.assertEqual(sio.getvalue(), expected)
|
||||
|
||||
# indent=0 should emit newlines
|
||||
check(0, '{\n"3": 1\n}')
|
||||
# indent=None is more compact
|
||||
check(None, '{"3": 1}')
|
||||
|
||||
|
||||
class TestPyIndent(TestIndent, PyTest): pass
|
||||
class TestCIndent(TestIndent, CTest): pass
|
@ -0,0 +1,79 @@
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
# from http://json.org/JSON_checker/test/pass1.json
|
||||
JSON = r'''
|
||||
[
|
||||
"JSON Test Pattern pass1",
|
||||
{"object with 1 member":["array with 1 element"]},
|
||||
{},
|
||||
[],
|
||||
-42,
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
{
|
||||
"integer": 1234567890,
|
||||
"real": -9876.543210,
|
||||
"e": 0.123456789e-12,
|
||||
"E": 1.234567890E+34,
|
||||
"": 23456789012E666,
|
||||
"zero": 0,
|
||||
"one": 1,
|
||||
"space": " ",
|
||||
"quote": "\"",
|
||||
"backslash": "\\",
|
||||
"controls": "\b\f\n\r\t",
|
||||
"slash": "/ & \/",
|
||||
"alpha": "abcdefghijklmnopqrstuvwyz",
|
||||
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
|
||||
"digit": "0123456789",
|
||||
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
|
||||
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
|
||||
"true": true,
|
||||
"false": false,
|
||||
"null": null,
|
||||
"array":[ ],
|
||||
"object":{ },
|
||||
"address": "50 St. James Street",
|
||||
"url": "http://www.JSON.org/",
|
||||
"comment": "// /* <!-- --",
|
||||
"# -- --> */": " ",
|
||||
" s p a c e d " :[1,2 , 3
|
||||
|
||||
,
|
||||
|
||||
4 , 5 , 6 ,7 ],
|
||||
"compact": [1,2,3,4,5,6,7],
|
||||
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
|
||||
"quotes": "" \u0022 %22 0x22 034 "",
|
||||
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
|
||||
: "A key can be any string"
|
||||
},
|
||||
0.5 ,98.6
|
||||
,
|
||||
99.44
|
||||
,
|
||||
|
||||
1066
|
||||
|
||||
|
||||
,"rosebud"]
|
||||
'''
|
||||
|
||||
class TestPass1(object):
|
||||
def test_parse(self):
|
||||
# test in/out equivalence and parsing
|
||||
res = self.loads(JSON)
|
||||
out = self.dumps(res)
|
||||
self.assertEqual(res, self.loads(out))
|
||||
try:
|
||||
self.dumps(res, allow_nan=False)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("23456789012E666 should be out of range")
|
||||
|
||||
|
||||
class TestPyPass1(TestPass1, PyTest): pass
|
||||
class TestCPass1(TestPass1, CTest): pass
|
@ -0,0 +1,18 @@
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
# from http://json.org/JSON_checker/test/pass2.json
|
||||
JSON = r'''
|
||||
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
||||
'''
|
||||
|
||||
class TestPass2(object):
|
||||
def test_parse(self):
|
||||
# test in/out equivalence and parsing
|
||||
res = self.loads(JSON)
|
||||
out = self.dumps(res)
|
||||
self.assertEqual(res, self.loads(out))
|
||||
|
||||
|
||||
class TestPyPass2(TestPass2, PyTest): pass
|
||||
class TestCPass2(TestPass2, CTest): pass
|
@ -0,0 +1,24 @@
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
# from http://json.org/JSON_checker/test/pass3.json
|
||||
JSON = r'''
|
||||
{
|
||||
"JSON Test Pattern pass3": {
|
||||
"The outermost value": "must be an object or array.",
|
||||
"In this test": "It is an object."
|
||||
}
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class TestPass3(object):
|
||||
def test_parse(self):
|
||||
# test in/out equivalence and parsing
|
||||
res = self.loads(JSON)
|
||||
out = self.dumps(res)
|
||||
self.assertEqual(res, self.loads(out))
|
||||
|
||||
|
||||
class TestPyPass3(TestPass3, PyTest): pass
|
||||
class TestCPass3(TestPass3, CTest): pass
|
@ -0,0 +1,108 @@
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class JSONTestObject:
|
||||
pass
|
||||
|
||||
|
||||
class TestRecursion(object):
|
||||
def test_listrecursion(self):
|
||||
x = []
|
||||
x.append(x)
|
||||
try:
|
||||
self.dumps(x)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("didn't raise ValueError on list recursion")
|
||||
x = []
|
||||
y = [x]
|
||||
x.append(y)
|
||||
try:
|
||||
self.dumps(x)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("didn't raise ValueError on alternating list recursion")
|
||||
y = []
|
||||
x = [y, y]
|
||||
# ensure that the marker is cleared
|
||||
self.dumps(x)
|
||||
|
||||
def test_dictrecursion(self):
|
||||
x = {}
|
||||
x["test"] = x
|
||||
try:
|
||||
self.dumps(x)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("didn't raise ValueError on dict recursion")
|
||||
x = {}
|
||||
y = {"a": x, "b": x}
|
||||
# ensure that the marker is cleared
|
||||
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
|
||||
try:
|
||||
enc.encode(JSONTestObject)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
self.fail("didn't raise ValueError on default recursion")
|
||||
|
||||
|
||||
def test_highly_nested_objects_decoding(self):
|
||||
# test that loading highly-nested objects doesn't segfault when C
|
||||
# accelerations are used. See #12017
|
||||
# str
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.loads('{"a":' * 100000 + '1' + '}' * 100000)
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.loads('{"a":' * 100000 + '[1]' + '}' * 100000)
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.loads('[' * 100000 + '1' + ']' * 100000)
|
||||
# unicode
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.loads(u'{"a":' * 100000 + u'1' + u'}' * 100000)
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.loads(u'{"a":' * 100000 + u'[1]' + u'}' * 100000)
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.loads(u'[' * 100000 + u'1' + u']' * 100000)
|
||||
|
||||
def test_highly_nested_objects_encoding(self):
|
||||
# See #12051
|
||||
l, d = [], {}
|
||||
for x in xrange(100000):
|
||||
l, d = [l], {'k':d}
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.dumps(l)
|
||||
with self.assertRaises(RuntimeError):
|
||||
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
|
@ -0,0 +1,109 @@
|
||||
import sys
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class TestScanstring(object):
|
||||
def test_scanstring(self):
|
||||
scanstring = self.json.decoder.scanstring
|
||||
self.assertEqual(
|
||||
scanstring('"z\\ud834\\udd20x"', 1, None, True),
|
||||
(u'z\U0001d120x', 16))
|
||||
|
||||
if sys.maxunicode == 65535:
|
||||
self.assertEqual(
|
||||
scanstring(u'"z\U0001d120x"', 1, None, True),
|
||||
(u'z\U0001d120x', 6))
|
||||
else:
|
||||
self.assertEqual(
|
||||
scanstring(u'"z\U0001d120x"', 1, None, True),
|
||||
(u'z\U0001d120x', 5))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('"\\u007b"', 1, None, True),
|
||||
(u'{', 8))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('"A JSON payload should be an object or array, not a string."', 1, None, True),
|
||||
(u'A JSON payload should be an object or array, not a string.', 60))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('["Unclosed array"', 2, None, True),
|
||||
(u'Unclosed array', 17))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('["extra comma",]', 2, None, True),
|
||||
(u'extra comma', 14))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('["double extra comma",,]', 2, None, True),
|
||||
(u'double extra comma', 21))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('["Comma after the close"],', 2, None, True),
|
||||
(u'Comma after the close', 24))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('["Extra close"]]', 2, None, True),
|
||||
(u'Extra close', 14))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Extra comma": true,}', 2, None, True),
|
||||
(u'Extra comma', 14))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Extra value after close": true} "misplaced quoted value"', 2, None, True),
|
||||
(u'Extra value after close', 26))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Illegal expression": 1 + 2}', 2, None, True),
|
||||
(u'Illegal expression', 21))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Illegal invocation": alert()}', 2, None, True),
|
||||
(u'Illegal invocation', 21))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Numbers cannot have leading zeroes": 013}', 2, None, True),
|
||||
(u'Numbers cannot have leading zeroes', 37))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Numbers cannot be hex": 0x14}', 2, None, True),
|
||||
(u'Numbers cannot be hex', 24))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', 21, None, True),
|
||||
(u'Too deep', 30))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Missing colon" null}', 2, None, True),
|
||||
(u'Missing colon', 16))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Double colon":: null}', 2, None, True),
|
||||
(u'Double colon', 15))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('{"Comma instead of colon", null}', 2, None, True),
|
||||
(u'Comma instead of colon', 25))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('["Colon instead of comma": false]', 2, None, True),
|
||||
(u'Colon instead of comma', 25))
|
||||
|
||||
self.assertEqual(
|
||||
scanstring('["Bad value", truth]', 2, None, True),
|
||||
(u'Bad value', 12))
|
||||
|
||||
def test_issue3623(self):
|
||||
self.assertRaises(ValueError, self.json.decoder.scanstring, b"xxx", 1,
|
||||
"xxx")
|
||||
self.assertRaises(UnicodeDecodeError,
|
||||
self.json.encoder.encode_basestring_ascii, b"xx\xff")
|
||||
|
||||
def test_overflow(self):
|
||||
with self.assertRaises(OverflowError):
|
||||
self.json.decoder.scanstring(b"xxx", sys.maxsize+1)
|
||||
|
||||
|
||||
class TestPyScanstring(TestScanstring, PyTest): pass
|
||||
class TestCScanstring(TestScanstring, CTest): pass
|
@ -0,0 +1,44 @@
|
||||
import textwrap
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class TestSeparators(object):
|
||||
def test_separators(self):
|
||||
h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
|
||||
{'nifty': 87}, {'field': 'yes', 'morefield': False} ]
|
||||
|
||||
expect = textwrap.dedent("""\
|
||||
[
|
||||
[
|
||||
"blorpie"
|
||||
] ,
|
||||
[
|
||||
"whoops"
|
||||
] ,
|
||||
[] ,
|
||||
"d-shtaeou" ,
|
||||
"d-nthiouh" ,
|
||||
"i-vhbjkhnth" ,
|
||||
{
|
||||
"nifty" : 87
|
||||
} ,
|
||||
{
|
||||
"field" : "yes" ,
|
||||
"morefield" : false
|
||||
}
|
||||
]""")
|
||||
|
||||
|
||||
d1 = self.dumps(h)
|
||||
d2 = self.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : '))
|
||||
|
||||
h1 = self.loads(d1)
|
||||
h2 = self.loads(d2)
|
||||
|
||||
self.assertEqual(h1, h)
|
||||
self.assertEqual(h2, h)
|
||||
self.assertEqual(d2, expect)
|
||||
|
||||
|
||||
class TestPySeparators(TestSeparators, PyTest): pass
|
||||
class TestCSeparators(TestSeparators, CTest): pass
|
@ -0,0 +1,23 @@
|
||||
from json.tests import CTest
|
||||
|
||||
|
||||
class TestSpeedups(CTest):
|
||||
def test_scanstring(self):
|
||||
self.assertEqual(self.json.decoder.scanstring.__module__, "_json")
|
||||
self.assertIs(self.json.decoder.scanstring, self.json.decoder.c_scanstring)
|
||||
|
||||
def test_encode_basestring_ascii(self):
|
||||
self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
|
||||
"_json")
|
||||
self.assertIs(self.json.encoder.encode_basestring_ascii,
|
||||
self.json.encoder.c_encode_basestring_ascii)
|
||||
|
||||
class TestDecode(CTest):
|
||||
def test_make_scanner(self):
|
||||
self.assertRaises(AttributeError, self.json.scanner.c_make_scanner, 1)
|
||||
|
||||
def test_make_encoder(self):
|
||||
self.assertRaises(TypeError, self.json.encoder.c_make_encoder,
|
||||
None,
|
||||
"\xCD\x7D\x3D\x4E\x12\x4C\xF9\x79\xD7\x52\xBA\x82\xF2\x27\x4A\x7D\xA0\xCA\x75",
|
||||
None)
|
@ -0,0 +1,85 @@
|
||||
from collections import OrderedDict
|
||||
from json.tests import PyTest, CTest
|
||||
|
||||
|
||||
class TestUnicode(object):
|
||||
def test_encoding1(self):
|
||||
encoder = self.json.JSONEncoder(encoding='utf-8')
|
||||
u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
|
||||
s = u.encode('utf-8')
|
||||
ju = encoder.encode(u)
|
||||
js = encoder.encode(s)
|
||||
self.assertEqual(ju, js)
|
||||
|
||||
def test_encoding2(self):
|
||||
u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
|
||||
s = u.encode('utf-8')
|
||||
ju = self.dumps(u, encoding='utf-8')
|
||||
js = self.dumps(s, encoding='utf-8')
|
||||
self.assertEqual(ju, js)
|
||||
|
||||
def test_encoding3(self):
|
||||
u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
|
||||
j = self.dumps(u)
|
||||
self.assertEqual(j, '"\\u03b1\\u03a9"')
|
||||
|
||||
def test_encoding4(self):
|
||||
u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
|
||||
j = self.dumps([u])
|
||||
self.assertEqual(j, '["\\u03b1\\u03a9"]')
|
||||
|
||||
def test_encoding5(self):
|
||||
u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
|
||||
j = self.dumps(u, ensure_ascii=False)
|
||||
self.assertEqual(j, u'"{0}"'.format(u))
|
||||
|
||||
def test_encoding6(self):
|
||||
u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
|
||||
j = self.dumps([u], ensure_ascii=False)
|
||||
self.assertEqual(j, u'["{0}"]'.format(u))
|
||||
|
||||
def test_big_unicode_encode(self):
|
||||
u = u'\U0001d120'
|
||||
self.assertEqual(self.dumps(u), '"\\ud834\\udd20"')
|
||||
self.assertEqual(self.dumps(u, ensure_ascii=False), u'"\U0001d120"')
|
||||
|
||||
def test_big_unicode_decode(self):
|
||||
u = u'z\U0001d120x'
|
||||
self.assertEqual(self.loads('"' + u + '"'), u)
|
||||
self.assertEqual(self.loads('"z\\ud834\\udd20x"'), u)
|
||||
|
||||
def test_unicode_decode(self):
|
||||
for i in range(0, 0xd7ff):
|
||||
u = unichr(i)
|
||||
s = '"\\u{0:04x}"'.format(i)
|
||||
self.assertEqual(self.loads(s), u)
|
||||
|
||||
def test_object_pairs_hook_with_unicode(self):
|
||||
s = u'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
|
||||
p = [(u"xkd", 1), (u"kcw", 2), (u"art", 3), (u"hxm", 4),
|
||||
(u"qrt", 5), (u"pad", 6), (u"hoy", 7)]
|
||||
self.assertEqual(self.loads(s), eval(s))
|
||||
self.assertEqual(self.loads(s, object_pairs_hook = lambda x: x), p)
|
||||
od = self.loads(s, object_pairs_hook = OrderedDict)
|
||||
self.assertEqual(od, OrderedDict(p))
|
||||
self.assertEqual(type(od), OrderedDict)
|
||||
# the object_pairs_hook takes priority over the object_hook
|
||||
self.assertEqual(self.loads(s,
|
||||
object_pairs_hook = OrderedDict,
|
||||
object_hook = lambda x: None),
|
||||
OrderedDict(p))
|
||||
|
||||
def test_default_encoding(self):
|
||||
self.assertEqual(self.loads(u'{"a": "\xe9"}'.encode('utf-8')),
|
||||
{'a': u'\xe9'})
|
||||
|
||||
def test_unicode_preservation(self):
|
||||
self.assertEqual(type(self.loads(u'""')), unicode)
|
||||
self.assertEqual(type(self.loads(u'"a"')), unicode)
|
||||
self.assertEqual(type(self.loads(u'["a"]')[0]), unicode)
|
||||
# Issue 10038.
|
||||
self.assertEqual(type(self.loads('"foo"')), unicode)
|
||||
|
||||
|
||||
class TestPyUnicode(TestUnicode, PyTest): pass
|
||||
class TestCUnicode(TestUnicode, CTest): pass
|
Reference in New Issue
Block a user