* an OSError and returns -1 on other error.
*/
static int
-encode_code_page_strict(UINT code_page, PyObject **outbytes,
+encode_code_page_strict(UINT code_page, PyBytesWriter **writer,
PyObject *unicode, Py_ssize_t offset, int len,
const char* errors)
{
goto done;
}
- if (*outbytes == NULL) {
+ if (*writer == NULL) {
/* Create string object */
- *outbytes = PyBytes_FromStringAndSize(NULL, outsize);
- if (*outbytes == NULL) {
+ *writer = PyBytesWriter_Create(outsize);
+ if (*writer == NULL) {
goto done;
}
- out = PyBytes_AS_STRING(*outbytes);
+ out = PyBytesWriter_GetData(*writer);
}
else {
/* Extend string object */
- const Py_ssize_t n = PyBytes_Size(*outbytes);
- if (outsize > PY_SSIZE_T_MAX - n) {
- PyErr_NoMemory();
- goto done;
- }
- if (_PyBytes_Resize(outbytes, n + outsize) < 0) {
+ Py_ssize_t n = PyBytesWriter_GetSize(*writer);
+ if (PyBytesWriter_Grow(*writer, outsize) < 0) {
goto done;
}
- out = PyBytes_AS_STRING(*outbytes) + n;
+ out = (char*)PyBytesWriter_GetData(*writer) + n;
}
/* Do the conversion */
* -1 on other error.
*/
static int
-encode_code_page_errors(UINT code_page, PyObject **outbytes,
+encode_code_page_errors(UINT code_page, PyBytesWriter **writer,
PyObject *unicode, Py_ssize_t unicode_offset,
Py_ssize_t insize, const char* errors)
{
PyObject *exc = NULL;
PyObject *encoding_obj = NULL;
const char *encoding;
- Py_ssize_t newpos, newoutsize;
+ Py_ssize_t newpos;
PyObject *rep;
int ret = -1;
}
outsize = insize * Py_ARRAY_LENGTH(buffer);
- if (*outbytes == NULL) {
+ if (*writer == NULL) {
/* Create string object */
- *outbytes = PyBytes_FromStringAndSize(NULL, outsize);
- if (*outbytes == NULL)
+ *writer = PyBytesWriter_Create(outsize);
+ if (*writer == NULL) {
goto error;
- out = PyBytes_AS_STRING(*outbytes);
+ }
+ out = PyBytesWriter_GetData(*writer);
}
else {
/* Extend string object */
- Py_ssize_t n = PyBytes_Size(*outbytes);
- if (n > PY_SSIZE_T_MAX - outsize) {
- PyErr_NoMemory();
+ Py_ssize_t n = PyBytesWriter_GetSize(*writer);
+ if (PyBytesWriter_Grow(*writer, outsize) < 0) {
goto error;
}
- if (_PyBytes_Resize(outbytes, n + outsize) < 0)
- goto error;
- out = PyBytes_AS_STRING(*outbytes) + n;
+ out = (char*)PyBytesWriter_GetData(*writer) + n;
}
/* Encode the string character per character */
outsize = PyBytes_GET_SIZE(rep);
morebytes += outsize;
if (morebytes > 0) {
- Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes);
- newoutsize = PyBytes_GET_SIZE(*outbytes) + morebytes;
- if (_PyBytes_Resize(outbytes, newoutsize) < 0) {
+ out = PyBytesWriter_GrowAndUpdatePointer(*writer, morebytes, out);
+ if (out == NULL) {
Py_DECREF(rep);
goto error;
}
- out = PyBytes_AS_STRING(*outbytes) + offset;
}
memcpy(out, PyBytes_AS_STRING(rep), outsize);
out += outsize;
outsize = PyUnicode_GET_LENGTH(rep);
morebytes += outsize;
if (morebytes > 0) {
- Py_ssize_t offset = out - PyBytes_AS_STRING(*outbytes);
- newoutsize = PyBytes_GET_SIZE(*outbytes) + morebytes;
- if (_PyBytes_Resize(outbytes, newoutsize) < 0) {
+ out = PyBytesWriter_GrowAndUpdatePointer(*writer, morebytes, out);
+ if (out == NULL) {
Py_DECREF(rep);
goto error;
}
- out = PyBytes_AS_STRING(*outbytes) + offset;
}
kind = PyUnicode_KIND(rep);
data = PyUnicode_DATA(rep);
}
/* write a NUL byte */
*out = 0;
- outsize = out - PyBytes_AS_STRING(*outbytes);
- assert(outsize <= PyBytes_GET_SIZE(*outbytes));
- if (_PyBytes_Resize(outbytes, outsize) < 0)
+ outsize = out - (char*)PyBytesWriter_GetData(*writer);
+ assert(outsize <= PyBytesWriter_GetSize(*writer));
+ if (PyBytesWriter_Resize(*writer, outsize) < 0) {
goto error;
+ }
ret = 0;
error:
return ret;
}
-static PyObject *
-encode_code_page(int code_page,
- PyObject *unicode,
- const char *errors)
+
+PyObject *
+PyUnicode_EncodeCodePage(int code_page,
+ PyObject *unicode,
+ const char *errors)
{
Py_ssize_t len;
- PyObject *outbytes = NULL;
+ PyBytesWriter *writer = NULL;
Py_ssize_t offset;
int chunk_len, ret, done;
done = 1;
}
- ret = encode_code_page_strict(code_page, &outbytes,
+ ret = encode_code_page_strict(code_page, &writer,
unicode, offset, chunk_len,
errors);
if (ret == -2)
- ret = encode_code_page_errors(code_page, &outbytes,
+ ret = encode_code_page_errors(code_page, &writer,
unicode, offset,
chunk_len, errors);
if (ret < 0) {
- Py_XDECREF(outbytes);
+ PyBytesWriter_Discard(writer);
return NULL;
}
len -= chunk_len;
} while (!done);
- return outbytes;
+ return PyBytesWriter_Finish(writer);
}
-PyObject *
-PyUnicode_EncodeCodePage(int code_page,
- PyObject *unicode,
- const char *errors)
-{
- return encode_code_page(code_page, unicode, errors);
-}
PyObject *
PyUnicode_AsMBCSString(PyObject *unicode)