Skip to content

Commit 551df9d

Browse files
authored
gh-162525: Fix json C encoder for str subclass separators (#162526)
1 parent 806d492 commit 551df9d

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
@@ -1566,8 +1566,11 @@ encoder_write_string(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *obj)
15661566
static int
15671567
_steal_accumulate(PyUnicodeWriter *writer, PyObject *stolen)
15681568
{
1569-
/* Append stolen and then decrement its reference count */
1570-
int rval = PyUnicodeWriter_WriteStr(writer, stolen);
1569+
/* Append stolen and then decrement its reference count.
1570+
gh-148241: Avoid PyUnicodeWriter_WriteStr() which calls str(obj)
1571+
on str subclasses. */
1572+
assert(PyUnicode_Check(stolen));
1573+
int rval = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, stolen);
15711574
Py_DECREF(stolen);
15721575
return rval;
15731576
}
@@ -1720,7 +1723,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
17201723
}
17211724
}
17221725
else {
1723-
if (PyUnicodeWriter_WriteStr(writer, item_separator) < 0) {
1726+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
1727+
item_separator) < 0) {
17241728
Py_DECREF(keystr);
17251729
return -1;
17261730
}
@@ -1732,7 +1736,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
17321736
if (rv < 0) {
17331737
return -1;
17341738
}
1735-
if (PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) {
1739+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
1740+
s->key_separator) < 0) {
17361741
return -1;
17371742
}
17381743
if (encoder_listencode_obj(s, writer, value, indent_level, indent_cache) < 0) {
@@ -1907,7 +1912,8 @@ _encoder_iterate_fast_seq_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
19071912
// that mutates the sequence, invalidating this borrowed ref.
19081913
Py_INCREF(obj);
19091914
if (i) {
1910-
if (PyUnicodeWriter_WriteStr(writer, separator) < 0) {
1915+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
1916+
separator) < 0) {
19111917
Py_DECREF(obj);
19121918
return -1;
19131919
}

0 commit comments

Comments
 (0)