int base64WhiteSpace,
const char *errors)
{
- int kind;
- const void *data;
- Py_ssize_t len;
- PyObject *v;
- int inShift = 0;
- Py_ssize_t i;
- unsigned int base64bits = 0;
- unsigned long base64buffer = 0;
- char * out;
- const char * start;
-
- kind = PyUnicode_KIND(str);
- data = PyUnicode_DATA(str);
- len = PyUnicode_GET_LENGTH(str);
-
- if (len == 0)
+ Py_ssize_t len = PyUnicode_GET_LENGTH(str);
+ if (len == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
+ }
+ int kind = PyUnicode_KIND(str);
+ const void *data = PyUnicode_DATA(str);
/* It might be possible to tighten this worst case */
- if (len > PY_SSIZE_T_MAX / 8)
+ if (len > PY_SSIZE_T_MAX / 8) {
return PyErr_NoMemory();
- v = PyBytes_FromStringAndSize(NULL, len * 8);
- if (v == NULL)
+ }
+ PyBytesWriter *writer = PyBytesWriter_Create(len * 8);
+ if (writer == NULL) {
return NULL;
+ }
- start = out = PyBytes_AS_STRING(v);
- for (i = 0; i < len; ++i) {
+ int inShift = 0;
+ unsigned int base64bits = 0;
+ unsigned long base64buffer = 0;
+ char *out = PyBytesWriter_GetData(writer);
+ for (Py_ssize_t i = 0; i < len; ++i) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (inShift) {
*out++= TO_BASE64(base64buffer << (6-base64bits) );
if (inShift)
*out++ = '-';
- if (_PyBytes_Resize(&v, out - start) < 0)
- return NULL;
- return v;
+ return PyBytesWriter_FinishWithPointer(writer, out);
}
#undef IS_BASE64