This commit is contained in:
Kovid Goyal 2019-03-10 17:30:08 +05:30
commit c897bc1a5d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
12 changed files with 269 additions and 165 deletions

View File

@ -86,7 +86,11 @@ static uint16_t data_to_python(void *params, void *priv, uint32_t sendlen, unsig
cb = (ProgressCallback *)priv; cb = (ProgressCallback *)priv;
*putlen = sendlen; *putlen = sendlen;
PyEval_RestoreThread(cb->state); 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); res = PyObject_CallMethod(cb->extra, "write", "s#", data, (Py_ssize_t)sendlen);
#endif
if (res == NULL) { if (res == NULL) {
ret = LIBMTP_HANDLER_RETURN_ERROR; ret = LIBMTP_HANDLER_RETURN_ERROR;
*putlen = 0; *putlen = 0;
@ -608,45 +612,44 @@ static PyGetSetDef Device_getsetters[] = {
}; };
static PyTypeObject DeviceType = { // {{{ static PyTypeObject DeviceType = { // {{{
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /*ob_size*/ /* tp_name */ "libmtp.Device",
"libmtp.Device", /*tp_name*/ /* tp_basicsize */ sizeof(Device),
sizeof(Device), /*tp_basicsize*/ /* tp_itemsize */ 0,
0, /*tp_itemsize*/ /* tp_dealloc */ (destructor)Device_dealloc,
(destructor)Device_dealloc, /*tp_dealloc*/ /* tp_print */ 0,
0, /*tp_print*/ /* tp_getattr */ 0,
0, /*tp_getattr*/ /* tp_setattr */ 0,
0, /*tp_setattr*/ /* tp_compare */ 0,
0, /*tp_compare*/ /* tp_repr */ 0,
0, /*tp_repr*/ /* tp_as_number */ 0,
0, /*tp_as_number*/ /* tp_as_sequence */ 0,
0, /*tp_as_sequence*/ /* tp_as_mapping */ 0,
0, /*tp_as_mapping*/ /* tp_hash */ 0,
0, /*tp_hash */ /* tp_call */ 0,
0, /*tp_call*/ /* tp_str */ 0,
0, /*tp_str*/ /* tp_getattro */ 0,
0, /*tp_getattro*/ /* tp_setattro */ 0,
0, /*tp_setattro*/ /* tp_as_buffer */ 0,
0, /*tp_as_buffer*/ /* tp_flags */ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/ /* tp_doc */ "Device",
"Device", /* tp_doc */ /* tp_traverse */ 0,
0, /* tp_traverse */ /* tp_clear */ 0,
0, /* tp_clear */ /* tp_richcompare */ 0,
0, /* tp_richcompare */ /* tp_weaklistoffset */ 0,
0, /* tp_weaklistoffset */ /* tp_iter */ 0,
0, /* tp_iter */ /* tp_iternext */ 0,
0, /* tp_iternext */ /* tp_methods */ Device_methods,
Device_methods, /* tp_methods */ /* tp_members */ 0,
0, /* tp_members */ /* tp_getset */ Device_getsetters,
Device_getsetters, /* tp_getset */ /* tp_base */ 0,
0, /* tp_base */ /* tp_dict */ 0,
0, /* tp_dict */ /* tp_descr_get */ 0,
0, /* tp_descr_get */ /* tp_descr_set */ 0,
0, /* tp_descr_set */ /* tp_dictoffset */ 0,
0, /* tp_dictoffset */ /* tp_init */ (initproc)Device_init,
(initproc)Device_init, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ 0,
0, /* tp_new */
}; // }}} }; // }}}
// }}} End Device object definition // }}} End Device object definition
@ -695,6 +698,8 @@ known_devices(PyObject *self, PyObject *args) {
return ans; return ans;
} }
static char libmtp_doc[] = "Interface to libmtp.";
static PyMethodDef libmtp_methods[] = { static PyMethodDef libmtp_methods[] = {
{"set_debug_level", set_debug_level, METH_VARARGS, {"set_debug_level", set_debug_level, METH_VARARGS,
"set_debug_level(level)\n\nSet the debug level bit mask, see LIBMTP_DEBUG_* constants." "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 #if PY_MAJOR_VERSION >= 3
initlibmtp(void) { #define INITERROR return NULL
PyObject *m; #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; DeviceType.tp_new = PyType_GenericNew;
if (PyType_Ready(&DeviceType) < 0) if (PyType_Ready(&DeviceType) < 0) {
return; INITERROR;
}
m = Py_InitModule3("libmtp", libmtp_methods, "Interface to libmtp."); PyObject *m = INITMODULE;
if (m == NULL) return; if (m == NULL) {
INITERROR;
}
MTPError = PyErr_NewException("libmtp.MTPError", NULL, NULL); MTPError = PyErr_NewException("libmtp.MTPError", NULL, NULL);
if (MTPError == NULL) return; if (MTPError == NULL) {
INITERROR;
}
PyModule_AddObject(m, "MTPError", MTPError); PyModule_AddObject(m, "MTPError", MTPError);
// Redirect stdout to get rid of the annoying message about mtpz. Really, // 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_USB);
PyModule_AddIntMacro(m, LIBMTP_DEBUG_DATA); PyModule_AddIntMacro(m, LIBMTP_DEBUG_DATA);
PyModule_AddIntMacro(m, LIBMTP_DEBUG_ALL); PyModule_AddIntMacro(m, LIBMTP_DEBUG_ALL);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
} }

8
src/calibre/devices/mtp/unix/upstream/update.py Normal file → Executable file
View File

@ -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 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai
from __future__ import (unicode_literals, division, absolute_import, from __future__ import (unicode_literals, division, absolute_import,
print_function) print_function)
@ -14,9 +14,7 @@ base = os.path.dirname(os.path.abspath(__file__))
os.chdir('/tmp') os.chdir('/tmp')
if os.path.exists('libmtp'): if os.path.exists('libmtp'):
shutil.rmtree('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']) 'libmtp'])
for x in ('src/music-players.h', 'src/device-flags.h'): 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.copyfile('libmtp/'+x, os.path.join(base, os.path.basename(x)))
shutil.copyfileobj(open('libmtp/'+x), f)

View File

@ -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; PyErr_SetString(PyExc_IOError, "Read access is denied to this object"); break;
} else if (SUCCEEDED(hr)) { } else if (SUCCEEDED(hr)) {
if (bytes_read > 0) { 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); res = PyObject_CallMethod(dest, "write", "s#", buf, bytes_read);
#endif
if (res == NULL) break; if (res == NULL) break;
Py_DECREF(res); res = NULL; Py_DECREF(res); res = NULL;
if (callback != NULL) Py_XDECREF(PyObject_CallFunction(callback, "kK", total_read, filesize)); if (callback != NULL) Py_XDECREF(PyObject_CallFunction(callback, "kK", total_read, filesize));

View File

@ -42,12 +42,20 @@ typedef struct {
#define CHAR(x) (( (x) > 127 ) ? (x)-256 : (x)) #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 * static PyObject *
cpalmdoc_decompress(PyObject *self, PyObject *args) { cpalmdoc_decompress(PyObject *self, PyObject *args) {
const char *_input = NULL; Py_ssize_t input_len = 0; const char *_input = NULL; Py_ssize_t input_len = 0;
Byte *input; char *output; Byte c; PyObject *ans; Byte *input; char *output; Byte c; PyObject *ans;
Py_ssize_t i = 0, o = 0, j = 0, di, n; 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; return NULL;
input = (Byte *) PyMem_Malloc(sizeof(Byte)*input_len); input = (Byte *) PyMem_Malloc(sizeof(Byte)*input_len);
if (input == NULL) return PyErr_NoMemory(); if (input == NULL) return PyErr_NoMemory();
@ -76,7 +84,7 @@ cpalmdoc_decompress(PyObject *self, PyObject *args) {
output[o] = output[o - di]; 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 (output != NULL) PyMem_Free(output);
if (input != NULL) PyMem_Free(input); if (input != NULL) PyMem_Free(input);
return ans; return ans;
@ -162,7 +170,7 @@ cpalmdoc_compress(PyObject *self, PyObject *args) {
char *output; PyObject *ans; char *output; PyObject *ans;
Py_ssize_t j = 0; Py_ssize_t j = 0;
buffer b; buffer b;
if (!PyArg_ParseTuple(args, "t#", &_input, &input_len)) if (!PyArg_ParseTuple(args, BUFFER_FMT, &_input, &input_len))
return NULL; return NULL;
b.data = (Byte *)PyMem_Malloc(sizeof(Byte)*input_len); b.data = (Byte *)PyMem_Malloc(sizeof(Byte)*input_len);
if (b.data == NULL) return PyErr_NoMemory(); if (b.data == NULL) return PyErr_NoMemory();
@ -176,7 +184,7 @@ cpalmdoc_compress(PyObject *self, PyObject *args) {
if (output == NULL) return PyErr_NoMemory(); if (output == NULL) return PyErr_NoMemory();
j = cpalmdoc_do_compress(&b, output); j = cpalmdoc_do_compress(&b, output);
if ( j == 0) return PyErr_NoMemory(); if ( j == 0) return PyErr_NoMemory();
ans = Py_BuildValue("s#", output, j); ans = Py_BuildValue(BYTES_FMT, output, j);
PyMem_Free(output); PyMem_Free(output);
PyMem_Free(b.data); PyMem_Free(b.data);
return ans; return ans;
@ -203,4 +211,3 @@ initcPalmdoc(void) {
); );
if (m == NULL) return; if (m == NULL) return;
} }

View File

@ -165,7 +165,11 @@ static PyObject* add_font(PyObject *self, PyObject *args) {
Py_ssize_t sz; Py_ssize_t sz;
DWORD num = 0; 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; if (!PyArg_ParseTuple(args, "s#", &data, &sz)) return NULL;
#endif
AddFontMemResourceEx(data, (DWORD)sz, NULL, &num); AddFontMemResourceEx(data, (DWORD)sz, NULL, &num);
@ -260,4 +264,3 @@ initwinfonts(void) {
PyModule_AddIntMacro(m, FW_HEAVY); PyModule_AddIntMacro(m, FW_HEAVY);
PyModule_AddIntMacro(m, FW_BLACK); PyModule_AddIntMacro(m, FW_BLACK);
} }

View File

@ -310,7 +310,11 @@ Compressor_compress(Compressor *self, PyObject *args, PyObject *kwds)
int flush = 0; int flush = 0;
if (!PyArg_ParseTupleAndKeywords( 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)) { args, kwds, "s#|b", kwlist, &data, &inlen, &flush)) {
#endif
return NULL; return NULL;
} }

View File

@ -157,7 +157,11 @@ decompress(PyObject *self, PyObject *args)
memory_file dest; memory_file dest;
PyObject *retval = NULL; 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)) { if (!PyArg_ParseTuple(args, "s#I", &inbuf, &inlen, &outlen)) {
#endif
return NULL; return NULL;
} }

View File

@ -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 {{{ // Loading/Opening of PDF files {{{
static PyObject * static PyObject *
PDFDoc_load(PDFDoc *self, PyObject *args) { PDFDoc_load(PDFDoc *self, PyObject *args) {
char *buffer; Py_ssize_t size; char *buffer; Py_ssize_t size;
if (PyArg_ParseTuple(args, "s#", &buffer, &size)) { if (PyArg_ParseTuple(args, BYTES_FMT, &buffer, &size)) {
try { try {
#if PODOFO_VERSION <= 0x000905 #if PODOFO_VERSION <= 0x000905
self->doc->Load(buffer, (long)size); self->doc->Load(buffer, (long)size);
@ -77,7 +83,7 @@ static PyObject *
PDFDoc_save(PDFDoc *self, PyObject *args) { PDFDoc_save(PDFDoc *self, PyObject *args) {
char *buffer; char *buffer;
if (PyArg_ParseTuple(args, "s", &buffer)) { if (PyArg_ParseTuple(args, BYTES_FMT, &buffer)) {
try { try {
self->doc->Write(buffer); self->doc->Write(buffer);
} catch(const PdfError & err) { } catch(const PdfError & err) {
@ -284,7 +290,7 @@ PDFDoc_get_xmp_metadata(PDFDoc *self, PyObject *args) {
if ((str = metadata->GetStream()) != NULL) { if ((str = metadata->GetStream()) != NULL) {
str->GetFilteredCopy(&buf, &len); str->GetFilteredCopy(&buf, &len);
if (buf != NULL) { if (buf != NULL) {
ans = Py_BuildValue("s#", buf, len); ans = Py_BuildValue(BYTES_FMT, buf, len);
free(buf); buf = NULL; free(buf); buf = NULL;
if (ans == NULL) goto error; if (ans == NULL) goto error;
} }
@ -312,7 +318,7 @@ PDFDoc_set_xmp_metadata(PDFDoc *self, PyObject *args) {
TVecFilters compressed(1); TVecFilters compressed(1);
compressed[0] = ePdfFilter_FlateDecode; compressed[0] = ePdfFilter_FlateDecode;
if (!PyArg_ParseTuple(args, "s#", &raw, &len)) return NULL; if (!PyArg_ParseTuple(args, BYTES_FMT, &raw, &len)) return NULL;
try { try {
if ((metadata = self->doc->GetMetadata()) != NULL) { if ((metadata = self->doc->GetMetadata()) != NULL) {
if ((str = metadata->GetStream()) == NULL) { PyErr_NoMemory(); goto error; } if ((str = metadata->GetStream()) == NULL) { PyErr_NoMemory(); goto error; }
@ -343,7 +349,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;
} }
@ -595,45 +605,43 @@ static PyMethodDef PDFDoc_methods[] = {
// Type definition {{{ // Type definition {{{
PyTypeObject pdf::PDFDocType = { PyTypeObject pdf::PDFDocType = {
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /*ob_size*/ /* tp_name */ "podofo.PDFDoc",
"podofo.PDFDoc", /*tp_name*/ /* tp_basicsize */ sizeof(PDFDoc),
sizeof(PDFDoc), /*tp_basicsize*/ /* tp_itemsize */ 0,
0, /*tp_itemsize*/ /* tp_dealloc */ (destructor)PDFDoc_dealloc,
(destructor)PDFDoc_dealloc, /*tp_dealloc*/ /* tp_print */ 0,
0, /*tp_print*/ /* tp_getattr */ 0,
0, /*tp_getattr*/ /* tp_setattr */ 0,
0, /*tp_setattr*/ /* tp_compare */ 0,
0, /*tp_compare*/ /* tp_repr */ 0,
0, /*tp_repr*/ /* tp_as_number */ 0,
0, /*tp_as_number*/ /* tp_as_sequence */ 0,
0, /*tp_as_sequence*/ /* tp_as_mapping */ 0,
0, /*tp_as_mapping*/ /* tp_hash */ 0,
0, /*tp_hash */ /* tp_call */ 0,
0, /*tp_call*/ /* tp_str */ 0,
0, /*tp_str*/ /* tp_getattro */ 0,
0, /*tp_getattro*/ /* tp_setattro */ 0,
0, /*tp_setattro*/ /* tp_as_buffer */ 0,
0, /*tp_as_buffer*/ /* tp_flags */ Py_TPFLAGS_DEFAULT,
Py_TPFLAGS_DEFAULT, /*tp_flags*/ /* tp_doc */ "PDF Documents",
"PDF Documents", /* tp_doc */ /* tp_traverse */ 0,
0, /* tp_traverse */ /* tp_clear */ 0,
0, /* tp_clear */ /* tp_richcompare */ 0,
0, /* tp_richcompare */ /* tp_weaklistoffset */ 0,
0, /* tp_weaklistoffset */ /* tp_iter */ 0,
0, /* tp_iter */ /* tp_iternext */ 0,
0, /* tp_iternext */ /* tp_methods */ PDFDoc_methods,
PDFDoc_methods, /* tp_methods */ /* tp_members */ 0,
0, /* tp_members */ /* tp_getset */ PDFDoc_getsetters,
PDFDoc_getsetters, /* tp_getset */ /* tp_base */ 0,
0, /* tp_base */ /* tp_dict */ 0,
0, /* tp_dict */ /* tp_descr_get */ 0,
0, /* tp_descr_get */ /* tp_descr_set */ 0,
0, /* tp_descr_set */ /* tp_dictoffset */ 0,
0, /* tp_dictoffset */ /* tp_init */ 0,
0, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ PDFDoc_new,
PDFDoc_new, /* tp_new */
}; };
// }}} // }}}

View File

@ -96,45 +96,43 @@ static PyMethodDef methods[] = {
// Type definition {{{ // Type definition {{{
PyTypeObject pdf::PDFOutlineItemType = { PyTypeObject pdf::PDFOutlineItemType = {
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /*ob_size*/ /* tp_name */ "podofo.PDFOutlineItem",
"podofo.PDFOutlineItem", /*tp_name*/ /* tp_basicsize */ sizeof(PDFOutlineItem),
sizeof(PDFOutlineItem), /*tp_basicsize*/ /* tp_itemsize */ 0,
0, /*tp_itemsize*/ /* tp_dealloc */ (destructor)dealloc,
(destructor)dealloc, /*tp_dealloc*/ /* tp_print */ 0,
0, /*tp_print*/ /* tp_getattr */ 0,
0, /*tp_getattr*/ /* tp_setattr */ 0,
0, /*tp_setattr*/ /* tp_compare */ 0,
0, /*tp_compare*/ /* tp_repr */ 0,
0, /*tp_repr*/ /* tp_as_number */ 0,
0, /*tp_as_number*/ /* tp_as_sequence */ 0,
0, /*tp_as_sequence*/ /* tp_as_mapping */ 0,
0, /*tp_as_mapping*/ /* tp_hash */ 0,
0, /*tp_hash */ /* tp_call */ 0,
0, /*tp_call*/ /* tp_str */ 0,
0, /*tp_str*/ /* tp_getattro */ 0,
0, /*tp_getattro*/ /* tp_setattro */ 0,
0, /*tp_setattro*/ /* tp_as_buffer */ 0,
0, /*tp_as_buffer*/ /* tp_flags */ Py_TPFLAGS_DEFAULT,
Py_TPFLAGS_DEFAULT, /*tp_flags*/ /* tp_doc */ "PDF Outline items",
"PDF Outline items", /* tp_doc */ /* tp_traverse */ 0,
0, /* tp_traverse */ /* tp_clear */ 0,
0, /* tp_clear */ /* tp_richcompare */ 0,
0, /* tp_richcompare */ /* tp_weaklistoffset */ 0,
0, /* tp_weaklistoffset */ /* tp_iter */ 0,
0, /* tp_iter */ /* tp_iternext */ 0,
0, /* tp_iternext */ /* tp_methods */ methods,
methods, /* tp_methods */ /* tp_members */ 0,
0, /* tp_members */ /* tp_getset */ 0,
0, /* tp_getset */ /* tp_base */ 0,
0, /* tp_base */ /* tp_dict */ 0,
0, /* tp_dict */ /* tp_descr_get */ 0,
0, /* tp_descr_get */ /* tp_descr_set */ 0,
0, /* tp_descr_set */ /* tp_dictoffset */ 0,
0, /* tp_dictoffset */ /* tp_init */ 0,
0, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ new_item,
new_item, /* tp_new */
}; };
// }}} // }}}

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;
} }

View File

@ -10,10 +10,6 @@ using namespace PoDoFo;
PyObject *pdf::Error = NULL; PyObject *pdf::Error = NULL;
static PyMethodDef podofo_methods[] = {
{NULL} /* Sentinel */
};
class PyLogMessage : public PdfError::LogMessageCallback { class PyLogMessage : public PdfError::LogMessageCallback {
public: public:
@ -38,29 +34,63 @@ class PyLogMessage : public PdfError::LogMessageCallback {
PyLogMessage log_message; PyLogMessage log_message;
CALIBRE_MODINIT_FUNC static char podofo_doc[] = "Wrapper for the PoDoFo PDF library";
initpodofo(void)
{ 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; PyObject* m;
if (PyType_Ready(&pdf::PDFDocType) < 0) if (PyType_Ready(&pdf::PDFDocType) < 0) {
return; INITERROR;
}
if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) {
return; INITERROR;
}
pdf::Error = PyErr_NewException((char*)"podofo.Error", NULL, NULL); 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::SetLogMessageCallback((PdfError::LogMessageCallback*)&log_message);
PdfError::EnableDebug(false); 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); Py_INCREF(&pdf::PDFDocType);
PyModule_AddObject(m, "PDFDoc", (PyObject *)&pdf::PDFDocType); PyModule_AddObject(m, "PDFDoc", (PyObject *)&pdf::PDFDocType);
PyModule_AddObject(m, "Error", pdf::Error); PyModule_AddObject(m, "Error", pdf::Error);
}
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}

View File

@ -58,6 +58,12 @@ static PyObject *LZMAError = NULL;
// Utils {{{ // Utils {{{
static UInt64 crc64_table[256]; 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 void init_crc_table() {
static const UInt64 poly64 = (UInt64)(0xC96C5795D7870F42); static const UInt64 poly64 = (UInt64)(0xC96C5795D7870F42);
size_t i, j; size_t i, j;
@ -79,7 +85,7 @@ crc64(PyObject *self, PyObject *args) {
Py_ssize_t size = 0; Py_ssize_t size = 0;
UInt64 crc = 0; UInt64 crc = 0;
size_t i; 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; crc = ~crc;
for (i = 0; i < (size_t)size; ++i) for (i = 0; i < (size_t)size; ++i)
crc = crc64_table[data[i] ^ (crc & 0xFF)] ^ (crc >> 8); 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; } } else { res = SZ_OK; bytes_written = 0; status = LZMA_STATUS_NEEDS_MORE_INPUT; }
if (res != SZ_OK) { SET_ERROR(res); goto exit; } if (res != SZ_OK) { SET_ERROR(res); goto exit; }
if (bytes_written > 0) { 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) { 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; SET_ERROR(SZ_ERROR_DATA); goto exit;
@ -188,7 +194,7 @@ decompress(PyObject *self, PyObject *args) {
ELzmaStatus status = LZMA_STATUS_NOT_FINISHED; ELzmaStatus status = LZMA_STATUS_NOT_FINISHED;
ELzmaFinishMode finish_mode = LZMA_FINISH_ANY; 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); size_known = (decompressed_size != (UInt64)(Int64)-1);
if (header_size != 13) { PyErr_SetString(LZMAError, "Header must be exactly 13 bytes long"); return NULL; } 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; } 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; } } else { res = SZ_OK; bytes_written = 0; status = LZMA_STATUS_NEEDS_MORE_INPUT; }
if (res != SZ_OK) { SET_ERROR(res); goto exit; } if (res != SZ_OK) { SET_ERROR(res); goto exit; }
if (bytes_written > 0) { 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; 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) { 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; PyObject *res = NULL;
if (!size) return 0; if (!size) return 0;
ACQUIRE_GIL 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; if (res == NULL) return 0;
Py_DECREF(res); Py_DECREF(res);
RELEASE_GIL RELEASE_GIL
@ -332,7 +338,7 @@ get_lzma2_properties(int preset) {
exit: exit:
if (lzma2) Lzma2Enc_Destroy(lzma2); if (lzma2) Lzma2Enc_Destroy(lzma2);
if (PyErr_Occurred()) return NULL; 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: exit:
if (lzma2) Lzma2Enc_Destroy(lzma2); if (lzma2) Lzma2Enc_Destroy(lzma2);
if (PyErr_Occurred()) return NULL; if (PyErr_Occurred()) return NULL;
return Py_BuildValue("s#", &props_out, 1); return Py_BuildValue(BYTES_FMT, &props_out, 1);
} }
// }}} // }}}