From 0e635e8c5d97b4ab86a16678c38cd43c8086679e Mon Sep 17 00:00:00 2001 From: Martin Wilck Date: Wed, 21 Mar 2018 23:55:12 +0100 Subject: [PATCH 07/33] 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 131e5e9..e1eb3e1 100644 --- a/pcard/pcardext/pcardext.c +++ b/pcard/pcardext/pcardext.c @@ -39,6 +39,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; @@ -81,8 +87,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 ); } @@ -233,9 +242,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, @@ -246,4 +277,5 @@ void initpcardext( void ) return; } +#endif -- 2.52.0