@@ -7988,7 +7988,7 @@ encode_code_page_flags(UINT code_page, const char *errors)
7988
7988
* an OSError and returns -1 on other error.
7989
7989
*/
7990
7990
static int
7991
- encode_code_page_strict (UINT code_page , PyObject * * outbytes ,
7991
+ encode_code_page_strict (UINT code_page , PyBytesWriter * * writer ,
7992
7992
PyObject * unicode , Py_ssize_t offset , int len ,
7993
7993
const char * errors )
7994
7994
{
@@ -8034,25 +8034,21 @@ encode_code_page_strict(UINT code_page, PyObject **outbytes,
8034
8034
goto done ;
8035
8035
}
8036
8036
8037
- if (* outbytes == NULL ) {
8037
+ if (* writer == NULL ) {
8038
8038
/* Create string object */
8039
- * outbytes = PyBytes_FromStringAndSize ( NULL , outsize );
8040
- if (* outbytes == NULL ) {
8039
+ * writer = PyBytesWriter_Create ( outsize );
8040
+ if (* writer == NULL ) {
8041
8041
goto done ;
8042
8042
}
8043
- out = PyBytes_AS_STRING ( * outbytes );
8043
+ out = PyBytesWriter_GetData ( * writer );
8044
8044
}
8045
8045
else {
8046
8046
/* Extend string object */
8047
- const Py_ssize_t n = PyBytes_Size (* outbytes );
8048
- if (outsize > PY_SSIZE_T_MAX - n ) {
8049
- PyErr_NoMemory ();
8050
- goto done ;
8051
- }
8052
- if (_PyBytes_Resize (outbytes , n + outsize ) < 0 ) {
8047
+ Py_ssize_t n = PyBytesWriter_GetSize (* writer );
8048
+ if (PyBytesWriter_Grow (* writer , outsize ) < 0 ) {
8053
8049
goto done ;
8054
8050
}
8055
- out = PyBytes_AS_STRING ( * outbytes ) + n ;
8051
+ out = ( char * ) PyBytesWriter_GetData ( * writer ) + n ;
8056
8052
}
8057
8053
8058
8054
/* Do the conversion */
@@ -8089,7 +8085,7 @@ encode_code_page_strict(UINT code_page, PyObject **outbytes,
8089
8085
* -1 on other error.
8090
8086
*/
8091
8087
static int
8092
- encode_code_page_errors (UINT code_page , PyObject * * outbytes ,
8088
+ encode_code_page_errors (UINT code_page , PyBytesWriter * * writer ,
8093
8089
PyObject * unicode , Py_ssize_t unicode_offset ,
8094
8090
Py_ssize_t insize , const char * errors )
8095
8091
{
@@ -8108,7 +8104,7 @@ encode_code_page_errors(UINT code_page, PyObject **outbytes,
8108
8104
PyObject * exc = NULL ;
8109
8105
PyObject * encoding_obj = NULL ;
8110
8106
const char * encoding ;
8111
- Py_ssize_t newpos , newoutsize ;
8107
+ Py_ssize_t newpos ;
8112
8108
PyObject * rep ;
8113
8109
int ret = -1 ;
8114
8110
@@ -8141,23 +8137,21 @@ encode_code_page_errors(UINT code_page, PyObject **outbytes,
8141
8137
}
8142
8138
outsize = insize * Py_ARRAY_LENGTH (buffer );
8143
8139
8144
- if (* outbytes == NULL ) {
8140
+ if (* writer == NULL ) {
8145
8141
/* Create string object */
8146
- * outbytes = PyBytes_FromStringAndSize ( NULL , outsize );
8147
- if (* outbytes == NULL )
8142
+ * writer = PyBytesWriter_Create ( outsize );
8143
+ if (* writer == NULL ) {
8148
8144
goto error ;
8149
- out = PyBytes_AS_STRING (* outbytes );
8145
+ }
8146
+ out = PyBytesWriter_GetData (* writer );
8150
8147
}
8151
8148
else {
8152
8149
/* Extend string object */
8153
- Py_ssize_t n = PyBytes_Size (* outbytes );
8154
- if (n > PY_SSIZE_T_MAX - outsize ) {
8155
- PyErr_NoMemory ();
8150
+ Py_ssize_t n = PyBytesWriter_GetSize (* writer );
8151
+ if (PyBytesWriter_Grow (* writer , outsize ) < 0 ) {
8156
8152
goto error ;
8157
8153
}
8158
- if (_PyBytes_Resize (outbytes , n + outsize ) < 0 )
8159
- goto error ;
8160
- out = PyBytes_AS_STRING (* outbytes ) + n ;
8154
+ out = (char * )PyBytesWriter_GetData (* writer ) + n ;
8161
8155
}
8162
8156
8163
8157
/* Encode the string character per character */
@@ -8206,13 +8200,11 @@ encode_code_page_errors(UINT code_page, PyObject **outbytes,
8206
8200
outsize = PyBytes_GET_SIZE (rep );
8207
8201
morebytes += outsize ;
8208
8202
if (morebytes > 0 ) {
8209
- Py_ssize_t offset = out - PyBytes_AS_STRING (* outbytes );
8210
- newoutsize = PyBytes_GET_SIZE (* outbytes ) + morebytes ;
8211
- if (_PyBytes_Resize (outbytes , newoutsize ) < 0 ) {
8203
+ out = PyBytesWriter_GrowAndUpdatePointer (* writer , morebytes , out );
8204
+ if (out == NULL ) {
8212
8205
Py_DECREF (rep );
8213
8206
goto error ;
8214
8207
}
8215
- out = PyBytes_AS_STRING (* outbytes ) + offset ;
8216
8208
}
8217
8209
memcpy (out , PyBytes_AS_STRING (rep ), outsize );
8218
8210
out += outsize ;
@@ -8225,13 +8217,11 @@ encode_code_page_errors(UINT code_page, PyObject **outbytes,
8225
8217
outsize = PyUnicode_GET_LENGTH (rep );
8226
8218
morebytes += outsize ;
8227
8219
if (morebytes > 0 ) {
8228
- Py_ssize_t offset = out - PyBytes_AS_STRING (* outbytes );
8229
- newoutsize = PyBytes_GET_SIZE (* outbytes ) + morebytes ;
8230
- if (_PyBytes_Resize (outbytes , newoutsize ) < 0 ) {
8220
+ out = PyBytesWriter_GrowAndUpdatePointer (* writer , morebytes , out );
8221
+ if (out == NULL ) {
8231
8222
Py_DECREF (rep );
8232
8223
goto error ;
8233
8224
}
8234
- out = PyBytes_AS_STRING (* outbytes ) + offset ;
8235
8225
}
8236
8226
kind = PyUnicode_KIND (rep );
8237
8227
data = PyUnicode_DATA (rep );
@@ -8254,10 +8244,11 @@ encode_code_page_errors(UINT code_page, PyObject **outbytes,
8254
8244
}
8255
8245
/* write a NUL byte */
8256
8246
* out = 0 ;
8257
- outsize = out - PyBytes_AS_STRING ( * outbytes );
8258
- assert (outsize <= PyBytes_GET_SIZE ( * outbytes ));
8259
- if (_PyBytes_Resize ( outbytes , outsize ) < 0 )
8247
+ outsize = out - ( char * ) PyBytesWriter_GetData ( * writer );
8248
+ assert (outsize <= PyBytesWriter_GetSize ( * writer ));
8249
+ if (PyBytesWriter_Resize ( * writer , outsize ) < 0 ) {
8260
8250
goto error ;
8251
+ }
8261
8252
ret = 0 ;
8262
8253
8263
8254
error :
@@ -8267,13 +8258,14 @@ encode_code_page_errors(UINT code_page, PyObject **outbytes,
8267
8258
return ret ;
8268
8259
}
8269
8260
8270
- static PyObject *
8271
- encode_code_page (int code_page ,
8272
- PyObject * unicode ,
8273
- const char * errors )
8261
+
8262
+ PyObject *
8263
+ PyUnicode_EncodeCodePage (int code_page ,
8264
+ PyObject * unicode ,
8265
+ const char * errors )
8274
8266
{
8275
8267
Py_ssize_t len ;
8276
- PyObject * outbytes = NULL ;
8268
+ PyBytesWriter * writer = NULL ;
8277
8269
Py_ssize_t offset ;
8278
8270
int chunk_len , ret , done ;
8279
8271
@@ -8307,32 +8299,25 @@ encode_code_page(int code_page,
8307
8299
done = 1 ;
8308
8300
}
8309
8301
8310
- ret = encode_code_page_strict (code_page , & outbytes ,
8302
+ ret = encode_code_page_strict (code_page , & writer ,
8311
8303
unicode , offset , chunk_len ,
8312
8304
errors );
8313
8305
if (ret == -2 )
8314
- ret = encode_code_page_errors (code_page , & outbytes ,
8306
+ ret = encode_code_page_errors (code_page , & writer ,
8315
8307
unicode , offset ,
8316
8308
chunk_len , errors );
8317
8309
if (ret < 0 ) {
8318
- Py_XDECREF ( outbytes );
8310
+ PyBytesWriter_Discard ( writer );
8319
8311
return NULL ;
8320
8312
}
8321
8313
8322
8314
offset += chunk_len ;
8323
8315
len -= chunk_len ;
8324
8316
} while (!done );
8325
8317
8326
- return outbytes ;
8318
+ return PyBytesWriter_Finish ( writer ) ;
8327
8319
}
8328
8320
8329
- PyObject *
8330
- PyUnicode_EncodeCodePage (int code_page ,
8331
- PyObject * unicode ,
8332
- const char * errors )
8333
- {
8334
- return encode_code_page (code_page , unicode , errors );
8335
- }
8336
8321
8337
8322
PyObject *
8338
8323
PyUnicode_AsMBCSString (PyObject * unicode )
0 commit comments