@@ -2035,11 +2035,9 @@ PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *pub_writer,
20352035 if (!converted ) {
20362036 return -1 ;
20372037 }
2038- PyObject * unicode = _PyUnicode_FromUCS4 (converted , size );
2039- PyMem_Free (converted );
20402038
2041- int res = _PyUnicodeWriter_WriteStr ( writer , unicode );
2042- Py_DECREF ( unicode );
2039+ int res = PyUnicodeWriter_WriteUCS4 ( pub_writer , converted , size );
2040+ PyMem_Free ( converted );
20432041 return res ;
20442042 }
20452043#endif
@@ -2289,6 +2287,51 @@ _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size)
22892287 return res ;
22902288}
22912289
2290+
2291+ int
2292+ PyUnicodeWriter_WriteUCS4 (PyUnicodeWriter * pub_writer ,
2293+ Py_UCS4 * str ,
2294+ Py_ssize_t size )
2295+ {
2296+ _PyUnicodeWriter * writer = (_PyUnicodeWriter * )pub_writer ;
2297+
2298+ if (size < 0 ) {
2299+ PyErr_SetString (PyExc_ValueError ,
2300+ "size must be positive" );
2301+ return -1 ;
2302+ }
2303+
2304+ if (size == 0 ) {
2305+ return 0 ;
2306+ }
2307+
2308+ Py_UCS4 max_char = ucs4lib_find_max_char (str , str + size );
2309+
2310+ if (_PyUnicodeWriter_Prepare (writer , size , max_char ) < 0 ) {
2311+ return -1 ;
2312+ }
2313+
2314+ int kind = writer -> kind ;
2315+ void * data = (Py_UCS1 * )writer -> data + writer -> pos * kind ;
2316+ if (kind == PyUnicode_1BYTE_KIND ) {
2317+ _PyUnicode_CONVERT_BYTES (Py_UCS4 , Py_UCS1 ,
2318+ str , str + size ,
2319+ data );
2320+ }
2321+ else if (kind == PyUnicode_2BYTE_KIND ) {
2322+ _PyUnicode_CONVERT_BYTES (Py_UCS4 , Py_UCS2 ,
2323+ str , str + size ,
2324+ data );
2325+ }
2326+ else {
2327+ memcpy (data , str , size * sizeof (Py_UCS4 ));
2328+ }
2329+ writer -> pos += size ;
2330+
2331+ return 0 ;
2332+ }
2333+
2334+
22922335PyObject *
22932336PyUnicode_FromKindAndData (int kind , const void * buffer , Py_ssize_t size )
22942337{
@@ -13357,7 +13400,7 @@ PyUnicodeWriter*
1335713400PyUnicodeWriter_Create (Py_ssize_t length )
1335813401{
1335913402 if (length < 0 ) {
13360- PyErr_SetString (PyExc_TypeError ,
13403+ PyErr_SetString (PyExc_ValueError ,
1336113404 "length must be positive" );
1336213405 return NULL ;
1336313406 }
0 commit comments