diff --git a/src/calibre/utils/podofo/doc.cpp b/src/calibre/utils/podofo/doc.cpp index c169f80719..d9cef4c423 100644 --- a/src/calibre/utils/podofo/doc.cpp +++ b/src/calibre/utils/podofo/doc.cpp @@ -293,18 +293,18 @@ PDFDoc_set_box(PDFDoc *self, PyObject *args) { static PyObject * PDFDoc_create_outline(PDFDoc *self, PyObject *args) { PDFOutlineItem *ans; - char *title_buf; + PyObject *title_buf; unsigned int pagenum; double left = 0, top = 0, zoom = 0; PdfPage *page; - if (!PyArg_ParseTuple(args, "esI|ddd", "UTF-8", &title_buf, &pagenum, &left, &top, &zoom)) return NULL; + if (!PyArg_ParseTuple(args, "UI|ddd", &title_buf, &pagenum, &left, &top, &zoom)) return NULL; ans = PyObject_New(PDFOutlineItem, &PDFOutlineItemType); if (ans == NULL) goto error; try { - PdfString title(reinterpret_cast(title_buf)); + PdfString title = podofo_convert_pystring(title_buf); PdfOutlines *outlines = self->doc->GetOutlines(); if (outlines == NULL) {PyErr_NoMemory(); goto error;} ans->item = outlines->CreateRoot(title); diff --git a/src/calibre/utils/podofo/outline.cpp b/src/calibre/utils/podofo/outline.cpp index 3a337492f2..134198dc0e 100644 --- a/src/calibre/utils/podofo/outline.cpp +++ b/src/calibre/utils/podofo/outline.cpp @@ -49,16 +49,16 @@ create(PDFOutlineItem *self, PyObject *args) { unsigned int num; double left = 0, top = 0, zoom = 0; PdfPage *page; - char *title_buf; + PyObject *title_buf; - if (!PyArg_ParseTuple(args, "esIO|ddd", "UTF-8", &title_buf, &num, &as_child, &left, &top, &zoom)) return NULL; + if (!PyArg_ParseTuple(args, "UIO|ddd", &title_buf, &num, &as_child, &left, &top, &zoom)) return NULL; ans = PyObject_New(PDFOutlineItem, &PDFOutlineItemType); if (ans == NULL) goto error; ans->doc = self->doc; try { - PdfString title(reinterpret_cast(title_buf)); + PdfString title = podofo_convert_pystring(title_buf); try { page = self->doc->GetPage(num - 1); } catch(const PdfError &err) { page = NULL; } diff --git a/src/calibre/utils/podofo/utils.cpp b/src/calibre/utils/podofo/utils.cpp index dee0aeb981..f9734fd09a 100644 --- a/src/calibre/utils/podofo/utils.cpp +++ b/src/calibre/utils/podofo/utils.cpp @@ -33,10 +33,8 @@ pdf::podofo_convert_pystring(PyObject *val) { #if PY_MAJOR_VERSION > 2 return PdfString(reinterpret_cast(PyUnicode_AsUTF8(val))); #else - PyObject *temp = PyUnicode_AsUTF8String(val); + pyunique_ptr temp(PyUnicode_AsUTF8String(val)); if (!temp) throw std::bad_alloc(); - PdfString s(reinterpret_cast(PyBytes_AS_STRING(temp))); - Py_DECREF(temp); - return s; + return PdfString(reinterpret_cast(PyBytes_AS_STRING(temp.get()))); #endif }