Skip to content

Commit ef871d2

Browse files
authored
Update json module to 3.13.11 (#6743)
1 parent 746e71a commit ef871d2

File tree

6 files changed

+98
-40
lines changed

6 files changed

+98
-40
lines changed

Lib/json/__init__.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
128128
instead of raising a ``TypeError``.
129129
130130
If ``ensure_ascii`` is false, then the strings written to ``fp`` can
131-
contain non-ASCII characters if they appear in strings contained in
132-
``obj``. Otherwise, all such characters are escaped in JSON strings.
131+
contain non-ASCII and non-printable characters if they appear in strings
132+
contained in ``obj``. Otherwise, all such characters are escaped in JSON
133+
strings.
133134
134135
If ``check_circular`` is false, then the circular reference check
135136
for container types will be skipped and a circular reference will
@@ -145,10 +146,11 @@ def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
145146
level of 0 will only insert newlines. ``None`` is the most compact
146147
representation.
147148
148-
If specified, ``separators`` should be an ``(item_separator, key_separator)``
149-
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
150-
``(',', ': ')`` otherwise. To get the most compact JSON representation,
151-
you should specify ``(',', ':')`` to eliminate whitespace.
149+
If specified, ``separators`` should be an ``(item_separator,
150+
key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is
151+
``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON
152+
representation, you should specify ``(',', ':')`` to eliminate
153+
whitespace.
152154
153155
``default(obj)`` is a function that should return a serializable version
154156
of obj or raise TypeError. The default simply raises TypeError.
@@ -189,9 +191,10 @@ def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
189191
(``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
190192
instead of raising a ``TypeError``.
191193
192-
If ``ensure_ascii`` is false, then the return value can contain non-ASCII
193-
characters if they appear in strings contained in ``obj``. Otherwise, all
194-
such characters are escaped in JSON strings.
194+
If ``ensure_ascii`` is false, then the return value can contain
195+
non-ASCII and non-printable characters if they appear in strings
196+
contained in ``obj``. Otherwise, all such characters are escaped in
197+
JSON strings.
195198
196199
If ``check_circular`` is false, then the circular reference check
197200
for container types will be skipped and a circular reference will
@@ -207,10 +210,11 @@ def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
207210
level of 0 will only insert newlines. ``None`` is the most compact
208211
representation.
209212
210-
If specified, ``separators`` should be an ``(item_separator, key_separator)``
211-
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
212-
``(',', ': ')`` otherwise. To get the most compact JSON representation,
213-
you should specify ``(',', ':')`` to eliminate whitespace.
213+
If specified, ``separators`` should be an ``(item_separator,
214+
key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is
215+
``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON
216+
representation, you should specify ``(',', ':')`` to eliminate
217+
whitespace.
214218
215219
``default(obj)`` is a function that should return a serializable version
216220
of obj or raise TypeError. The default simply raises TypeError.
@@ -281,11 +285,12 @@ def load(fp, *, cls=None, object_hook=None, parse_float=None,
281285
``object_hook`` will be used instead of the ``dict``. This feature
282286
can be used to implement custom decoders (e.g. JSON-RPC class hinting).
283287
284-
``object_pairs_hook`` is an optional function that will be called with the
285-
result of any object literal decoded with an ordered list of pairs. The
286-
return value of ``object_pairs_hook`` will be used instead of the ``dict``.
287-
This feature can be used to implement custom decoders. If ``object_hook``
288-
is also defined, the ``object_pairs_hook`` takes priority.
288+
``object_pairs_hook`` is an optional function that will be called with
289+
the result of any object literal decoded with an ordered list of pairs.
290+
The return value of ``object_pairs_hook`` will be used instead of the
291+
``dict``. This feature can be used to implement custom decoders. If
292+
``object_hook`` is also defined, the ``object_pairs_hook`` takes
293+
priority.
289294
290295
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
291296
kwarg; otherwise ``JSONDecoder`` is used.
@@ -306,11 +311,12 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None,
306311
``object_hook`` will be used instead of the ``dict``. This feature
307312
can be used to implement custom decoders (e.g. JSON-RPC class hinting).
308313
309-
``object_pairs_hook`` is an optional function that will be called with the
310-
result of any object literal decoded with an ordered list of pairs. The
311-
return value of ``object_pairs_hook`` will be used instead of the ``dict``.
312-
This feature can be used to implement custom decoders. If ``object_hook``
313-
is also defined, the ``object_pairs_hook`` takes priority.
314+
``object_pairs_hook`` is an optional function that will be called with
315+
the result of any object literal decoded with an ordered list of pairs.
316+
The return value of ``object_pairs_hook`` will be used instead of the
317+
``dict``. This feature can be used to implement custom decoders. If
318+
``object_hook`` is also defined, the ``object_pairs_hook`` takes
319+
priority.
314320
315321
``parse_float``, if specified, will be called with the string
316322
of every JSON float to be decoded. By default this is equivalent to

Lib/json/decoder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,10 @@ def __init__(self, *, object_hook=None, parse_float=None,
311311
place of the given ``dict``. This can be used to provide custom
312312
deserializations (e.g. to support JSON-RPC class hinting).
313313
314-
``object_pairs_hook``, if specified will be called with the result of
315-
every JSON object decoded with an ordered list of pairs. The return
316-
value of ``object_pairs_hook`` will be used instead of the ``dict``.
317-
This feature can be used to implement custom decoders.
314+
``object_pairs_hook``, if specified will be called with the result
315+
of every JSON object decoded with an ordered list of pairs. The
316+
return value of ``object_pairs_hook`` will be used instead of the
317+
``dict``. This feature can be used to implement custom decoders.
318318
If ``object_hook`` is also defined, the ``object_pairs_hook`` takes
319319
priority.
320320

Lib/json/encoder.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ def __init__(self, *, skipkeys=False, ensure_ascii=True,
111111
encoding of keys that are not str, int, float, bool or None.
112112
If skipkeys is True, such items are simply skipped.
113113
114-
If ensure_ascii is true, the output is guaranteed to be str
115-
objects with all incoming non-ASCII characters escaped. If
116-
ensure_ascii is false, the output can contain non-ASCII characters.
114+
If ensure_ascii is true, the output is guaranteed to be str objects
115+
with all incoming non-ASCII and non-printable characters escaped.
116+
If ensure_ascii is false, the output can contain non-ASCII and
117+
non-printable characters.
117118
118119
If check_circular is true, then lists, dicts, and custom encoded
119120
objects will be checked for circular references during encoding to
@@ -134,14 +135,15 @@ def __init__(self, *, skipkeys=False, ensure_ascii=True,
134135
indent level. An indent level of 0 will only insert newlines.
135136
None is the most compact representation.
136137
137-
If specified, separators should be an (item_separator, key_separator)
138-
tuple. The default is (', ', ': ') if *indent* is ``None`` and
139-
(',', ': ') otherwise. To get the most compact JSON representation,
140-
you should specify (',', ':') to eliminate whitespace.
138+
If specified, separators should be an (item_separator,
139+
key_separator) tuple. The default is (', ', ': ') if *indent* is
140+
``None`` and (',', ': ') otherwise. To get the most compact JSON
141+
representation, you should specify (',', ':') to eliminate
142+
whitespace.
141143
142144
If specified, default is a function that gets called for objects
143-
that can't otherwise be serialized. It should return a JSON encodable
144-
version of the object or raise a ``TypeError``.
145+
that can't otherwise be serialized. It should return a JSON
146+
encodable version of the object or raise a ``TypeError``.
145147
146148
"""
147149

Lib/test/test_json/test_encode_basestring_ascii.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
99
('controls', '"controls"'),
1010
('\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'),
11+
('\x00\x1f\x7f', '"\\u0000\\u001f\\u007f"'),
1112
('{"object with 1 member":["array with 1 element"]}', '"{\\"object with 1 member\\":[\\"array with 1 element\\"]}"'),
1213
(' s p a c e d ', '" s p a c e d "'),
1314
('\U0001d120', '"\\ud834\\udd20"'),
1415
('\u03b1\u03a9', '"\\u03b1\\u03a9"'),
1516
("`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'),
16-
('\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'),
17-
('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
1817
]
1918

2019
class TestEncodeBasestringAscii:

Lib/test/test_json/test_scanstring.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import unittest # XXX: RUSTPYTHON; importing to be able to skip tests
55

6+
67
class TestScanstring:
78
def test_scanstring(self):
89
scanstring = self.json.decoder.scanstring
@@ -147,7 +148,7 @@ def test_bad_escapes(self):
147148
@unittest.expectedFailure
148149
def test_overflow(self):
149150
with self.assertRaises(OverflowError):
150-
self.json.decoder.scanstring(b"xxx", sys.maxsize+1)
151+
self.json.decoder.scanstring("xxx", sys.maxsize+1)
151152

152153

153154
class TestPyScanstring(TestScanstring, PyTest): pass

Lib/test/test_json/test_unicode.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ def test_encoding7(self):
3434
j = self.dumps(u + "\n", ensure_ascii=False)
3535
self.assertEqual(j, f'"{u}\\n"')
3636

37+
def test_ascii_non_printable_encode(self):
38+
u = '\b\t\n\f\r\x00\x1f\x7f'
39+
self.assertEqual(self.dumps(u),
40+
'"\\b\\t\\n\\f\\r\\u0000\\u001f\\u007f"')
41+
self.assertEqual(self.dumps(u, ensure_ascii=False),
42+
'"\\b\\t\\n\\f\\r\\u0000\\u001f\x7f"')
43+
44+
def test_ascii_non_printable_decode(self):
45+
self.assertEqual(self.loads('"\\b\\t\\n\\f\\r"'),
46+
'\b\t\n\f\r')
47+
s = ''.join(map(chr, range(32)))
48+
for c in s:
49+
self.assertRaises(self.JSONDecodeError, self.loads, f'"{c}"')
50+
self.assertEqual(self.loads(f'"{s}"', strict=False), s)
51+
self.assertEqual(self.loads('"\x7f"'), '\x7f')
52+
53+
def test_escaped_decode(self):
54+
self.assertEqual(self.loads('"\\b\\t\\n\\f\\r"'), '\b\t\n\f\r')
55+
self.assertEqual(self.loads('"\\"\\\\\\/"'), '"\\/')
56+
for c in set(map(chr, range(0x100))) - set('"\\/bfnrt'):
57+
self.assertRaises(self.JSONDecodeError, self.loads, f'"\\{c}"')
58+
self.assertRaises(self.JSONDecodeError, self.loads, f'"\\{c}"', strict=False)
59+
3760
def test_big_unicode_encode(self):
3861
u = '\U0001d120'
3962
self.assertEqual(self.dumps(u), '"\\ud834\\udd20"')
@@ -50,6 +73,18 @@ def test_unicode_decode(self):
5073
s = f'"\\u{i:04x}"'
5174
self.assertEqual(self.loads(s), u)
5275

76+
def test_single_surrogate_encode(self):
77+
self.assertEqual(self.dumps('\uD83D'), '"\\ud83d"')
78+
self.assertEqual(self.dumps('\uD83D', ensure_ascii=False), '"\ud83d"')
79+
self.assertEqual(self.dumps('\uDC0D'), '"\\udc0d"')
80+
self.assertEqual(self.dumps('\uDC0D', ensure_ascii=False), '"\udc0d"')
81+
82+
def test_single_surrogate_decode(self):
83+
self.assertEqual(self.loads('"\uD83D"'), '\ud83d')
84+
self.assertEqual(self.loads('"\\uD83D"'), '\ud83d')
85+
self.assertEqual(self.loads('"\udc0d"'), '\udc0d')
86+
self.assertEqual(self.loads('"\\udc0d"'), '\udc0d')
87+
5388
def test_unicode_preservation(self):
5489
self.assertEqual(type(self.loads('""')), str)
5590
self.assertEqual(type(self.loads('"a"')), str)
@@ -104,4 +139,19 @@ def test_object_pairs_hook_with_unicode(self):
104139

105140

106141
class TestPyUnicode(TestUnicode, PyTest): pass
107-
class TestCUnicode(TestUnicode, CTest): pass
142+
143+
class TestCUnicode(TestUnicode, CTest):
144+
# TODO: RUSTPYTHON
145+
@unittest.expectedFailure
146+
def test_ascii_non_printable_encode(self):
147+
return super().test_ascii_non_printable_encode()
148+
149+
# TODO: RUSTPYTHON
150+
@unittest.skip("TODO: RUSTPYTHON; panics with 'str has surrogates'")
151+
def test_single_surrogate_decode(self):
152+
return super().test_single_surrogate_decode()
153+
154+
# TODO: RUSTPYTHON
155+
@unittest.skip("TODO: RUSTPYTHON; panics with 'str has surrogates'")
156+
def test_single_surrogate_encode(self):
157+
return super().test_single_surrogate_encode()

0 commit comments

Comments
 (0)