Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_json/test_separators.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ def test_illegal_separators(self):
self.assertRaises(TypeError, self.dumps, h, separators=(', ', b': '))
self.assertRaises(TypeError, self.dumps, h, separators=(b', ', b': '))

def test_str_subclass_separators(self):
# Don't call obj.__str__() on str subclasses (see gh-148241)
class StrSubclass(str):
def __str__(self):
return "StrSubclass"

h = {'a': [1, 2], 'b': 3}
seps = (StrSubclass(',_'), StrSubclass(':_'))
self.assertEqual(self.dumps(h, separators=seps),
'{"a":_[1,_2],_"b":_3}')
self.assertEqual(self.dumps(h, separators=seps, sort_keys=True),
'{"a":_[1,_2],_"b":_3}')
self.assertEqual(self.dumps(h, separators=seps, indent=1),
'{\n "a":_[\n 1,_\n 2\n ],_\n "b":_3\n}')


class TestPySeparators(TestSeparators, PyTest): pass
class TestCSeparators(TestSeparators, CTest): pass
14 changes: 14 additions & 0 deletions Lib/test/test_json/test_speedups.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ def bad_encoder2(*args):
with self.assertRaises(ZeroDivisionError):
enc('spam', 4)

def test_str_subclass_from_encoder(self):
# gh-162525: Don't call __str__() on a str subclass returned by
# the encoder() argument
class StrSubclass(str):
def __str__(self):
return "StrSubclass"

enc = self.json.encoder.c_make_encoder(None, lambda obj: str(obj),
lambda s: StrSubclass('"x"'),
None, ': ', ', ',
False, False, False)
self.assertEqual(enc('spam', 0), ('"x"',))
self.assertEqual(enc({'spam': 'eggs'}, 0), ('{"x": "x"}',))

def test_bad_markers_argument_to_encoder(self):
# https://bugs.python.org/issue45269
with self.assertRaisesRegex(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix the C :mod:`json` encoder writing the :meth:`~object.__str__` result of
:class:`str` subclasses used as *separators*, which produced invalid JSON.
It now writes their characters, like the pure Python encoder.
16 changes: 11 additions & 5 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1558,8 +1558,11 @@ encoder_write_string(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *obj)
static int
_steal_accumulate(PyUnicodeWriter *writer, PyObject *stolen)
{
/* Append stolen and then decrement its reference count */
int rval = PyUnicodeWriter_WriteStr(writer, stolen);
/* Append stolen and then decrement its reference count.
gh-148241: Avoid PyUnicodeWriter_WriteStr() which calls str(obj)
on str subclasses. */
assert(PyUnicode_Check(stolen));
int rval = _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, stolen);
Py_DECREF(stolen);
return rval;
}
Expand Down Expand Up @@ -1712,7 +1715,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
}
}
else {
if (PyUnicodeWriter_WriteStr(writer, item_separator) < 0) {
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
item_separator) < 0) {
Py_DECREF(keystr);
return -1;
}
Expand All @@ -1724,7 +1728,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
if (rv < 0) {
return -1;
}
if (PyUnicodeWriter_WriteStr(writer, s->key_separator) < 0) {
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
s->key_separator) < 0) {
return -1;
}
if (encoder_listencode_obj(s, writer, value, indent_level, indent_cache) < 0) {
Expand Down Expand Up @@ -1899,7 +1904,8 @@ _encoder_iterate_fast_seq_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
// that mutates the sequence, invalidating this borrowed ref.
Py_INCREF(obj);
if (i) {
if (PyUnicodeWriter_WriteStr(writer, separator) < 0) {
if (_PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer,
separator) < 0) {
Py_DECREF(obj);
return -1;
}
Expand Down
Loading