Port lzx plugin to build on python2/python3

This commit is contained in:
Eli Schwartz 2018-12-17 12:38:20 -05:00
parent fdcd6d746c
commit e20566c7fc
2 changed files with 92 additions and 74 deletions

View File

@ -81,7 +81,7 @@ Compressor_dealloc(Compressor *self)
self->output.data = NULL; self->output.data = NULL;
} }
self->ob_type->tp_free((PyObject *)self); Py_TYPE(self)->tp_free((PyObject *)self);
} }
static PyObject * static PyObject *
@ -288,7 +288,7 @@ Compressor_compress__(
self->rtable = rtable; self->rtable = rtable;
return NULL; return NULL;
} }
cdata = PyString_FromStringAndSize(output->data, output->offset); cdata = PyBytes_FromStringAndSize(output->data, output->offset);
if (cdata == NULL) { if (cdata == NULL) {
Py_DECREF(rtable); Py_DECREF(rtable);
return NULL; return NULL;
@ -333,43 +333,42 @@ static PyMethodDef Compressor_methods[] = {
}; };
PyTypeObject CompressorType = { PyTypeObject CompressorType = {
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /*ob_size*/ /* tp_name */ "lzx.Compressor",
"lzx.Compressor", /*tp_name*/ /* tp_basicsize */ sizeof(Compressor),
sizeof(Compressor), /*tp_basicsize*/ /* tp_itemsize */ 0,
0, /*tp_itemsize*/ /* tp_dealloc */ (destructor)Compressor_dealloc,
(destructor)Compressor_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_HAVE_GC,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ /* tp_doc */ "Compressor objects",
"Compressor objects", /* tp_doc */ /* tp_traverse */ (traverseproc)Compressor_traverse,
(traverseproc)Compressor_traverse, /* tp_traverse */ /* tp_clear */ (inquiry)Compressor_clear,
(inquiry)Compressor_clear, /* 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 */ Compressor_methods,
Compressor_methods, /* tp_methods */ /* tp_members */ Compressor_members,
Compressor_members, /* 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 */ (initproc)Compressor_init,
(initproc)Compressor_init, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ Compressor_new,
Compressor_new, /* tp_new */
}; };

View File

@ -161,11 +161,11 @@ decompress(PyObject *self, PyObject *args)
return NULL; return NULL;
} }
retval = PyString_FromStringAndSize(NULL, outlen); retval = PyBytes_FromStringAndSize(NULL, outlen);
if (retval == NULL) { if (retval == NULL) {
return NULL; return NULL;
} }
outbuf = (unsigned char *)PyString_AS_STRING(retval); outbuf = (unsigned char *)PyBytes_AS_STRING(retval);
source.magic = 0xB5; source.magic = 0xB5;
source.buffer = inbuf; source.buffer = inbuf;
@ -202,18 +202,35 @@ static PyMethodDef lzx_methods[] = {
{ NULL } { NULL }
}; };
CALIBRE_MODINIT_FUNC #if PY_MAJOR_VERSION >= 3
initlzx(void) #define INITERROR return NULL
{ #define INITMODULE PyModule_Create(&lzx_module)
PyObject *m; static struct PyModuleDef lzx_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "lzx",
/* m_doc */ lzx_doc,
/* m_size */ -1,
/* m_methods */ lzx_methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC PyInit_lzx(void) {
#else
#define INITERROR return
#define INITMODULE Py_InitModule3("lzx", lzx_methods, lzx_doc);
CALIBRE_MODINIT_FUNC initlzx(void) {
#endif
if (PyType_Ready(&CompressorType) < 0) { if (PyType_Ready(&CompressorType) < 0) {
return; INITERROR;
} }
m = Py_InitModule3("lzx", lzx_methods, lzx_doc); PyObject *m = INITMODULE;
if (m == NULL) { if (m == NULL) {
return; INITERROR;
} }
LZXError = PyErr_NewException("lzx.LZXError", NULL, NULL); LZXError = PyErr_NewException("lzx.LZXError", NULL, NULL);
@ -223,5 +240,7 @@ initlzx(void)
Py_INCREF(&CompressorType); Py_INCREF(&CompressorType);
PyModule_AddObject(m, "Compressor", (PyObject *)&CompressorType); PyModule_AddObject(m, "Compressor", (PyObject *)&CompressorType);
return; #if PY_MAJOR_VERSION >= 3
return m;
#endif
} }