mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
C extensions: format strings with the right type of values on python2/python3
This commit is contained in:
parent
a14454a857
commit
c0754900fe
@ -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;
|
||||||
|
@ -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));
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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; }
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user