mirror of
https://github.com/python/cpython.git
synced 2026-01-26 21:03:34 +00:00
[3.14] gh-143196: Fix crash in non-standard use of internal JSON encoder object (GH-143618) (GH-143748)
The internal encoder object returned by undocumented function json.encoder.c_make_encoder() (aka _json.make_encoder()) crashed when it was called with non-zero second argument. (cherry picked from commit c559135c931789ebc752ae68814858c398cb798b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
9c7785e83a
commit
7a066ad020
@ -80,3 +80,34 @@ class TestEncode(CTest):
|
||||
def test_unsortable_keys(self):
|
||||
with self.assertRaises(TypeError):
|
||||
self.json.encoder.JSONEncoder(sort_keys=True).encode({'a': 1, 1: 'a'})
|
||||
|
||||
def test_current_indent_level(self):
|
||||
enc = self.json.encoder.c_make_encoder(
|
||||
markers=None,
|
||||
default=str,
|
||||
encoder=self.json.encoder.c_encode_basestring,
|
||||
indent='\t',
|
||||
key_separator=': ',
|
||||
item_separator=', ',
|
||||
sort_keys=False,
|
||||
skipkeys=False,
|
||||
allow_nan=False)
|
||||
expected = (
|
||||
'[\n'
|
||||
'\t"spam", \n'
|
||||
'\t{\n'
|
||||
'\t\t"ham": "eggs"\n'
|
||||
'\t}\n'
|
||||
']')
|
||||
self.assertEqual(enc(['spam', {'ham': 'eggs'}], 0)[0], expected)
|
||||
self.assertEqual(enc(['spam', {'ham': 'eggs'}], -3)[0], expected)
|
||||
expected2 = (
|
||||
'[\n'
|
||||
'\t\t\t\t"spam", \n'
|
||||
'\t\t\t\t{\n'
|
||||
'\t\t\t\t\t"ham": "eggs"\n'
|
||||
'\t\t\t\t}\n'
|
||||
'\t\t\t]')
|
||||
self.assertEqual(enc(['spam', {'ham': 'eggs'}], 3)[0], expected2)
|
||||
self.assertRaises(TypeError, enc, ['spam', {'ham': 'eggs'}], 3.0)
|
||||
self.assertRaises(TypeError, enc, ['spam', {'ham': 'eggs'}])
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
Fix crash when the internal encoder object returned by undocumented function
|
||||
:func:`!json.encoder.c_make_encoder` was called with non-zero second
|
||||
(*_current_indent_level*) argument.
|
||||
@ -1383,6 +1383,7 @@ encoder_call(PyObject *op, PyObject *args, PyObject *kwds)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
indent_level = 0;
|
||||
if (encoder_listencode_obj(self, writer, obj, indent_level, indent_cache)) {
|
||||
PyUnicodeWriter_Discard(writer);
|
||||
Py_XDECREF(indent_cache);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user