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

gh-129813, PEP 782: Use PyBytesWriter in pickle and struct by vstinner · Pull Request #138833 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) 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
40 changes: 18 additions & 22 deletions Modules/_pickle.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 @@ -2612,31 +2612,26 @@ save_picklebuffer(PickleState *st, PicklerObject *self, PyObject *obj)
static PyObject *
raw_unicode_escape(PyObject *obj)
{
char *p;
Py_ssize_t i, size;
const void *data;
int kind;
_PyBytesWriter writer;
Py_ssize_t size = PyUnicode_GET_LENGTH(obj);
const void *data = PyUnicode_DATA(obj);
int kind = PyUnicode_KIND(obj);

_PyBytesWriter_Init(&writer);

size = PyUnicode_GET_LENGTH(obj);
data = PyUnicode_DATA(obj);
kind = PyUnicode_KIND(obj);

p = _PyBytesWriter_Alloc(&writer, size);
if (p == NULL)
goto error;
writer.overallocate = 1;
Py_ssize_t alloc = size;
PyBytesWriter *writer = PyBytesWriter_Create(alloc);
if (writer == NULL) {
return NULL;
}
char *p = PyBytesWriter_GetData(writer);

for (i=0; i < size; i++) {
for (Py_ssize_t i=0; i < size; i++) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
/* Map 32-bit characters to '\Uxxxxxxxx' */
if (ch >= 0x10000) {
/* -1: subtract 1 preallocated byte */
p = _PyBytesWriter_Prepare(&writer, p, 10-1);
if (p == NULL)
p = PyBytesWriter_GrowAndUpdatePointer(writer, 10-1, p);
if (p == NULL) {
goto error;
}

*p++ = '\\';
*p++ = 'U';
Expand All @@ -2655,9 +2650,10 @@ raw_unicode_escape(PyObject *obj)
ch == 0x1a)
{
/* -1: subtract 1 preallocated byte */
p = _PyBytesWriter_Prepare(&writer, p, 6-1);
if (p == NULL)
p = PyBytesWriter_GrowAndUpdatePointer(writer, 6-1, p);
if (p == NULL) {
goto error;
}

*p++ = '\\';
*p++ = 'u';
Expand All @@ -2671,10 +2667,10 @@ raw_unicode_escape(PyObject *obj)
*p++ = (char) ch;
}

return _PyBytesWriter_Finish(&writer, p);
return PyBytesWriter_FinishWithPointer(writer, p);

error:
_PyBytesWriter_Dealloc(&writer);
PyBytesWriter_Discard(writer);
return NULL;
}

Expand Down
13 changes: 5 additions & 8 deletions Modules/_struct.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 @@ -2189,7 +2189,6 @@ strings.");
static PyObject *
s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{
char *buf;
PyStructObject *soself;
_structmodulestate *state = get_struct_state_structinst(self);

Expand All @@ -2205,21 +2204,19 @@ s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
}

/* Allocate a new string */
_PyBytesWriter writer;
_PyBytesWriter_Init(&writer);
buf = _PyBytesWriter_Alloc(&writer, soself->s_size);
if (buf == NULL) {
_PyBytesWriter_Dealloc(&writer);
PyBytesWriter *writer = PyBytesWriter_Create(soself->s_size);
if (writer == NULL) {
return NULL;
}
char *buf = PyBytesWriter_GetData(writer);

/* Call the guts */
if ( s_pack_internal(soself, args, 0, buf, state) != 0 ) {
_PyBytesWriter_Dealloc(&writer);
PyBytesWriter_Discard(writer);
return NULL;
}

return _PyBytesWriter_Finish(&writer, buf + soself->s_size);
return PyBytesWriter_FinishWithSize(writer, soself->s_size);
}

PyDoc_STRVAR(s_pack_into__doc__,
Expand Down
Loading

Back | FazBrowse Home | New Git URL