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 *
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<pdf_utf8 *>(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);

View File

@ -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<pdf_utf8 *>(title_buf));
PdfString title = podofo_convert_pystring(title_buf);
try {
page = self->doc->GetPage(num - 1);
} catch(const PdfError &err) { page = NULL; }

View File

@ -33,10 +33,8 @@ pdf::podofo_convert_pystring(PyObject *val) {
#if PY_MAJOR_VERSION > 2
return PdfString(reinterpret_cast<const pdf_utf8*>(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<const pdf_utf8*>(PyBytes_AS_STRING(temp)));
Py_DECREF(temp);
return s;
return PdfString(reinterpret_cast<const pdf_utf8*>(PyBytes_AS_STRING(temp.get())));
#endif
}