8000 Don't copy string · matplotlib/matplotlib@1b2a762 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b2a762

Browse files
committed
Don't copy string
1 parent 3f51ef5 commit 1b2a762

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/_path_wrapper.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -674,24 +674,33 @@ static PyObject *Py_convert_to_string(PyObject *self, PyObject *args, PyObject *
674674
}
675675

676676
size_t buffersize = path.total_vertices() * (precision + 5) * 4;
677-
std::string buffer;
678-
buffer.reserve(buffersize);
677+
PyObject *bufferobj = PyBytes_FromStringAndSize(NULL, buffersize);
678+
if (bufferobj == NULL) {
679+
return NULL;
680+
}
681+
char *buffer = PyBytes_AsString(bufferobj);
679682

680683
CALL_CPP("convert_to_string",
681684
(status = convert_to_string(
682685
path, trans, cliprect, simplify, sketch,
683-
precision, codes, (bool)postfix, &buffer[0],
686+
precision, codes, (bool)postfix, buffer,
684687
&buffersize)));
685688

686-
if (status == 1) {
687-
PyErr_SetString(PyExc_MemoryError, "Buffer overflow");
689+
if (status) {
690+
Py_DECREF(bufferobj);
691+
if (status == 1) {
692+
PyErr_SetString(PyExc_MemoryError, "Buffer overflow");
693+
} else if (status == 2) {
694+
PyErr_SetString(PyExc_ValueError, "Malformed path codes");
695+
}
688696
return NULL;
689-
} else if (status == 2) {
690-
PyErr_SetString(PyExc_ValueError, "Malformed path codes");
697+
}
698+
699+
if (_PyBytes_Resize(&bufferobj, buffersize)) {
691700
return NULL;
692701
}
693702

694-
return PyBytes_FromStringAndSize(&buffer[0], buffersize);
703+
return bufferobj;
695704
}
696705

697706
extern "C" {

0 commit comments

Comments
 (0)
0