Skip to content

Commit 46e173e

Browse files
thejesh23miss-islington
authored andcommitted
gh-162525: Fix json C encoder for str subclass separators (GH-162526)
(cherry picked from commit 551df9d) Co-authored-by: Thejesh <thejesh36@gmail.com>
1 parent 47bbbb8 commit 46e173e

4 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎Lib/test/test_json/test_separators.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ def test_illegal_separators(self):
4545
self.assertRaises(TypeError, self.dumps, h, separators=(', ', b': '))
4646
self.assertRaises(TypeError, self.dumps, h, separators=(b', ', b': '))
4747

48+
def test_str_subclass_separators(self):
49+
# Don't call obj.__str__() on str subclasses (see gh-148241)
50+
class StrSubclass(str):
51+
def __str__(self):
52+
return "StrSubclass"
53+
54+
h = {'a': [1, 2], 'b': 3}
55+
seps = (StrSubclass(',_'), StrSubclass(':_'))
56+
self.assertEqual(self.dumps(h, separators=seps),
57+
'{"a":_[1,_2],_"b":_3}')
58+
self.assertEqual(self.dumps(h, separators=seps, sort_keys=True),
59+
'{"a":_[1,_2],_"b":_3}')
60+
self.assertEqual(self.dumps(h, separators=seps, indent=1),
61+
'{\n "a":_[\n 1,_\n 2\n ],_\n "b":_3\n}')
62+
4863

4964
class TestPySeparators(TestSeparators, PyTest): pass
5065
class TestCSeparators(TestSeparators, CTest): pass

‎Lib/test/test_json/test_speedups.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ def bad_encoder2(*args):
6060
with self.assertRaises(ZeroDivisionError):
6161
enc('spam', 4)
6262

63+
def test_str_subclass_from_encoder(self):
64+
# gh-162525: Don't call __str__() on a str subclass returned by
65+
# the encoder() argument
66+
class StrSubclass(str):
67+
def __str__(self):
68+
return "StrSubclass"
69+
70+
enc = self.json.encoder.c_make_encoder(None, lambda obj: str(obj),
71+
lambda s: StrSubclass('"x"'),
72+
None, ': ', ', ',
73+
False, False, False)
74+
self.assertEqual(enc('spam', 0), ('"x"',))
75+
self.assertEqual(enc({'spam': 'eggs'}, 0), ('{"x": "x"}',))
76+
6377
def test_bad_markers_argument_to_encoder(self):
6478
# https://bugs.python.org/issue45269
6579
with self.assertRaisesRegex(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the C :mod:`json` encoder writing the :meth:`~object.__str__` result of
2+
:class:`str` subclasses used as *separators*, which produced invalid JSON.
3+
It now writes their characters, like the pure Python encoder.

‎Modules/_json.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,8 +1559,11 @@ encoder_write_string(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *obj)
15591559
static int
15601560
_steal_accumulate(PyUnicodeWriter *writer, PyObject *stolen)
15611561
{
1562-
/* Append stolen and then decrement its reference count */
1563-
int rval = PyUnicodeWriter_WriteStr(writer, stolen);
1562+
/* Append stolen and then decrement its reference count.
1563+
gh-148241: Avoid PyUnicodeWriter_WriteStr() which calls str(obj)
1564+
on str subclasses. */
1565+
assert(PyUnicode_Check(stolen));
1566+
int rval = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, stolen);
15641567
Py_DECREF(stolen);
15651568
return rval;
15661569
}
@@ -1713,7 +1716,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
17131716
}
17141717
}
17151718
else {
1716-
if (PyUnicodeWriter_WriteStr(writer, item_separator) < 0) {
1719+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
1720+
item_separator) < 0) {
17171721
Py_DECREF(keystr);
17181722
return -1;
17191723
}
@@ -1725,7 +1729,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
17251729
if (rv < 0) {
17261730
return -1;
17271731
}
1728-
if (PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) {
1732+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
1733+
s->key_separator) < 0) {
17291734
return -1;
17301735
}
17311736
if (encoder_listencode_obj(s, writer, value, indent_level, indent_cache) < 0) {
@@ -1897,7 +1902,8 @@ _encoder_iterate_fast_seq_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
18971902
// that mutates the sequence, invalidating this borrowed ref.
18981903
Py_INCREF(obj);
18991904
if (i) {
1900-
if (PyUnicodeWriter_WriteStr(writer, separator) < 0) {
1905+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
1906+
separator) < 0) {
19011907
Py_DECREF(obj);
19021908
return -1;
19031909
}

0 commit comments

Comments
 (0)