8000 PyUnicodeWriter_Create() expects a length · python/cpython@b12f085 · GitHub
[go: up one dir, main page]

Skip to content

Commit b12f085

Browse files
committed
PyUnicodeWriter_Create() expects a length
Remove PyUnicodeWriter_SetOverallocate().
1 parent 3c4da2e commit b12f085

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

Include/cpython/unicodeobject.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,10 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
448448

449449
typedef struct PyUnicodeWriter PyUnicodeWriter;
450450

451-
PyAPI_FUNC(PyUnicodeWriter*) PyUnicodeWriter_Create(void);
451+
PyAPI_FUNC(PyUnicodeWriter*) PyUnicodeWriter_Create(Py_ssize_t length);
452452
PyAPI_FUNC(void) PyUnicodeWriter_Discard(PyUnicodeWriter *writer);
453453
PyAPI_FUNC(PyObject*) PyUnicodeWriter_Finish(PyUnicodeWriter *writer);
454454

455-
PyAPI_FUNC(void) PyUnicodeWriter_SetOverallocate(
456-
PyUnicodeWriter *writer,
457-
int overallocate);
458-
459455
PyAPI_FUNC(int) PyUnicodeWriter_WriteChar(
460456
PyUnicodeWriter *writer,
461457
Py_UCS4 ch);

Modules/_testcapi/unicode.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,11 @@ unicode_copycharacters(PyObject *self, PyObject *args)
224224
static PyObject *
225225
test_unicodewriter(PyObject *self, PyObject *Py_UNUSED(args))
226226
{
227-
PyUnicodeWriter *writer = PyUnicodeWriter_Create();
227+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(100);
228228
if (writer == NULL) {
229229
return NULL;
230230
}
231231

232-
// test PyUnicodeWriter_SetOverallocate()
233-
PyUnicodeWriter_SetOverallocate(writer, 1);
234-
235232
// test PyUnicodeWriter_WriteUTF8()
236233
if (PyUnicodeWriter_WriteUTF8(writer, "var", -1) < 0) {
237234
goto error;
@@ -293,7 +290,7 @@ test_unicodewriter(PyObject *self, PyObject *Py_UNUSED(args))
293290
static PyObject *
294291
test_unicodewriter_utf8(PyObject *self, PyObject *Py_UNUSED(args))
295292
{
296-
PyUnicodeWriter *writer = PyUnicodeWriter_Create();
293+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
297294
if (writer == NULL) {
298295
return NULL;
299296
}
@@ -335,7 +332,7 @@ test_unicodewriter_utf8(PyObject *self, PyObject *Py_UNUSED(args))
335332
static PyObject *
336333
test_unicodewriter_invalid_utf8(PyObject *self, PyObject *Py_UNUSED(args))
337334
{
338-
PyUnicodeWriter *writer = PyUnicodeWriter_Create();
335+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
339336
if (writer == NULL) {
340337
return NULL;
341338
}
@@ -352,7 +349,7 @@ test_unicodewriter_invalid_utf8(PyObject *self, PyObject *Py_UNUSED(args))
352349
static PyObject *
353350
test_unicodewriter_format(PyObject *self, PyObject *Py_UNUSED(args))
354351
{
355-
PyUnicodeWriter *writer = PyUnicodeWriter_Create();
352+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
356353
if (writer == NULL) {
357354
return NULL;
358355
}

Objects/unicodeobject.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13139,22 +13139,29 @@ _PyUnicodeWriter_Init(_PyUnicodeWriter *writer)
1313913139
/* use a kind value smaller than PyUnicode_1BYTE_KIND so
1314013140
_PyUnicodeWriter_PrepareKind() will copy the buffer. */
1314113141
assert(writer->kind == 0);
13142-
assert(writer->kind <= PyUnicode_1BYTE_KIND);
13142+
assert(writer->kind < PyUnicode_1BYTE_KIND);
1314313143
}
1314413144

1314513145

1314613146
PyUnicodeWriter*
13147-
PyUnicodeWriter_Create(void)
13147+
PyUnicodeWriter_Create(Py_ssize_t length)
1314813148
{
1314913149
const size_t size = sizeof(_PyUnicodeWriter);
13150-
PyUnicodeWriter *writer = (PyUnicodeWriter *)PyMem_Malloc(size);
13151-
if (writer == NULL) {
13150+
PyUnicodeWriter *pub_writer = (PyUnicodeWriter *)PyMem_Malloc(size);
13151+
if (pub_writer == NULL) {
1315213152
PyErr_NoMemory();
1315313153
return NULL;
1315413154
}
13155-
_PyUnicodeWriter_Init((_PyUnicodeWriter*)writer);
13156-
PyUnicodeWriter_SetOverallocate(writer, 1);
13157-
return writer;
13155+
_PyUnicodeWriter *writer = (_PyUnicodeWriter *)pub_writer;
13156+
13157+
_PyUnicodeWriter_Init(writer);
13158+
if (_PyUnicodeWriter_Prepare(writer, length, 127) < 0) {
13159+
PyUnicodeWriter_Discard(pub_writer);
13160+
return NULL;
13161+
}
13162+
writer->overallocate = 1;
13163+
13164+
return pub_writer;
1315813165
}
1315913166

1316013167

@@ -13176,13 +13183,6 @@ _PyUnicodeWriter_InitWithBuffer(_PyUnicodeWriter *writer, PyObject *buffer)
1317613183
}
1317713184

1317813185

13179-
void
13180-
PyUnicodeWriter_SetOverallocate(PyUnicodeWriter *writer, int overallocate)
13181-
{
13182-
((_PyUnicodeWriter*)writer)->overallocate = (unsigned char)overallocate;
13183-
}
13184-
13185-
1318613186
int
1318713187
_PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
1318813188
Py_ssize_t length, Py_UCS4 maxchar)

0 commit comments

Comments
 (0)
0