int alternate);
extern char* _PyLong_FormatBytesWriter(
- _PyBytesWriter *writer,
+ PyBytesWriter *writer,
char *str,
PyObject *obj,
int base,
static char*
formatfloat(PyObject *v, int flags, int prec, int type,
- PyObject **p_result, _PyBytesWriter *writer, char *str)
+ PyObject **p_result, PyBytesWriter *writer, char *str)
{
char *p;
PyObject *result;
len = strlen(p);
if (writer != NULL) {
- str = _PyBytesWriter_Prepare(writer, str, len);
+ str = PyBytesWriter_GrowAndUpdatePointer(writer, len, str);
if (str == NULL) {
PyMem_Free(p);
return NULL;
PyObject *args, int use_bytearray)
{
const char *fmt;
- char *res;
Py_ssize_t arglen, argidx;
Py_ssize_t fmtcnt;
int args_owned = 0;
PyObject *dict = NULL;
- _PyBytesWriter writer;
if (args == NULL) {
PyErr_BadInternalCall();
fmt = format;
fmtcnt = format_len;
- _PyBytesWriter_Init(&writer);
- writer.use_bytearray = use_bytearray;
-
- res = _PyBytesWriter_Alloc(&writer, fmtcnt);
- if (res == NULL)
+ PyBytesWriter *writer;
+ if (use_bytearray) {
+ writer = _PyBytesWriter_CreateByteArray(fmtcnt);
+ }
+ else {
+ writer = PyBytesWriter_Create(fmtcnt);
+ }
+ if (writer == NULL) {
return NULL;
- if (!use_bytearray)
- writer.overallocate = 1;
+ }
+ char *res = PyBytesWriter_GetData(writer);
if (PyTuple_Check(args)) {
arglen = PyTuple_GET_SIZE(args);
if (v == NULL)
goto error;
- if (fmtcnt == 0) {
- /* last write: disable writer overallocation */
- writer.overallocate = 0;
- }
-
sign = 0;
fill = ' ';
switch (c) {
}
/* Fast path */
- writer.min_size -= 2; /* size preallocated for "%d" */
- res = _PyLong_FormatBytesWriter(&writer, res,
+ res = _PyLong_FormatBytesWriter(writer, res,
v, base, alternate);
if (res == NULL)
goto error;
&& !(flags & (F_SIGN | F_BLANK)))
{
/* Fast path */
- writer.min_size -= 2; /* size preallocated for "%f" */
- res = formatfloat(v, flags, prec, c, NULL, &writer, res);
+ res = formatfloat(v, flags, prec, c, NULL, writer, res);
if (res == NULL)
goto error;
continue;
alloc++;
/* 2: size preallocated for %s */
if (alloc > 2) {
- res = _PyBytesWriter_Prepare(&writer, res, alloc - 2);
- if (res == NULL)
+ res = PyBytesWriter_GrowAndUpdatePointer(writer, alloc - 2, res);
+ if (res == NULL) {
goto error;
+ }
}
#ifndef NDEBUG
char *before = res;
assert((res - before) == alloc);
#endif
} /* '%' */
-
- /* If overallocation was disabled, ensure that it was the last
- write. Otherwise, we missed an optimization */
- assert(writer.overallocate || fmtcnt == 0 || use_bytearray);
} /* until end */
if (argidx < arglen && !dict) {
if (args_owned) {
Py_DECREF(args);
}
- return _PyBytesWriter_Finish(&writer, res);
+ return PyBytesWriter_FinishWithPointer(writer, res);
error:
- _PyBytesWriter_Dealloc(&writer);
+ PyBytesWriter_Discard(writer);
if (args_owned) {
Py_DECREF(args);
}
pylong_int_to_decimal_string(PyObject *aa,
PyObject **p_output,
_PyUnicodeWriter *writer,
- _PyBytesWriter *bytes_writer,
+ PyBytesWriter *bytes_writer,
char **bytes_str)
{
PyObject *s = NULL;
Py_ssize_t size = PyUnicode_GET_LENGTH(s);
const void *data = PyUnicode_DATA(s);
int kind = PyUnicode_KIND(s);
- *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, size);
+ *bytes_str = PyBytesWriter_GrowAndUpdatePointer(bytes_writer, size,
+ *bytes_str);
if (*bytes_str == NULL) {
goto error;
}
long_to_decimal_string_internal(PyObject *aa,
PyObject **p_output,
_PyUnicodeWriter *writer,
- _PyBytesWriter *bytes_writer,
+ PyBytesWriter *bytes_writer,
char **bytes_str)
{
PyLongObject *scratch, *a;
}
}
else if (bytes_writer) {
- *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
+ *bytes_str = PyBytesWriter_GrowAndUpdatePointer(bytes_writer, strlen,
+ *bytes_str);
if (*bytes_str == NULL) {
Py_DECREF(scratch);
return -1;
static int
long_format_binary(PyObject *aa, int base, int alternate,
PyObject **p_output, _PyUnicodeWriter *writer,
- _PyBytesWriter *bytes_writer, char **bytes_str)
+ PyBytesWriter *bytes_writer, char **bytes_str)
{
PyLongObject *a = (PyLongObject *)aa;
PyObject *v = NULL;
return -1;
}
else if (bytes_writer) {
- *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
+ *bytes_str = PyBytesWriter_GrowAndUpdatePointer(bytes_writer, sz,
+ *bytes_str);
if (*bytes_str == NULL)
return -1;
}
}
char*
-_PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str,
+_PyLong_FormatBytesWriter(PyBytesWriter *writer, char *str,
PyObject *obj,
int base, int alternate)
{
/*[clinic end generated code: output=89c801df114050a3 input=66f9d0c20529b44f]*/
{
int little_endian;
- PyObject *bytes;
-
if (byteorder == NULL)
little_endian = 0;
else if (_PyUnicode_Equal(byteorder, &_Py_ID(little)))
return NULL;
}
- bytes = PyBytes_FromStringAndSize(NULL, length);
- if (bytes == NULL)
+ PyBytesWriter *writer = PyBytesWriter_Create(length);
+ if (writer == NULL) {
return NULL;
+ }
if (_PyLong_AsByteArray((PyLongObject *)self,
- (unsigned char *)PyBytes_AS_STRING(bytes),
+ PyBytesWriter_GetData(writer),
length, little_endian, is_signed, 1) < 0) {
- Py_DECREF(bytes);
+ PyBytesWriter_Discard(writer);
return NULL;
}
- return bytes;
+ return PyBytesWriter_Finish(writer);
}
/*[clinic input]