Cleanup conversion of python strings to podofo strings

This commit is contained in:
Kovid Goyal 2019-07-31 18:05:19 +05:30
parent 3255ecbcf1
commit 6d449818c2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 38 additions and 57 deletions

View File

@ -243,35 +243,35 @@ static PyObject *
PDFDoc_create_outline(PDFDoc *self, PyObject *args) { PDFDoc_create_outline(PDFDoc *self, PyObject *args) {
PyObject *p; PyObject *p;
PDFOutlineItem *ans; PDFOutlineItem *ans;
PdfString *title;
int pagenum; int pagenum;
if (!PyArg_ParseTuple(args, "Ui", &p, &pagenum)) return NULL; if (!PyArg_ParseTuple(args, "Ui", &p, &pagenum)) return NULL;
title = podofo_convert_pystring(p);
if (title == NULL) return NULL;
ans = PyObject_New(PDFOutlineItem, &PDFOutlineItemType); ans = PyObject_New(PDFOutlineItem, &PDFOutlineItemType);
if (ans == NULL) goto error; if (ans == NULL) goto error;
try { try {
const PdfString title = podofo_convert_pystring(p);
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);
if (ans->item == NULL) {PyErr_NoMemory(); goto error;} if (ans->item == NULL) {PyErr_NoMemory(); goto error;}
ans->doc = self->doc; ans->doc = self->doc;
PdfDestination dest(self->doc->GetPage(pagenum)); PdfDestination dest(self->doc->GetPage(pagenum));
ans->item->SetDestination(dest); ans->item->SetDestination(dest);
} catch(const PdfError & err) { } catch(const PdfError & err) {
podofo_set_exception(err); goto error; podofo_set_exception(err); goto error;
} catch(const std::exception & err) {
PyErr_Format(PyExc_ValueError, "An error occurred while trying to create the outline: %s", err.what());
goto error;
} catch (...) { } catch (...) {
PyErr_SetString(PyExc_ValueError, "An unknown error occurred while trying to create the outline"); PyErr_SetString(PyExc_ValueError, "An unknown error occurred while trying to create the outline");
goto error; goto error;
} }
delete title;
return (PyObject*)ans; return (PyObject*)ans;
error: error:
Py_XDECREF(ans); delete title; Py_XDECREF(ans);
return NULL; return NULL;
} // }}} } // }}}
@ -427,33 +427,25 @@ PDFDoc_setter(PDFDoc *self, PyObject *val, int field) {
PyErr_SetString(PyExc_ValueError, "Must use unicode objects to set metadata"); PyErr_SetString(PyExc_ValueError, "Must use unicode objects to set metadata");
return -1; return -1;
} }
PdfInfo *info = new PdfInfo(*self->doc->GetInfo()); PdfInfo *info = self->doc->GetInfo();
if (info == NULL) { if (!info) { PyErr_SetString(Error, "You must first load a PDF Document"); return -1; }
PyErr_SetString(PyExc_Exception, "You must first load a PDF Document"); const PdfString s = podofo_convert_pystring(val);
return -1;
}
PdfString *s = NULL;
if (self->doc->GetEncrypted()) s = podofo_convert_pystring_single_byte(val);
else s = podofo_convert_pystring(val);
if (s == NULL) return -1;
switch (field) { switch (field) {
case 0: case 0:
info->SetTitle(*s); break; info->SetTitle(s); break;
case 1: case 1:
info->SetAuthor(*s); break; info->SetAuthor(s); break;
case 2: case 2:
info->SetSubject(*s); break; info->SetSubject(s); break;
case 3: case 3:
info->SetKeywords(*s); break; info->SetKeywords(s); break;
case 4: case 4:
info->SetCreator(*s); break; info->SetCreator(s); break;
case 5: case 5:
info->SetProducer(*s); break; info->SetProducer(s); break;
default: default:
PyErr_SetString(PyExc_Exception, "Bad field"); PyErr_SetString(Error, "Bad field");
return -1; return -1;
} }

View File

@ -37,11 +37,8 @@ extern PyTypeObject PDFOutlineItemType;
extern PyObject *Error; extern PyObject *Error;
// Utilities // Utilities
extern void podofo_set_exception(const PdfError &err); void podofo_set_exception(const PdfError &err);
extern PyObject * podofo_convert_pdfstring(const PdfString &s); PyObject * podofo_convert_pdfstring(const PdfString &s);
extern PdfString * podofo_convert_pystring(PyObject *py); const PdfString podofo_convert_pystring(PyObject *py);
extern PdfString * podofo_convert_pystring_single_byte(PyObject *py); PyObject* write_doc(PdfMemDocument *doc, PyObject *f);
extern PyObject* write_doc(PdfMemDocument *doc, PyObject *f);
} }

View File

@ -47,36 +47,36 @@ create(PDFOutlineItem *self, PyObject *args) {
PyObject *ptitle, *as_child = NULL; PyObject *ptitle, *as_child = NULL;
PDFOutlineItem *ans; PDFOutlineItem *ans;
int num; int num;
PdfString *title;
PdfPage *page; PdfPage *page;
if (!PyArg_ParseTuple(args, "Ui|O", &ptitle, &num, &as_child)) return NULL; if (!PyArg_ParseTuple(args, "Ui|O", &ptitle, &num, &as_child)) return NULL;
title = podofo_convert_pystring(ptitle);
if (title == NULL) 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 {
const PdfString title = podofo_convert_pystring(ptitle);
page = self->doc->GetPage(num); page = self->doc->GetPage(num);
if (page == NULL) { PyErr_Format(PyExc_ValueError, "Invalid page number: %d", num); goto error; } if (page == NULL) { PyErr_Format(PyExc_ValueError, "Invalid page number: %d", num); goto error; }
PdfDestination dest(page); PdfDestination dest(page);
if (as_child != NULL && PyObject_IsTrue(as_child)) { if (as_child != NULL && PyObject_IsTrue(as_child)) {
ans->item = self->item->CreateChild(*title, dest); ans->item = self->item->CreateChild(title, dest);
} else } else
ans->item = self->item->CreateNext(*title, dest); ans->item = self->item->CreateNext(title, dest);
} catch (const PdfError &err) { } catch (const PdfError &err) {
podofo_set_exception(err); goto error; podofo_set_exception(err); goto error;
} catch(const std::exception & err) {
PyErr_Format(PyExc_ValueError, "An error occurred while trying to create the outline: %s", err.what());
goto error;
} catch (...) { } catch (...) {
PyErr_SetString(PyExc_Exception, "An unknown error occurred while trying to create the outline item"); PyErr_SetString(PyExc_Exception, "An unknown error occurred while trying to create the outline item");
goto error; goto error;
} }
delete title;
return (PyObject*) ans; return (PyObject*) ans;
error: error:
Py_XDECREF(ans); delete title; Py_XDECREF(ans);
return NULL; return NULL;
} }

View File

@ -28,23 +28,15 @@ pdf::podofo_convert_pdfstring(const PdfString &s) {
return PyUnicode_FromString(s.GetStringUtf8().c_str()); return PyUnicode_FromString(s.GetStringUtf8().c_str());
} }
PdfString * const PdfString
pdf::podofo_convert_pystring(PyObject *py) { pdf::podofo_convert_pystring(PyObject *val) {
PyObject *u8 = PyUnicode_AsEncodedString(py, "UTF-8", "replace"); #if PY_MAJOR_VERSION > 2
if (u8 == NULL) { return NULL; } return s(reinterpret_cast<const pdf_utf8*>(PyUnicode_AsUTF8(val)));
pdf_utf8 *s8 = reinterpret_cast<pdf_utf8 *>(PyBytes_AS_STRING(u8)); #else
PdfString *ans = new PdfString(s8); PyObject *temp = PyUnicode_AsUTF8String(val);
Py_DECREF(u8); if (!temp) throw std::bad_alloc();
if (ans == NULL) PyErr_NoMemory(); PdfString s(reinterpret_cast<const pdf_utf8*>(PyBytes_AS_STRING(temp)));
return ans; Py_DECREF(temp);
} return s;
#endif
PdfString *
pdf::podofo_convert_pystring_single_byte(PyObject *py) {
PyObject *s = PyUnicode_AsEncodedString(py, "cp1252", "replace");
if (s == NULL) { return NULL; }
PdfString *ans = new PdfString(PyBytes_AS_STRING(s));
Py_DECREF(s);
if (ans == NULL) PyErr_NoMemory();
return ans;
} }