mirror of
https://github.com/python/cpython.git
synced 2025-12-05 00:52:25 +00:00
Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
This commit is contained in:
parent
8e1a338129
commit
76a982a027
5 changed files with 17 additions and 8 deletions
|
|
@ -266,7 +266,8 @@ def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
|
||||||
return loads(fp.read(),
|
return loads(fp.read(),
|
||||||
encoding=encoding, cls=cls, object_hook=object_hook,
|
encoding=encoding, cls=cls, object_hook=object_hook,
|
||||||
parse_float=parse_float, parse_int=parse_int,
|
parse_float=parse_float, parse_int=parse_int,
|
||||||
parse_constant=parse_constant, object_pairs_hook=None, **kw)
|
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
|
||||||
|
**kw)
|
||||||
|
|
||||||
|
|
||||||
def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
|
def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ def py_make_scanner(context):
|
||||||
parse_int = context.parse_int
|
parse_int = context.parse_int
|
||||||
parse_constant = context.parse_constant
|
parse_constant = context.parse_constant
|
||||||
object_hook = context.object_hook
|
object_hook = context.object_hook
|
||||||
|
object_pairs_hook = context.object_pairs_hook
|
||||||
|
|
||||||
def _scan_once(string, idx):
|
def _scan_once(string, idx):
|
||||||
try:
|
try:
|
||||||
|
|
@ -34,7 +35,7 @@ def py_make_scanner(context):
|
||||||
return parse_string(string, idx + 1, encoding, strict)
|
return parse_string(string, idx + 1, encoding, strict)
|
||||||
elif nextchar == '{':
|
elif nextchar == '{':
|
||||||
return parse_object((string, idx + 1), encoding, strict,
|
return parse_object((string, idx + 1), encoding, strict,
|
||||||
_scan_once, object_hook)
|
_scan_once, object_hook, object_pairs_hook)
|
||||||
elif nextchar == '[':
|
elif nextchar == '[':
|
||||||
return parse_array((string, idx + 1), _scan_once)
|
return parse_array((string, idx + 1), _scan_once)
|
||||||
elif nextchar == 'n' and string[idx:idx + 4] == 'null':
|
elif nextchar == 'n' and string[idx:idx + 4] == 'null':
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import decimal
|
import decimal
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
@ -27,12 +28,14 @@ class TestDecode(TestCase):
|
||||||
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
|
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
|
||||||
("qrt", 5), ("pad", 6), ("hoy", 7)]
|
("qrt", 5), ("pad", 6), ("hoy", 7)]
|
||||||
self.assertEqual(json.loads(s), eval(s))
|
self.assertEqual(json.loads(s), eval(s))
|
||||||
self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p)
|
self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p)
|
||||||
od = json.loads(s, object_pairs_hook = OrderedDict)
|
self.assertEqual(json.load(StringIO(s),
|
||||||
|
object_pairs_hook=lambda x: x), p)
|
||||||
|
od = json.loads(s, object_pairs_hook=OrderedDict)
|
||||||
self.assertEqual(od, OrderedDict(p))
|
self.assertEqual(od, OrderedDict(p))
|
||||||
self.assertEqual(type(od), OrderedDict)
|
self.assertEqual(type(od), OrderedDict)
|
||||||
# the object_pairs_hook takes priority over the object_hook
|
# the object_pairs_hook takes priority over the object_hook
|
||||||
self.assertEqual(json.loads(s,
|
self.assertEqual(json.loads(s,
|
||||||
object_pairs_hook = OrderedDict,
|
object_pairs_hook=OrderedDict,
|
||||||
object_hook = lambda x: None),
|
object_hook=lambda x: None),
|
||||||
OrderedDict(p))
|
OrderedDict(p))
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,15 @@ import json
|
||||||
|
|
||||||
class TestFloat(TestCase):
|
class TestFloat(TestCase):
|
||||||
def test_floats(self):
|
def test_floats(self):
|
||||||
for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100, 3.1]:
|
for num in [1617161771.7650001, math.pi, math.pi**100,
|
||||||
|
math.pi**-100, 3.1]:
|
||||||
self.assertEquals(float(json.dumps(num)), num)
|
self.assertEquals(float(json.dumps(num)), num)
|
||||||
self.assertEquals(json.loads(json.dumps(num)), num)
|
self.assertEquals(json.loads(json.dumps(num)), num)
|
||||||
|
self.assertEquals(json.loads(unicode(json.dumps(num))), num)
|
||||||
|
|
||||||
def test_ints(self):
|
def test_ints(self):
|
||||||
for num in [1, 1L, 1<<32, 1<<64]:
|
for num in [1, 1L, 1<<32, 1<<64]:
|
||||||
self.assertEquals(json.dumps(num), str(num))
|
self.assertEquals(json.dumps(num), str(num))
|
||||||
self.assertEquals(int(json.dumps(num)), num)
|
self.assertEquals(int(json.dumps(num)), num)
|
||||||
|
self.assertEquals(json.loads(json.dumps(num)), num)
|
||||||
|
self.assertEquals(json.loads(unicode(json.dumps(num))), num)
|
||||||
|
|
|
||||||
|
|
@ -1468,7 +1468,7 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_
|
||||||
if (idx < end_idx && str[idx] == '.' && str[idx + 1] >= '0' && str[idx + 1] <= '9') {
|
if (idx < end_idx && str[idx] == '.' && str[idx + 1] >= '0' && str[idx + 1] <= '9') {
|
||||||
is_float = 1;
|
is_float = 1;
|
||||||
idx += 2;
|
idx += 2;
|
||||||
while (idx < end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
|
while (idx <= end_idx && str[idx] >= '0' && str[idx] <= '9') idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
|
/* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue