From 74ed15a16e7564d0665afc68defbd6d03bdff21d Mon Sep 17 00:00:00 2001 From: Martin Wilck Date: Wed, 21 Mar 2018 23:55:12 +0100 Subject: [PATCH] pcardext: python3 fixes --- pcard/pcardext/pcardext.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/pcard/pcardext/pcardext.c b/pcard/pcardext/pcardext.c index c1a8273e4359..cdf260d62223 100644 --- a/pcard/pcardext/pcardext.c +++ b/pcard/pcardext/pcardext.c @@ -36,6 +36,12 @@ typedef int Py_ssize_t; #define PY_SSIZE_T_MIN INT_MIN #endif +#if PY_MAJOR_VERSION >= 3 +#define PyString_AsStringAndSize PyBytes_AsStringAndSize +#define PyString_FromStringAndSize PyBytes_FromStringAndSize +#define PyInt_AS_LONG PyLong_AS_LONG +#endif + int verbose=0; PyObject * readsectorFunc = NULL; @@ -78,8 +84,11 @@ int WriteSector(int sector, int nsector, void *buf, int size ) if( writesectorFunc ) { +#if PY_MAJOR_VERSION >= 3 result = PyObject_CallFunction( writesectorFunc, "iis#", sector, nsector, buf, size ); - +#else + result = PyObject_CallFunction( writesectorFunc, "iiy#", sector, nsector, buf, size ); +#endif return PyInt_AS_LONG( result ); } @@ -230,9 +239,31 @@ static PyMethodDef pcardext_methods[] = { NULL, NULL } }; - static char pcardext_documentation[] = "Python extension for HP photocard services"; +#if PY_MAJOR_VERSION >= 3 +static struct PyModuleDef moduledef = { + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "pcardext", + .m_doc = pcardext_documentation, + .m_size = -1, + .m_methods = pcardext_methods, + .m_slots = NULL, + .m_traverse = NULL, + .m_clear = NULL, + .m_free = NULL, +}; + +PyMODINIT_FUNC +PyInit_pcardext( void ) +{ + PyObject *module = PyModule_Create2(&moduledef, PYTHON_API_VERSION); + + return module; +} + +#else + void initpcardext( void ) { PyObject * mod = Py_InitModule4( "pcardext", pcardext_methods, @@ -243,4 +274,5 @@ void initpcardext( void ) return; } +#endif -- 2.16.1