Skip to content

Commit 4cf02fa

Browse files
committed
_PyUnicodeWriter_Update() sets kind for read-only
1 parent 6d4f8f6 commit 4cf02fa

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

‎Include/internal/pycore_unicodeobject.h‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ _PyUnicode_EnsureUnicode(PyObject *obj)
112112
static inline int
113113
_PyUnicodeWriter_CanWrite(_PyUnicodeWriter *writer)
114114
{
115-
// Code adapted from _PyUnicode_IsModifiable()
116115
assert(!writer->readonly);
116+
117117
PyObject *buffer = writer->buffer;
118118
assert(buffer != NULL);
119+
120+
// Code adapted from _PyUnicode_IsModifiable().
119121
// Do not use _PyObject_IsUniquelyReferenced(): the caller can have its own
120122
// lock to prevent a writer from being used by two threads at the same
121123
// time.
@@ -133,17 +135,12 @@ _PyUnicodeWriter_Update(_PyUnicodeWriter *writer)
133135
PyObject *buffer = writer->buffer;
134136
writer->maxchar = PyUnicode_MAX_CHAR_VALUE(buffer);
135137
writer->data = PyUnicode_DATA(buffer);
138+
writer->kind = PyUnicode_KIND(buffer);
136139

137140
if (!writer->readonly) {
138-
writer->kind = PyUnicode_KIND(buffer);
139141
writer->size = PyUnicode_GET_LENGTH(buffer);
140142
}
141143
else {
142-
/* use a value smaller than PyUnicode_1BYTE_KIND() so
143-
_PyUnicodeWriter_PrepareKind() will copy the buffer. */
144-
writer->kind = 0;
145-
assert(writer->kind < PyUnicode_1BYTE_KIND);
146-
147144
/* Copy-on-write mode: set buffer size to 0 so
148145
* _PyUnicodeWriter_Prepare() will copy (and enlarge) the buffer on
149146
* next write. */

‎Objects/unicodeobject.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5230,6 +5230,7 @@ unicode_decode_utf8_impl(_PyUnicodeWriter *writer,
52305230

52315231
if (_PyUnicodeWriter_PrepareKind(writer, PyUnicode_2BYTE_KIND) < 0)
52325232
goto onError;
5233+
assert(_PyUnicodeWriter_CanWrite(writer));
52335234
for (i=startinpos; i<endinpos; i++) {
52345235
ch = (Py_UCS4)(unsigned char)(starts[i]);
52355236
PyUnicode_WRITE(writer->kind, writer->data, writer->pos,
@@ -7472,6 +7473,7 @@ PyUnicode_DecodeASCII(const char *s,
74727473
but we may switch to UCS2 at the first write */
74737474
if (_PyUnicodeWriter_PrepareKind(&writer, PyUnicode_2BYTE_KIND) < 0)
74747475
goto onError;
7476+
assert(_PyUnicodeWriter_CanWrite(&writer));
74757477
kind = writer.kind;
74767478
data = writer.data;
74777479

0 commit comments

Comments
 (0)