Skip to content

Commit 79fa5f8

Browse files
committed
Add PyBytesWriter_ResizeAndUpdatePointer() function
1 parent 86d0fd9 commit 79fa5f8

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

Include/cpython/bytesobject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,7 @@ PyAPI_FUNC(int) PyBytesWriter_Format(
7878
PyAPI_FUNC(int) PyBytesWriter_Resize(
7979
PyBytesWriter *writer,
8080
Py_ssize_t size);
81+
PyAPI_FUNC(void*) PyBytesWriter_ResizeAndUpdatePointer(
82+
PyBytesWriter *writer,
83+
Py_ssize_t size,
84+
void *data);

Modules/_pickle.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,11 +2632,10 @@ raw_unicode_escape(PyObject *obj)
26322632
if (ch >= 0x10000) {
26332633
/* -1: subtract 1 preallocated byte */
26342634
alloc += 10-1;
2635-
Py_ssize_t pos = p - (char*)PyBytesWriter_GetData(writer);
2636-
if (PyBytesWriter_Resize(writer, alloc) < 0) {
2635+
p = PyBytesWriter_ResizeAndUpdatePointer(writer, alloc, p);
2636+
if (p == NULL) {
26372637
goto error;
26382638
}
2639-
p = (char*)PyBytesWriter_GetData(writer) + pos;
26402639

26412640
*p++ = '\\';
26422641
*p++ = 'U';
@@ -2656,11 +2655,10 @@ raw_unicode_escape(PyObject *obj)
26562655
{
26572656
/* -1: subtract 1 preallocated byte */
26582657
alloc += 6-1;
2659-
Py_ssize_t pos = p - (char*)PyBytesWriter_GetData(writer);
2660-
if (PyBytesWriter_Resize(writer, alloc) < 0) {
2658+
p = PyBytesWriter_ResizeAndUpdatePointer(writer, alloc, p);
2659+
if (p == NULL) {
26612660
goto error;
26622661
}
2663-
p = (char*)PyBytesWriter_GetData(writer) + pos;
26642662

26652663
*p++ = '\\';
26662664
*p++ = 'u';

Objects/bytesobject.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,10 @@ bytes_fromformat(PyBytesWriter *writer, Py_ssize_t writer_pos,
222222
do { \
223223
size_t len = (len_expr); \
224224
alloc += len; \
225-
Py_ssize_t pos = s - (char*)PyBytesWriter_GetData(writer); \
226-
if (PyBytesWriter_Resize(writer, alloc) < 0) { \
225+
s = PyBytesWriter_ResizeAndUpdatePointer(writer, alloc, s); \
226+
if (s == NULL) { \
227227
goto error; \
228228
} \
229-
s = (char*)PyBytesWriter_GetData(writer) + pos; \
230229
memcpy(s, (str), len); \
231230
s += len; \
232231
} while (0)
@@ -2889,11 +2888,10 @@ _PyBytes_FromList(PyObject *x)
28892888
}
28902889

28912890
if (i >= size) {
2892-
Py_ssize_t pos = str - (char*)PyBytesWriter_GetData(writer);
2893-
if (PyBytesWriter_Resize(writer, size + 1) < 0) {
2891+
str = PyBytesWriter_ResizeAndUpdatePointer(writer, size + 1, str);
2892+
if (str == NULL) {
28942893
goto error;
28952894
}
2896-
str = (char*)PyBytesWriter_GetData(writer) + pos;
28972895
size = PyBytesWriter_GetAllocated(writer);
28982896
}
28992897
*str++ = (char) value;
@@ -3992,3 +3990,15 @@ PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)
39923990
Py_ssize_t size = buf - byteswriter_data(writer);
39933991
return PyBytesWriter_Resize(writer, size);
39943992
}
3993+
3994+
3995+
void*
3996+
PyBytesWriter_ResizeAndUpdatePointer(PyBytesWriter *writer, Py_ssize_t size,
3997+
void *data)
3998+
{
3999+
Py_ssize_t pos = (char*)data - byteswriter_data(writer);
4000+
if (PyBytesWriter_Resize(writer, size) < 0) {
4001+
return NULL;
4002+
}
4003+
return byteswriter_data(writer) + pos;
4004+
}

0 commit comments

Comments
 (0)