@@ -1566,8 +1566,11 @@ encoder_write_string(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *obj)
15661566static 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