FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-155742: Use PyBytesWriter in marshal by vstinner · Pull Request #155748 · python/cpython · GitHub

Repository navigation

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
52 changes: 35 additions & 17 deletions Python/marshal.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ typedef struct {
FILE *fp;
int error; /* see WFERR_* values */
int depth;
PyObject *str;
PyBytesWriter *writer;
char *ptr;
const char *end;
char *buf;
Expand Down Expand Up @@ -158,16 +158,16 @@ w_flush(WFILE *p)
static int
w_reserve(WFILE *p, Py_ssize_t needed)
{
Py_ssize_t pos, size, delta;
if (p->ptr == NULL)
return 0; /* An error already occurred */
if (p->fp != NULL) {
w_flush(p);
return needed <= p->end - p->ptr;
}
assert(p->str != NULL);
pos = p->ptr - p->buf;
size = PyBytes_GET_SIZE(p->str);
assert(p->writer != NULL);
Py_ssize_t pos = p->ptr - p->buf;
Py_ssize_t size = PyBytesWriter_GetSize(p->writer);
Py_ssize_t delta;
if (size > 16*1024*1024)
delta = (size >> 3); /* 12.5% overallocation */
else
Expand All @@ -178,12 +178,14 @@ w_reserve(WFILE *p, Py_ssize_t needed)
return 0;
}
size += delta;
if (_PyBytes_Resize(&p->str, size) != 0) {
if (PyBytesWriter_Resize(p->writer, size) != 0) {
Comment thread
vstinner marked this conversation as resolved.
PyBytesWriter_Discard(p->writer);
p->writer = NULL;
p->end = p->ptr = p->buf = NULL;
return 0;
}
else {
p->buf = PyBytes_AS_STRING(p->str);
p->buf = PyBytesWriter_GetData(p->writer);
p->ptr = p->buf + pos;
p->end = p->buf + size;
return 1;
Expand Down Expand Up @@ -2000,31 +2002,47 @@ _PyMarshal_WriteObjectToString(PyObject *x, int version, int allow_code)
return NULL;
}
memset(&wf, 0, sizeof(wf));
wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
if (wf.str == NULL)
wf.writer = PyBytesWriter_Create(50);
if (wf.writer == NULL) {
return NULL;
wf.ptr = wf.buf = PyBytes_AS_STRING(wf.str);
wf.end = wf.ptr + PyBytes_GET_SIZE(wf.str);
}
wf.writer->overallocate = 0;
wf.ptr = wf.buf = PyBytesWriter_GetData(wf.writer);
wf.end = wf.ptr + PyBytesWriter_GetSize(wf.writer);
wf.error = WFERR_OK;
wf.version = version;
wf.allow_code = allow_code;
if (w_init_refs(&wf, version)) {
Py_DECREF(wf.str);
PyBytesWriter_Discard(wf.writer);
return NULL;
}
w_object(x, &wf);
w_clear_refs(&wf);
if (wf.str != NULL) {
const char *base = PyBytes_AS_STRING(wf.str);
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)

if (wf.writer != NULL) {
Comment thread
vstinner marked this conversation as resolved.
assert(wf.ptr != NULL);
const char *base = PyBytesWriter_GetData(wf.writer);
if (PyBytesWriter_Resize(wf.writer, (Py_ssize_t)(wf.ptr - base)) < 0) {
PyBytesWriter_Discard(wf.writer);
// PyBytesWriter_Resize() sets an exception
assert(PyErr_Occurred());
return NULL;
}
}

if (wf.error != WFERR_OK) {
Py_XDECREF(wf.str);
PyBytesWriter_Discard(wf.writer);
w_set_exception(&wf);
return NULL;
}
return wf.str;

if (wf.writer == NULL) {
// In w_reserve(), PyBytesWriter_Resize() failed with an exception set
assert(PyErr_Occurred());
return NULL;
}

return PyBytesWriter_Finish(wf.writer);
}

PyObject *
Expand Down
Loading

Back | FazBrowse Home | New Git URL