@@ -1559,8 +1559,11 @@ encoder_write_string(PyEncoderObject *s, PyUnicodeWriter *writer, PyObject *obj)
15591559static int
15601560_steal_accumulate (PyUnicodeWriter * writer , PyObject * stolen )
15611561{
1562- /* Append stolen and then decrement its reference count */
1563- int rval = PyUnicodeWriter_WriteStr (writer , stolen );
1562+ /* Append stolen and then decrement its reference count.
1563+ gh-148241: Avoid PyUnicodeWriter_WriteStr() which calls str(obj)
1564+ on str subclasses. */
1565+ assert (PyUnicode_Check (stolen ));
1566+ int rval = _PyUnicodeWriter_WriteStr ((_PyUnicodeWriter * )writer , stolen );
15641567 Py_DECREF (stolen );
15651568 return rval ;
15661569}
@@ -1713,7 +1716,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
17131716 }
17141717 }
17151718 else {
1716- if (PyUnicodeWriter_WriteStr (writer , item_separator ) < 0 ) {
1719+ if (_PyUnicodeWriter_WriteStr ((_PyUnicodeWriter * )writer ,
1720+ item_separator ) < 0 ) {
17171721 Py_DECREF (keystr );
17181722 return -1 ;
17191723 }
@@ -1725,7 +1729,8 @@ encoder_encode_key_value(PyEncoderObject *s, PyUnicodeWriter *writer, bool *firs
17251729 if (rv < 0 ) {
17261730 return -1 ;
17271731 }
1728- if (PyUnicodeWriter_WriteStr (writer , s -> key_separator ) < 0 ) {
1732+ if (_PyUnicodeWriter_WriteStr ((_PyUnicodeWriter * )writer ,
1733+ s -> key_separator ) < 0 ) {
17291734 return -1 ;
17301735 }
17311736 if (encoder_listencode_obj (s , writer , value , indent_level , indent_cache ) < 0 ) {
@@ -1897,7 +1902,8 @@ _encoder_iterate_fast_seq_lock_held(PyEncoderObject *s, PyUnicodeWriter *writer,
18971902 // that mutates the sequence, invalidating this borrowed ref.
18981903 Py_INCREF (obj );
18991904 if (i ) {
1900- if (PyUnicodeWriter_WriteStr (writer , separator ) < 0 ) {
1905+ if (_PyUnicodeWriter_WriteStr ((_PyUnicodeWriter * )writer ,
1906+ separator ) < 0 ) {
19011907 Py_DECREF (obj );
19021908 return -1 ;
19031909 }
0 commit comments