Cleanup python str -> podofo str

This commit is contained in:
Kovid Goyal 2019-07-31 18:21:15 +05:30
parent bc4c988fce
commit 84c8544e14
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 8 additions and 10 deletions

View File

@ -293,18 +293,18 @@ PDFDoc_set_box(PDFDoc *self, PyObject *args) {
static PyObject * static PyObject *
PDFDoc_create_outline(PDFDoc *self, PyObject *args) { PDFDoc_create_outline(PDFDoc *self, PyObject *args) {
PDFOutlineItem *ans; PDFOutlineItem *ans;
char *title_buf; PyObject *title_buf;
unsigned int pagenum; unsigned int pagenum;
double left = 0, top = 0, zoom = 0; double left = 0, top = 0, zoom = 0;
PdfPage *page; 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); ans = PyObject_New(PDFOutlineItem, &PDFOutlineItemType);
if (ans == NULL) goto error; if (ans == NULL) goto error;
try { try {
PdfString title(reinterpret_cast<pdf_utf8 *>(title_buf)); PdfString title = podofo_convert_pystring(title_buf);
PdfOutlines *outlines = self->doc->GetOutlines(); PdfOutlines *outlines = self->doc->GetOutlines();
if (outlines == NULL) {PyErr_NoMemory(); goto error;} if (outlines == NULL) {PyErr_NoMemory(); goto error;}
ans->item = outlines->CreateRoot(title); ans->item = outlines->CreateRoot(title);

View File

@ -49,16 +49,16 @@ create(PDFOutlineItem *self, PyObject *args) {
unsigned int num; unsigned int num;
double left = 0, top = 0, zoom = 0; double left = 0, top = 0, zoom = 0;
PdfPage *page; 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); ans = PyObject_New(PDFOutlineItem, &PDFOutlineItemType);
if (ans == NULL) goto error; if (ans == NULL) goto error;
ans->doc = self->doc; ans->doc = self->doc;
try { try {
PdfString title(reinterpret_cast<pdf_utf8 *>(title_buf)); PdfString title = podofo_convert_pystring(title_buf);
try { try {
page = self->doc->GetPage(num - 1); page = self->doc->GetPage(num - 1);
} catch(const PdfError &err) { page = NULL; } } catch(const PdfError &err) { page = NULL; }

View File

@ -33,10 +33,8 @@ pdf::podofo_convert_pystring(PyObject *val) {
#if PY_MAJOR_VERSION > 2 #if PY_MAJOR_VERSION > 2
return PdfString(reinterpret_cast<const pdf_utf8*>(PyUnicode_AsUTF8(val))); return PdfString(reinterpret_cast<const pdf_utf8*>(PyUnicode_AsUTF8(val)));
#else #else
PyObject *temp = PyUnicode_AsUTF8String(val); pyunique_ptr temp(PyUnicode_AsUTF8String(val));
if (!temp) throw std::bad_alloc(); if (!temp) throw std::bad_alloc();
PdfString s(reinterpret_cast<const pdf_utf8*>(PyBytes_AS_STRING(temp))); return PdfString(reinterpret_cast<const pdf_utf8*>(PyBytes_AS_STRING(temp.get())));
Py_DECREF(temp);
return s;
#endif #endif
} }