Skip to content

Commit 449a731

Browse files
vstinnerthejesh23
andauthored
[3.14] gh-162525: Fix json C encoder for str subclass separators (#162526) (#162746)
gh-162525: Fix json C encoder for str subclass separators (#162526) (cherry picked from commit 551df9d) Co-authored-by: Thejesh <thejesh36@gmail.com>
1 parent 92ae284 commit 449a731

4 files changed

Lines changed: 42 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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,11 @@ encoder_encode_string(PyEncoderObject *s, PyObject *obj)
14691469
static int
14701470
_steal_accumulate(PyUnicodeWriter *writer, PyObject *stolen)
14711471
{
1472-
/* Append stolen and then decrement its reference count */
1473-
int rval = PyUnicodeWriter_WriteStr(writer, stolen);
1472+
/* Append stolen and then decrement its reference count.
1473+
gh-148241: Avoid PyUnicodeWriter_WriteStr() which calls str(obj)
1474+
on str subclasses. */
1475+
assert(PyUnicode_Check(stolen));
1476+
int rval = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, stolen);
14741477
Py_DECREF(stolen);
14751478
return rval;
14761479
}
@@ -1626,7 +1629,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
16261629
}
16271630
}
16281631
else {
1629-
if (PyUnicodeWriter_WriteStr(writer, item_separator) < 0) {
1632+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
1633+
item_separator) < 0) {
16301634
Py_DECREF(keystr);
16311635
return -1;
16321636
}
@@ -1641,7 +1645,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
16411645
if (_steal_accumulate(writer, encoded) < 0) {
16421646
return -1;
16431647
}
1644-
if (PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) {
1648+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
1649+
s->key_separator) < 0) {
16451650
return -1;
16461651
}
16471652
if (encoder_listencode_obj(s, writer, value, indent_level, indent_cache) < 0) {
@@ -1820,7 +1825,7 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
18201825
// that mutates the sequence, invalidating this borrowed ref.
18211826
Py_INCREF(obj);
18221827
if (i) {
1823-
if (PyUnicodeWriter_WriteStr(writer, separator) < 0) {
1828+
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, separator) < 0) {
18241829
Py_DECREF(obj);
18251830
goto bail;
18261831
}

0 commit comments

Comments
 (0)