From a14454a8572722be44064494317e940e91365f26 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 25 Feb 2019 02:47:11 -0500 Subject: [PATCH] py3: fix PyInt types for python2/python3 --- src/calibre/utils/podofo/doc.cpp | 4 ++++ src/calibre/utils/podofo/output.cpp | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/calibre/utils/podofo/doc.cpp b/src/calibre/utils/podofo/doc.cpp index 6e73e729bd..8c54782205 100644 --- a/src/calibre/utils/podofo/doc.cpp +++ b/src/calibre/utils/podofo/doc.cpp @@ -343,7 +343,11 @@ error: static PyObject * PDFDoc_pages_getter(PDFDoc *self, void *closure) { int pages = self->doc->GetPageCount(); +#if PY_MAJOR_VERSION >= 3 + PyObject *ans = PyLong_FromLong(static_cast(pages)); +#else PyObject *ans = PyInt_FromLong(static_cast(pages)); +#endif if (ans != NULL) Py_INCREF(ans); return ans; } diff --git a/src/calibre/utils/podofo/output.cpp b/src/calibre/utils/podofo/output.cpp index a03cc0b188..106b4ae763 100644 --- a/src/calibre/utils/podofo/output.cpp +++ b/src/calibre/utils/podofo/output.cpp @@ -97,7 +97,11 @@ class OutputDevice : public PdfOutputDevice { char *buf = NULL; Py_ssize_t len = 0; +#if PY_MAJOR_VERSION >= 3 + if ((temp = PyLong_FromSize_t(lLen)) == NULL) throw pyerr(); +#else if ((temp = PyInt_FromSize_t(lLen)) == NULL) throw pyerr(); +#endif ret = PyObject_CallFunctionObjArgs(read_func, temp, NULL); NUKE(temp); if (ret != NULL) { @@ -118,7 +122,11 @@ class OutputDevice : public PdfOutputDevice { void Seek(size_t offset) { PyObject *ret, *temp; +#if PY_MAJOR_VERSION >= 3 + if ((temp = PyLong_FromSize_t(offset)) == NULL) throw pyerr(); +#else if ((temp = PyInt_FromSize_t(offset)) == NULL) throw pyerr(); +#endif ret = PyObject_CallFunctionObjArgs(seek_func, temp, NULL); NUKE(temp); if (ret == NULL) { @@ -144,7 +152,11 @@ class OutputDevice : public PdfOutputDevice { PyErr_SetString(PyExc_Exception, "tell() method did not return a number"); throw pyerr(); } +#if PY_MAJOR_VERSION >= 3 + ans = PyLong_AsUnsignedLongMask(ret); +#else ans = PyInt_AsUnsignedLongMask(ret); +#endif Py_DECREF(ret); if (PyErr_Occurred() != NULL) throw pyerr(); @@ -191,4 +203,3 @@ PyObject* pdf::write_doc(PdfMemDocument *doc, PyObject *f) { Py_RETURN_NONE; } -