@@ -674,24 +674,33 @@ static PyObject *Py_convert_to_string(PyObject *self, PyObject *args, PyObject *
674
674
}
675
675
676
676
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);
679
682
680
683
CALL_CPP (" convert_to_string" ,
681
684
(status = convert_to_string (
682
685
path, trans, cliprect, simplify, sketch,
683
- precision, codes, (bool )postfix, & buffer[ 0 ] ,
686
+ precision, codes, (bool )postfix, buffer,
684
687
&buffersize)));
685
688
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
+ }
688
696
return NULL ;
689
- } else if (status == 2 ) {
690
- PyErr_SetString (PyExc_ValueError, " Malformed path codes" );
697
+ }
698
+
699
+ if (_PyBytes_Resize (&bufferobj, buffersize)) {
691
700
return NULL ;
692
701
}
693
702
694
- return PyBytes_FromStringAndSize (&buffer[ 0 ], buffersize) ;
703
+ return bufferobj ;
695
704
}
696
705
697
706
extern " C" {
0 commit comments