@@ -1469,8 +1469,11 @@ encoder_encode_string(PyEncoderObject *s, PyObject *obj)
14691469static 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