summaryrefslogtreecommitdiffstats
path: root/Modules/_pickle.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-08-17 19:14:46 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-08-17 19:14:46 (GMT)
commit7270b7f1aae31ba04885aa5e400e6542a20c789e (patch)
treedaf79fc548ae089b1003a91fbcd323cf287bafc4 /Modules/_pickle.c
parent88d146b7b9cece5c3e2e85feedce3417a589e8c3 (diff)
downloadcpython-7270b7f1aae31ba04885aa5e400e6542a20c789e.zip
cpython-7270b7f1aae31ba04885aa5e400e6542a20c789e.tar.gz
cpython-7270b7f1aae31ba04885aa5e400e6542a20c789e.tar.bz2
_pickle: Optimize raw_unicode_escape(), use directly a bytes object, don't use
a temporary bytearray object.
Diffstat (limited to 'Modules/_pickle.c')
-rw-r--r--Modules/_pickle.c19
1 files changed, 9 insertions, 10 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 665a324..1c15190 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -2050,7 +2050,7 @@ save_bytes(PicklerObject *self, PyObject *obj)
static PyObject *
raw_unicode_escape(PyObject *obj)
{
- PyObject *repr, *result;
+ PyObject *repr;
char *p;
Py_ssize_t i, size, expandsize;
void *data;
@@ -2069,13 +2069,14 @@ raw_unicode_escape(PyObject *obj)
if (size > PY_SSIZE_T_MAX / expandsize)
return PyErr_NoMemory();
- repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
+ repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
if (repr == NULL)
return NULL;
if (size == 0)
- goto done;
+ return repr;
+ assert(Py_REFCNT(repr) == 1);
- p = PyByteArray_AS_STRING(repr);
+ p = PyBytes_AS_STRING(repr);
for (i=0; i < size; i++) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
/* Map 32-bit characters to '\Uxxxxxxxx' */
@@ -2104,12 +2105,10 @@ raw_unicode_escape(PyObject *obj)
else
*p++ = (char) ch;
}
- size = p - PyByteArray_AS_STRING(repr);
-
-done:
- result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
- Py_DECREF(repr);
- return result;
+ size = p - PyBytes_AS_STRING(repr);
+ if (_PyBytes_Resize(&repr, size) < 0)
+ return NULL;
+ return repr;
}
static int