Skip to content

Commit de119b3

Browse files
committed
gh-155742: Use PyBytesWriter in marshal
Replace soft deprecated PyBytes_FromStringAndSize() and _PyBytes_Resize() with PyBytesWriter.
1 parent cd98657 commit de119b3

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

Python/marshal.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ typedef struct {
111111
FILE *fp;
112112
int error; /* see WFERR_* values */
113113
int depth;
114-
PyObject *str;
114+
PyBytesWriter *writer;
115115
char *ptr;
116116
const char *end;
117117
char *buf;
@@ -136,16 +136,16 @@ w_flush(WFILE *p)
136136
static int
137137
w_reserve(WFILE *p, Py_ssize_t needed)
138138
{
139-
Py_ssize_t pos, size, delta;
140139
if (p->ptr == NULL)
141140
return 0; /* An error already occurred */
142141
if (p->fp != NULL) {
143142
w_flush(p);
144143
return needed <= p->end - p->ptr;
145144
}
146-
assert(p->str != NULL);
147-
pos = p->ptr - p->buf;
148-
size = PyBytes_GET_SIZE(p->str);
145+
assert(p->writer != NULL);
146+
Py_ssize_t pos = p->ptr - p->buf;
147+
Py_ssize_t size = PyBytesWriter_GetSize(p->writer);
148+
Py_ssize_t delta;
149149
if (size > 16*1024*1024)
150150
delta = (size >> 3); /* 12.5% overallocation */
151151
else
@@ -156,12 +156,12 @@ w_reserve(WFILE *p, Py_ssize_t needed)
156156
return 0;
157157
}
158158
size += delta;
159-
if (_PyBytes_Resize(&p->str, size) != 0) {
159+
if (PyBytesWriter_Resize(p->writer, size) != 0) {
160160
p->end = p->ptr = p->buf = NULL;
161161
return 0;
162162
}
163163
else {
164-
p->buf = PyBytes_AS_STRING(p->str);
164+
p->buf = PyBytesWriter_GetData(p->writer);
165165
p->ptr = p->buf + pos;
166166
p->end = p->buf + size;
167167
return 1;
@@ -1308,16 +1308,17 @@ r_object(RFILE *p)
13081308
}
13091309
break;
13101310
}
1311-
v = PyBytes_FromStringAndSize((char *)NULL, n);
1312-
if (v == NULL)
1311+
PyBytesWriter *writer = PyBytesWriter_Create(n);
1312+
if (writer == NULL) {
13131313
break;
1314+
}
13141315
ptr = r_string(n, p);
13151316
if (ptr == NULL) {
1316-
Py_DECREF(v);
1317+
PyBytesWriter_Discard(writer);
13171318
break;
13181319
}
1319-
memcpy(PyBytes_AS_STRING(v), ptr, n);
1320-
retval = v;
1320+
memcpy(PyBytesWriter_GetData(writer), ptr, n);
1321+
retval = PyBytesWriter_Finish(writer); // can be NULL
13211322
R_REF(retval);
13221323
break;
13231324
}
@@ -1908,27 +1909,30 @@ _PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code)
19081909
return NULL;
19091910
}
19101911
memset(&wf, 0, sizeof(wf));
1911-
wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1912-
if (wf.str == NULL)
1912+
wf.writer = PyBytesWriter_Create(50);
1913+
if (wf.writer == NULL) {
19131914
return NULL;
1914-
wf.ptr = wf.buf = PyBytes_AS_STRING(wf.str);
1915-
wf.end = wf.ptr + PyBytes_GET_SIZE(wf.str);
1915+
}
1916+
wf.ptr = wf.buf = PyBytesWriter_GetData(wf.writer);
1917+
wf.end = wf.ptr + PyBytesWriter_GetSize(wf.writer);
19161918
wf.error = WFERR_OK;
19171919
wf.version = version;
19181920
wf.allow_code = allow_code;
19191921
if (w_init_refs(&wf, version)) {
1920-
Py_DECREF(wf.str);
1922+
PyBytesWriter_Discard(wf.writer);
19211923
return NULL;
19221924
}
19231925
w_object(x, &wf);
19241926
w_clear_refs(&wf);
1925-
if (wf.str != NULL) {
1926-
const char *base = PyBytes_AS_STRING(wf.str);
1927-
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
1927+
if (wf.writer != NULL) {
1928+
const char *base = PyBytesWriter_GetData(wf.writer);
1929+
if (PyBytesWriter_Resize(wf.writer, (Py_ssize_t)(wf.ptr - base)) < 0) {
1930+
PyBytesWriter_Discard(wf.writer);
19281931
return NULL;
1932+
}
19291933
}
19301934
if (wf.error != WFERR_OK) {
1931-
Py_XDECREF(wf.str);
1935+
PyBytesWriter_Discard(wf.writer);
19321936
switch (wf.error) {
19331937
case WFERR_NOMEMORY:
19341938
PyErr_NoMemory();
@@ -1949,7 +1953,7 @@ _PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code)
19491953
}
19501954
return NULL;
19511955
}
1952-
return wf.str;
1956+
return PyBytesWriter_Finish(wf.writer);
19531957
}
19541958

19551959
PyObject *

0 commit comments

Comments
 (0)