py3: fix PyInt types for python2/python3

This commit is contained in:
Eli Schwartz 2019-02-25 02:47:11 -05:00
parent 76447ba379
commit a14454a857
2 changed files with 16 additions and 1 deletions

View File

@ -343,7 +343,11 @@ error:
static PyObject * static PyObject *
PDFDoc_pages_getter(PDFDoc *self, void *closure) { PDFDoc_pages_getter(PDFDoc *self, void *closure) {
int pages = self->doc->GetPageCount(); int pages = self->doc->GetPageCount();
#if PY_MAJOR_VERSION >= 3
PyObject *ans = PyLong_FromLong(static_cast<long>(pages));
#else
PyObject *ans = PyInt_FromLong(static_cast<long>(pages)); PyObject *ans = PyInt_FromLong(static_cast<long>(pages));
#endif
if (ans != NULL) Py_INCREF(ans); if (ans != NULL) Py_INCREF(ans);
return ans; return ans;
} }

View File

@ -97,7 +97,11 @@ class OutputDevice : public PdfOutputDevice {
char *buf = NULL; char *buf = NULL;
Py_ssize_t len = 0; 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(); if ((temp = PyInt_FromSize_t(lLen)) == NULL) throw pyerr();
#endif
ret = PyObject_CallFunctionObjArgs(read_func, temp, NULL); ret = PyObject_CallFunctionObjArgs(read_func, temp, NULL);
NUKE(temp); NUKE(temp);
if (ret != NULL) { if (ret != NULL) {
@ -118,7 +122,11 @@ class OutputDevice : public PdfOutputDevice {
void Seek(size_t offset) { void Seek(size_t offset) {
PyObject *ret, *temp; 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(); if ((temp = PyInt_FromSize_t(offset)) == NULL) throw pyerr();
#endif
ret = PyObject_CallFunctionObjArgs(seek_func, temp, NULL); ret = PyObject_CallFunctionObjArgs(seek_func, temp, NULL);
NUKE(temp); NUKE(temp);
if (ret == NULL) { if (ret == NULL) {
@ -144,7 +152,11 @@ class OutputDevice : public PdfOutputDevice {
PyErr_SetString(PyExc_Exception, "tell() method did not return a number"); PyErr_SetString(PyExc_Exception, "tell() method did not return a number");
throw pyerr(); throw pyerr();
} }
#if PY_MAJOR_VERSION >= 3
ans = PyLong_AsUnsignedLongMask(ret);
#else
ans = PyInt_AsUnsignedLongMask(ret); ans = PyInt_AsUnsignedLongMask(ret);
#endif
Py_DECREF(ret); Py_DECREF(ret);
if (PyErr_Occurred() != NULL) throw pyerr(); if (PyErr_Occurred() != NULL) throw pyerr();
@ -191,4 +203,3 @@ PyObject* pdf::write_doc(PdfMemDocument *doc, PyObject *f) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }