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

View File

@ -161,11 +161,11 @@ decompress(PyObject *self, PyObject *args)
return NULL;
}
retval = PyString_FromStringAndSize(NULL, outlen);
retval = PyBytes_FromStringAndSize(NULL, outlen);
if (retval == NULL) {
return NULL;
}
outbuf = (unsigned char *)PyString_AS_STRING(retval);
outbuf = (unsigned char *)PyBytes_AS_STRING(retval);
source.magic = 0xB5;
source.buffer = inbuf;
@ -202,18 +202,35 @@ static PyMethodDef lzx_methods[] = {
{ NULL }
};
CALIBRE_MODINIT_FUNC
initlzx(void)
{
PyObject *m;
#if PY_MAJOR_VERSION >= 3
#define INITERROR return NULL
#define INITMODULE PyModule_Create(&lzx_module)
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) {
return;
INITERROR;
}
m = Py_InitModule3("lzx", lzx_methods, lzx_doc);
PyObject *m = INITMODULE;
if (m == NULL) {
return;
INITERROR;
}
LZXError = PyErr_NewException("lzx.LZXError", NULL, NULL);
@ -223,5 +240,7 @@ initlzx(void)
Py_INCREF(&CompressorType);
PyModule_AddObject(m, "Compressor", (PyObject *)&CompressorType);
return;
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}