diff --git a/src/calibre/devices/mtp/unix/libmtp.c b/src/calibre/devices/mtp/unix/libmtp.c index 8d8481d551..6456ec4afd 100644 --- a/src/calibre/devices/mtp/unix/libmtp.c +++ b/src/calibre/devices/mtp/unix/libmtp.c @@ -86,7 +86,11 @@ static uint16_t data_to_python(void *params, void *priv, uint32_t sendlen, unsig cb = (ProgressCallback *)priv; *putlen = sendlen; PyEval_RestoreThread(cb->state); +#if PY_MAJOR_VERSION >= 3 + res = PyObject_CallMethod(cb->extra, "write", "y#", data, (Py_ssize_t)sendlen); +#else res = PyObject_CallMethod(cb->extra, "write", "s#", data, (Py_ssize_t)sendlen); +#endif if (res == NULL) { ret = LIBMTP_HANDLER_RETURN_ERROR; *putlen = 0; @@ -608,45 +612,44 @@ static PyGetSetDef Device_getsetters[] = { }; static PyTypeObject DeviceType = { // {{{ - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ - "libmtp.Device", /*tp_name*/ - sizeof(Device), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)Device_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "Device", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - Device_methods, /* tp_methods */ - 0, /* tp_members */ - Device_getsetters, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)Device_init, /* tp_init */ - 0, /* tp_alloc */ - 0, /* tp_new */ + PyVarObject_HEAD_INIT(NULL, 0) + /* tp_name */ "libmtp.Device", + /* tp_basicsize */ sizeof(Device), + /* tp_itemsize */ 0, + /* tp_dealloc */ (destructor)Device_dealloc, + /* tp_print */ 0, + /* tp_getattr */ 0, + /* tp_setattr */ 0, + /* tp_compare */ 0, + /* tp_repr */ 0, + /* tp_as_number */ 0, + /* tp_as_sequence */ 0, + /* tp_as_mapping */ 0, + /* tp_hash */ 0, + /* tp_call */ 0, + /* tp_str */ 0, + /* tp_getattro */ 0, + /* tp_setattro */ 0, + /* tp_as_buffer */ 0, + /* tp_flags */ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + /* tp_doc */ "Device", + /* tp_traverse */ 0, + /* tp_clear */ 0, + /* tp_richcompare */ 0, + /* tp_weaklistoffset */ 0, + /* tp_iter */ 0, + /* tp_iternext */ 0, + /* tp_methods */ Device_methods, + /* tp_members */ 0, + /* tp_getset */ Device_getsetters, + /* tp_base */ 0, + /* tp_dict */ 0, + /* tp_descr_get */ 0, + /* tp_descr_set */ 0, + /* tp_dictoffset */ 0, + /* tp_init */ (initproc)Device_init, + /* tp_alloc */ 0, + /* tp_new */ 0, }; // }}} // }}} End Device object definition @@ -695,6 +698,8 @@ known_devices(PyObject *self, PyObject *args) { return ans; } +static char libmtp_doc[] = "Interface to libmtp."; + static PyMethodDef libmtp_methods[] = { {"set_debug_level", set_debug_level, METH_VARARGS, "set_debug_level(level)\n\nSet the debug level bit mask, see LIBMTP_DEBUG_* constants." @@ -712,19 +717,41 @@ static PyMethodDef libmtp_methods[] = { }; -CALIBRE_MODINIT_FUNC -initlibmtp(void) { - PyObject *m; +#if PY_MAJOR_VERSION >= 3 +#define INITERROR return NULL +#define INITMODULE PyModule_Create(&libmtp_module) +static struct PyModuleDef libmtp_module = { + /* m_base */ PyModuleDef_HEAD_INIT, + /* m_name */ "libmtp", + /* m_doc */ libmtp_doc, + /* m_size */ -1, + /* m_methods */ libmtp_methods, + /* m_slots */ 0, + /* m_traverse */ 0, + /* m_clear */ 0, + /* m_free */ 0, +}; +CALIBRE_MODINIT_FUNC PyInit_libmtp(void) { +#else +#define INITERROR return +#define INITMODULE Py_InitModule3("libmtp", libmtp_methods, libmtp_doc); +CALIBRE_MODINIT_FUNC initlibmtp(void) { +#endif DeviceType.tp_new = PyType_GenericNew; - if (PyType_Ready(&DeviceType) < 0) - return; + if (PyType_Ready(&DeviceType) < 0) { + INITERROR; + } - m = Py_InitModule3("libmtp", libmtp_methods, "Interface to libmtp."); - if (m == NULL) return; + PyObject *m = INITMODULE; + if (m == NULL) { + INITERROR; + } MTPError = PyErr_NewException("libmtp.MTPError", NULL, NULL); - if (MTPError == NULL) return; + if (MTPError == NULL) { + INITERROR; + } PyModule_AddObject(m, "MTPError", MTPError); // Redirect stdout to get rid of the annoying message about mtpz. Really, @@ -754,4 +781,8 @@ initlibmtp(void) { PyModule_AddIntMacro(m, LIBMTP_DEBUG_USB); PyModule_AddIntMacro(m, LIBMTP_DEBUG_DATA); PyModule_AddIntMacro(m, LIBMTP_DEBUG_ALL); + +#if PY_MAJOR_VERSION >= 3 + return m; +#endif } diff --git a/src/calibre/devices/mtp/unix/upstream/update.py b/src/calibre/devices/mtp/unix/upstream/update.py old mode 100644 new mode 100755 index 0fc6a2b41c..98edf95812 --- a/src/calibre/devices/mtp/unix/upstream/update.py +++ b/src/calibre/devices/mtp/unix/upstream/update.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai from __future__ import (unicode_literals, division, absolute_import, print_function) @@ -14,9 +14,7 @@ base = os.path.dirname(os.path.abspath(__file__)) os.chdir('/tmp') if os.path.exists('libmtp'): shutil.rmtree('libmtp') -subprocess.check_call(['git', 'clone', 'git://git.code.sf.net/p/libmtp/code', +subprocess.check_call(['git', 'clone', '--depth=1', 'git://git.code.sf.net/p/libmtp/code', 'libmtp']) for x in ('src/music-players.h', 'src/device-flags.h'): - with open(os.path.join(base, os.path.basename(x)), 'wb') as f: - shutil.copyfileobj(open('libmtp/'+x), f) - + shutil.copyfile('libmtp/'+x, os.path.join(base, os.path.basename(x))) diff --git a/src/calibre/devices/mtp/windows/content_enumeration.cpp b/src/calibre/devices/mtp/windows/content_enumeration.cpp index 67fe2b7914..9afff2461d 100644 --- a/src/calibre/devices/mtp/windows/content_enumeration.cpp +++ b/src/calibre/devices/mtp/windows/content_enumeration.cpp @@ -554,7 +554,11 @@ PyObject* wpd::get_file(IPortableDevice *device, const wchar_t *object_id, PyObj PyErr_SetString(PyExc_IOError, "Read access is denied to this object"); break; } else if (SUCCEEDED(hr)) { if (bytes_read > 0) { +#if PY_MAJOR_VERSION >= 3 + res = PyObject_CallMethod(dest, "write", "y#", buf, bytes_read); +#else res = PyObject_CallMethod(dest, "write", "s#", buf, bytes_read); +#endif if (res == NULL) break; Py_DECREF(res); res = NULL; if (callback != NULL) Py_XDECREF(PyObject_CallFunction(callback, "kK", total_read, filesize)); diff --git a/src/calibre/ebooks/compression/palmdoc.c b/src/calibre/ebooks/compression/palmdoc.c index 61d00ab04f..623bb10f67 100644 --- a/src/calibre/ebooks/compression/palmdoc.c +++ b/src/calibre/ebooks/compression/palmdoc.c @@ -42,12 +42,20 @@ typedef struct { #define CHAR(x) (( (x) > 127 ) ? (x)-256 : (x)) +#if PY_MAJOR_VERSION >= 3 + #define BUFFER_FMT "y*" + #define BYTES_FMT "y#" +#else + #define BUFFER_FMT "t#" + #define BYTES_FMT "s#" +#endif + static PyObject * cpalmdoc_decompress(PyObject *self, PyObject *args) { const char *_input = NULL; Py_ssize_t input_len = 0; Byte *input; char *output; Byte c; PyObject *ans; Py_ssize_t i = 0, o = 0, j = 0, di, n; - if (!PyArg_ParseTuple(args, "t#", &_input, &input_len)) + if (!PyArg_ParseTuple(args, BUFFER_FMT, &_input, &input_len)) return NULL; input = (Byte *) PyMem_Malloc(sizeof(Byte)*input_len); if (input == NULL) return PyErr_NoMemory(); @@ -76,7 +84,7 @@ cpalmdoc_decompress(PyObject *self, PyObject *args) { output[o] = output[o - di]; } } - ans = Py_BuildValue("s#", output, o); + ans = Py_BuildValue(BYTES_FMT, output, o); if (output != NULL) PyMem_Free(output); if (input != NULL) PyMem_Free(input); return ans; @@ -162,7 +170,7 @@ cpalmdoc_compress(PyObject *self, PyObject *args) { char *output; PyObject *ans; Py_ssize_t j = 0; buffer b; - if (!PyArg_ParseTuple(args, "t#", &_input, &input_len)) + if (!PyArg_ParseTuple(args, BUFFER_FMT, &_input, &input_len)) return NULL; b.data = (Byte *)PyMem_Malloc(sizeof(Byte)*input_len); if (b.data == NULL) return PyErr_NoMemory(); @@ -176,7 +184,7 @@ cpalmdoc_compress(PyObject *self, PyObject *args) { if (output == NULL) return PyErr_NoMemory(); j = cpalmdoc_do_compress(&b, output); if ( j == 0) return PyErr_NoMemory(); - ans = Py_BuildValue("s#", output, j); + ans = Py_BuildValue(BYTES_FMT, output, j); PyMem_Free(output); PyMem_Free(b.data); return ans; @@ -203,4 +211,3 @@ initcPalmdoc(void) { ); if (m == NULL) return; } - diff --git a/src/calibre/utils/fonts/winfonts.cpp b/src/calibre/utils/fonts/winfonts.cpp index 428f31d529..bafc8b5728 100644 --- a/src/calibre/utils/fonts/winfonts.cpp +++ b/src/calibre/utils/fonts/winfonts.cpp @@ -165,7 +165,11 @@ static PyObject* add_font(PyObject *self, PyObject *args) { Py_ssize_t sz; DWORD num = 0; +#if PY_MAJOR_VERSION >= 3 + if (!PyArg_ParseTuple(args, "y#", &data, &sz)) return NULL; +#else if (!PyArg_ParseTuple(args, "s#", &data, &sz)) return NULL; +#endif AddFontMemResourceEx(data, (DWORD)sz, NULL, &num); @@ -260,4 +264,3 @@ initwinfonts(void) { PyModule_AddIntMacro(m, FW_HEAVY); PyModule_AddIntMacro(m, FW_BLACK); } - diff --git a/src/calibre/utils/lzx/compressor.c b/src/calibre/utils/lzx/compressor.c index b63ee3ee95..605ca515f5 100644 --- a/src/calibre/utils/lzx/compressor.c +++ b/src/calibre/utils/lzx/compressor.c @@ -310,7 +310,11 @@ Compressor_compress(Compressor *self, PyObject *args, PyObject *kwds) int flush = 0; if (!PyArg_ParseTupleAndKeywords( +#if PYTHON_MAJOR_VERSION >= 3 + args, kwds, "y#|b", kwlist, &data, &inlen, &flush)) { +#else args, kwds, "s#|b", kwlist, &data, &inlen, &flush)) { +#endif return NULL; } diff --git a/src/calibre/utils/lzx/lzxmodule.c b/src/calibre/utils/lzx/lzxmodule.c index 6c9a4f15d8..3cb5d36919 100644 --- a/src/calibre/utils/lzx/lzxmodule.c +++ b/src/calibre/utils/lzx/lzxmodule.c @@ -157,7 +157,11 @@ decompress(PyObject *self, PyObject *args) memory_file dest; PyObject *retval = NULL; +#if PY_MAJOR_VERSION >= 3 + if (!PyArg_ParseTuple(args, "y#I", &inbuf, &inlen, &outlen)) { +#else if (!PyArg_ParseTuple(args, "s#I", &inbuf, &inlen, &outlen)) { +#endif return NULL; } diff --git a/src/calibre/utils/podofo/doc.cpp b/src/calibre/utils/podofo/doc.cpp index 6e73e729bd..9f19b47446 100644 --- a/src/calibre/utils/podofo/doc.cpp +++ b/src/calibre/utils/podofo/doc.cpp @@ -32,12 +32,18 @@ PDFDoc_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } // }}} +#if PY_MAJOR_VERSION >= 3 + #define BYTES_FMT "y#" +#else + #define BYTES_FMT "s#" +#endif + // Loading/Opening of PDF files {{{ static PyObject * PDFDoc_load(PDFDoc *self, PyObject *args) { char *buffer; Py_ssize_t size; - if (PyArg_ParseTuple(args, "s#", &buffer, &size)) { + if (PyArg_ParseTuple(args, BYTES_FMT, &buffer, &size)) { try { #if PODOFO_VERSION <= 0x000905 self->doc->Load(buffer, (long)size); @@ -77,7 +83,7 @@ static PyObject * PDFDoc_save(PDFDoc *self, PyObject *args) { char *buffer; - if (PyArg_ParseTuple(args, "s", &buffer)) { + if (PyArg_ParseTuple(args, BYTES_FMT, &buffer)) { try { self->doc->Write(buffer); } catch(const PdfError & err) { @@ -284,7 +290,7 @@ PDFDoc_get_xmp_metadata(PDFDoc *self, PyObject *args) { if ((str = metadata->GetStream()) != NULL) { str->GetFilteredCopy(&buf, &len); if (buf != NULL) { - ans = Py_BuildValue("s#", buf, len); + ans = Py_BuildValue(BYTES_FMT, buf, len); free(buf); buf = NULL; if (ans == NULL) goto error; } @@ -312,7 +318,7 @@ PDFDoc_set_xmp_metadata(PDFDoc *self, PyObject *args) { TVecFilters compressed(1); compressed[0] = ePdfFilter_FlateDecode; - if (!PyArg_ParseTuple(args, "s#", &raw, &len)) return NULL; + if (!PyArg_ParseTuple(args, BYTES_FMT, &raw, &len)) return NULL; try { if ((metadata = self->doc->GetMetadata()) != NULL) { if ((str = metadata->GetStream()) == NULL) { PyErr_NoMemory(); goto error; } @@ -343,7 +349,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; } @@ -595,45 +605,43 @@ static PyMethodDef PDFDoc_methods[] = { // Type definition {{{ PyTypeObject pdf::PDFDocType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ - "podofo.PDFDoc", /*tp_name*/ - sizeof(PDFDoc), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)PDFDoc_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ - "PDF Documents", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - PDFDoc_methods, /* tp_methods */ - 0, /* tp_members */ - PDFDoc_getsetters, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - PDFDoc_new, /* tp_new */ - + PyVarObject_HEAD_INIT(NULL, 0) + /* tp_name */ "podofo.PDFDoc", + /* tp_basicsize */ sizeof(PDFDoc), + /* tp_itemsize */ 0, + /* tp_dealloc */ (destructor)PDFDoc_dealloc, + /* tp_print */ 0, + /* tp_getattr */ 0, + /* tp_setattr */ 0, + /* tp_compare */ 0, + /* tp_repr */ 0, + /* tp_as_number */ 0, + /* tp_as_sequence */ 0, + /* tp_as_mapping */ 0, + /* tp_hash */ 0, + /* tp_call */ 0, + /* tp_str */ 0, + /* tp_getattro */ 0, + /* tp_setattro */ 0, + /* tp_as_buffer */ 0, + /* tp_flags */ Py_TPFLAGS_DEFAULT, + /* tp_doc */ "PDF Documents", + /* tp_traverse */ 0, + /* tp_clear */ 0, + /* tp_richcompare */ 0, + /* tp_weaklistoffset */ 0, + /* tp_iter */ 0, + /* tp_iternext */ 0, + /* tp_methods */ PDFDoc_methods, + /* tp_members */ 0, + /* tp_getset */ PDFDoc_getsetters, + /* tp_base */ 0, + /* tp_dict */ 0, + /* tp_descr_get */ 0, + /* tp_descr_set */ 0, + /* tp_dictoffset */ 0, + /* tp_init */ 0, + /* tp_alloc */ 0, + /* tp_new */ PDFDoc_new, }; // }}} diff --git a/src/calibre/utils/podofo/outline.cpp b/src/calibre/utils/podofo/outline.cpp index b7be4aeae8..01a3b186d9 100644 --- a/src/calibre/utils/podofo/outline.cpp +++ b/src/calibre/utils/podofo/outline.cpp @@ -96,45 +96,43 @@ static PyMethodDef methods[] = { // Type definition {{{ PyTypeObject pdf::PDFOutlineItemType = { - PyObject_HEAD_INIT(NULL) - 0, /*ob_size*/ - "podofo.PDFOutlineItem", /*tp_name*/ - sizeof(PDFOutlineItem), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash */ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ - "PDF Outline items", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - new_item, /* tp_new */ - + PyVarObject_HEAD_INIT(NULL, 0) + /* tp_name */ "podofo.PDFOutlineItem", + /* tp_basicsize */ sizeof(PDFOutlineItem), + /* tp_itemsize */ 0, + /* tp_dealloc */ (destructor)dealloc, + /* tp_print */ 0, + /* tp_getattr */ 0, + /* tp_setattr */ 0, + /* tp_compare */ 0, + /* tp_repr */ 0, + /* tp_as_number */ 0, + /* tp_as_sequence */ 0, + /* tp_as_mapping */ 0, + /* tp_hash */ 0, + /* tp_call */ 0, + /* tp_str */ 0, + /* tp_getattro */ 0, + /* tp_setattro */ 0, + /* tp_as_buffer */ 0, + /* tp_flags */ Py_TPFLAGS_DEFAULT, + /* tp_doc */ "PDF Outline items", + /* tp_traverse */ 0, + /* tp_clear */ 0, + /* tp_richcompare */ 0, + /* tp_weaklistoffset */ 0, + /* tp_iter */ 0, + /* tp_iternext */ 0, + /* tp_methods */ methods, + /* tp_members */ 0, + /* tp_getset */ 0, + /* tp_base */ 0, + /* tp_dict */ 0, + /* tp_descr_get */ 0, + /* tp_descr_set */ 0, + /* tp_dictoffset */ 0, + /* tp_init */ 0, + /* tp_alloc */ 0, + /* tp_new */ new_item, }; // }}} 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; } - diff --git a/src/calibre/utils/podofo/podofo.cpp b/src/calibre/utils/podofo/podofo.cpp index 50f20de8cf..517f4cbf80 100644 --- a/src/calibre/utils/podofo/podofo.cpp +++ b/src/calibre/utils/podofo/podofo.cpp @@ -10,10 +10,6 @@ using namespace PoDoFo; PyObject *pdf::Error = NULL; -static PyMethodDef podofo_methods[] = { - {NULL} /* Sentinel */ -}; - class PyLogMessage : public PdfError::LogMessageCallback { public: @@ -38,29 +34,63 @@ class PyLogMessage : public PdfError::LogMessageCallback { PyLogMessage log_message; -CALIBRE_MODINIT_FUNC -initpodofo(void) -{ +static char podofo_doc[] = "Wrapper for the PoDoFo PDF library"; + +static PyMethodDef podofo_methods[] = { + {NULL} /* Sentinel */ +}; + +#if PY_MAJOR_VERSION >= 3 +#define INITERROR return NULL +#define INITMODULE PyModule_Create(&podofo_module) +static struct PyModuleDef podofo_module = { + /* m_base */ PyModuleDef_HEAD_INIT, + /* m_name */ "podofo", + /* m_doc */ podofo_doc, + /* m_size */ -1, + /* m_methods */ podofo_methods, + /* m_slots */ 0, + /* m_traverse */ 0, + /* m_clear */ 0, + /* m_free */ 0, +}; +CALIBRE_MODINIT_FUNC PyInit_podofo(void) { +#else +#define INITERROR return +#define INITMODULE Py_InitModule3("podofo", podofo_methods, podofo_doc) +CALIBRE_MODINIT_FUNC initpodofo(void) { +#endif + PyObject* m; - if (PyType_Ready(&pdf::PDFDocType) < 0) - return; + if (PyType_Ready(&pdf::PDFDocType) < 0) { + INITERROR; + } - if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) - return; + if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) { + INITERROR; + } pdf::Error = PyErr_NewException((char*)"podofo.Error", NULL, NULL); - if (pdf::Error == NULL) return; + if (pdf::Error == NULL) { + INITERROR; + } PdfError::SetLogMessageCallback((PdfError::LogMessageCallback*)&log_message); PdfError::EnableDebug(false); - m = Py_InitModule3("podofo", podofo_methods, - "Wrapper for the PoDoFo PDF library"); + + m = INITMODULE; + if (m == NULL) { + INITERROR; + } Py_INCREF(&pdf::PDFDocType); PyModule_AddObject(m, "PDFDoc", (PyObject *)&pdf::PDFDocType); PyModule_AddObject(m, "Error", pdf::Error); -} +#if PY_MAJOR_VERSION >= 3 + return m; +#endif +} diff --git a/src/lzma/lzma_binding.c b/src/lzma/lzma_binding.c index 24dd28e60a..d02f14c523 100644 --- a/src/lzma/lzma_binding.c +++ b/src/lzma/lzma_binding.c @@ -58,6 +58,12 @@ static PyObject *LZMAError = NULL; // Utils {{{ static UInt64 crc64_table[256]; +#if PY_MAJOR_VERSION >= 3 + #define BYTES_FMT "y#" +#else + #define BYTES_FMT "s#" +#endif + static void init_crc_table() { static const UInt64 poly64 = (UInt64)(0xC96C5795D7870F42); size_t i, j; @@ -79,7 +85,7 @@ crc64(PyObject *self, PyObject *args) { Py_ssize_t size = 0; UInt64 crc = 0; size_t i; - if (!PyArg_ParseTuple(args, "s#|K", &data, &size, &crc)) return NULL; + if (!PyArg_ParseTuple(args, BYTES_FMT "|K", &data, &size, &crc)) return NULL; crc = ~crc; for (i = 0; i < (size_t)size; ++i) crc = crc64_table[data[i] ^ (crc & 0xFF)] ^ (crc >> 8); @@ -142,7 +148,7 @@ decompress2(PyObject *self, PyObject *args) { } else { res = SZ_OK; bytes_written = 0; status = LZMA_STATUS_NEEDS_MORE_INPUT; } if (res != SZ_OK) { SET_ERROR(res); goto exit; } if (bytes_written > 0) { - if(!PyObject_CallFunction(write, "s#", outbuf, bytes_written)) goto exit; + if(!PyObject_CallFunction(write, BYTES_FMT, outbuf, bytes_written)) goto exit; } if (inbuf_len > inbuf_pos && !bytes_read && !bytes_written && status != LZMA_STATUS_NEEDS_MORE_INPUT && status != LZMA_STATUS_FINISHED_WITH_MARK) { SET_ERROR(SZ_ERROR_DATA); goto exit; @@ -188,7 +194,7 @@ decompress(PyObject *self, PyObject *args) { ELzmaStatus status = LZMA_STATUS_NOT_FINISHED; ELzmaFinishMode finish_mode = LZMA_FINISH_ANY; - if(!PyArg_ParseTuple(args, "OOOKs#k", &read, &seek, &write, &decompressed_size, &header, &header_size, &bufsize)) return NULL; + if(!PyArg_ParseTuple(args, "OOOK" BYTES_FMT "k", &read, &seek, &write, &decompressed_size, &header, &header_size, &bufsize)) return NULL; size_known = (decompressed_size != (UInt64)(Int64)-1); if (header_size != 13) { PyErr_SetString(LZMAError, "Header must be exactly 13 bytes long"); return NULL; } if (!decompressed_size) { PyErr_SetString(LZMAError, "Cannot decompress empty file"); return NULL; } @@ -214,7 +220,7 @@ decompress(PyObject *self, PyObject *args) { } else { res = SZ_OK; bytes_written = 0; status = LZMA_STATUS_NEEDS_MORE_INPUT; } if (res != SZ_OK) { SET_ERROR(res); goto exit; } if (bytes_written > 0) { - if(!PyObject_CallFunction(write, "s#", outbuf, bytes_written)) goto exit; + if(!PyObject_CallFunction(write, BYTES_FMT, outbuf, bytes_written)) goto exit; total_written += bytes_written; } if (inbuf_len > inbuf_pos && !bytes_read && !bytes_written && status != LZMA_STATUS_NEEDS_MORE_INPUT && status != LZMA_STATUS_FINISHED_WITH_MARK) { @@ -296,7 +302,7 @@ static size_t owrite(void *p, const void *buf, size_t size) { PyObject *res = NULL; if (!size) return 0; ACQUIRE_GIL - res = PyObject_CallFunction(self->write, "s#", (char*)buf, size); + res = PyObject_CallFunction(self->write, BYTES_FMT, (char*)buf, size); if (res == NULL) return 0; Py_DECREF(res); RELEASE_GIL @@ -332,7 +338,7 @@ get_lzma2_properties(int preset) { exit: if (lzma2) Lzma2Enc_Destroy(lzma2); if (PyErr_Occurred()) return NULL; - return Py_BuildValue("s#", &props_out, 1); + return Py_BuildValue(BYTES_FMT, &props_out, 1); } @@ -382,7 +388,7 @@ compress(PyObject *self, PyObject *args) { exit: if (lzma2) Lzma2Enc_Destroy(lzma2); if (PyErr_Occurred()) return NULL; - return Py_BuildValue("s#", &props_out, 1); + return Py_BuildValue(BYTES_FMT, &props_out, 1); } // }}}