C extensions: format strings with the right type of values on python2/python3

This commit is contained in:
Eli Schwartz 2019-03-10 00:10:14 -05:00
parent a14454a857
commit c0754900fe
8 changed files with 55 additions and 17 deletions

View File

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

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

View File

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

View File

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

View File

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

View File

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

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

View File

@ -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);
}
// }}}