@@ -2335,16 +2335,15 @@ PyUnicodeWriter_WriteUCS4(PyUnicodeWriter *pub_writer,
2335
2335
static int32_t
2336
2336
unicode_export (PyObject * obj , Py_buffer * view ,
2337
2337
Py_ssize_t len , const void * buf ,
2338
- int itemsize , const char * format , int32_t internal_format )
2338
+ int itemsize , const char * format , int32_t export_format )
2339
2339
{
2340
2340
if (PyBuffer_FillInfo (view , obj , (void * )buf , len ,
2341
2341
1 , PyBUF_SIMPLE ) < 0 ) {
2342
2342
return -1 ;
2343
2343
}
2344
2344
view -> itemsize = itemsize ;
2345
2345
view -> format = (char * )format ;
2346
- view -> internal = (void * )(uintptr_t )internal_format ;
2347
- return internal_format ;
2346
+ return export_format ;
2348
2347
}
2349
2348
2350
2349
@@ -2398,20 +2397,15 @@ PyUnicode_Export(PyObject *unicode, int32_t requested_formats,
2398
2397
if (kind == PyUnicode_1BYTE_KIND
2399
2398
&& requested_formats & PyUnicode_FORMAT_UCS2 )
2400
2399
{
2401
- PyObject * bytes = PyBytes_FromStringAndSize (NULL , (len + 1 ) * 2 );
2400
+ const int byteorder = (PY_BIG_ENDIAN == 1 ) ? 1 : -1 ;
2401
+ PyObject * bytes = _PyUnicode_EncodeUTF16 (unicode , NULL , byteorder );
2402
2402
if (!bytes ) {
2403
2403
return -1 ;
2404
2404
}
2405
- Py_UCS2 * ucs2 = (Py_UCS2 * )PyBytes_AS_STRING (bytes );
2406
-
2407
- _PyUnicode_CONVERT_BYTES (Py_UCS1 , Py_UCS2 ,
2408
- PyUnicode_1BYTE_DATA (unicode ),
2409
- PyUnicode_1BYTE_DATA (unicode ) + len ,
2410
- ucs2 );
2411
- ucs2 [len ] = 0 ;
2405
+ void * data = PyBytes_AS_STRING (bytes );
2412
2406
2413
2407
int32_t res = unicode_export (bytes , view ,
2414
- len , ucs2 ,
2408
+ len , data ,
2415
2409
2 , "H" , PyUnicode_FORMAT_UCS2 );
2416
2410
Py_DECREF (bytes );
2417
2411
return res ;
@@ -2428,20 +2422,14 @@ PyUnicode_Export(PyObject *unicode, int32_t requested_formats,
2428
2422
2429
2423
// Convert ASCII, UCS1 or UCS2 to UCS4
2430
2424
if (requested_formats & PyUnicode_FORMAT_UCS4 ) {
2431
- Py_UCS4 * ucs4 = PyUnicode_AsUCS4Copy (unicode );
2432
- if (ucs4 == NULL ) {
2433
- return -1 ;
2434
- }
2435
-
2436
- PyObject * bytes = PyBytes_FromStringAndSize ((char * )ucs4 , (len + 1 ) * 4 );
2437
- PyMem_Free (ucs4 );
2438
- if (bytes == NULL ) {
2425
+ const int byteorder = (PY_BIG_ENDIAN == 1 ) ? 1 : -1 ;
2426
+ PyObject * bytes = _PyUnicode_EncodeUTF32 (unicode , NULL , byteorder );
2427
+ if (!bytes ) {
2439
2428
return -1 ;
2440
2429
}
2441
- ucs4 = (Py_UCS4 * )PyBytes_AS_STRING (bytes );
2442
-
2430
+ void * data = PyBytes_AS_STRING (bytes );
2443
2431
int32_t res = unicode_export (bytes , view ,
2444
- len , ucs4 ,
2432
+ len , data ,
2445
2433
4 , BUFFER_UCS4 , PyUnicode_FORMAT_UCS4 );
2446
2434
Py_DECREF (bytes );
2447
2435
return res ;
0 commit comments