xtruder-archive / pylirc2

Python ir library.
https://code.launchpad.net/pylirc2
GNU Lesser General Public License v2.1
6 stars 3 forks source link

pylirc is not loadable for python3 #3

Open ghost opened 8 years ago

ghost commented 8 years ago

Hello! Thank you for pylirc. Do you have any plans to support pylirc for python3? Now pylirc is not loadable. I am not an expert of python. But I think it is because Py_InitModule() does not exist in python3. PyModule_Create() should be used instead. https://docs.python.org/3/howto/cporting.html I have installed pylirc as pip3 install pylirc2. Got an error python3 -c 'import pylirc'
Traceback (most recent call last): File "", line 1, in ImportError: No module named 'pylirc'

with -v option Traceback (most recent call last): File "", line 1, in File "", line 2237, in _find_and_load File "", line 2224, in _find_and_load_unlocked ImportError: No module named 'pylirc' if I rename /usr/local/lib/python3.4/dist-packages/pylircmodule.cpython-34m.so to /usr/local/lib/python3.4/dist-packages/pylirc.cpython-34m.so Then I got python3 -c 'import pylirc'
Traceback (most recent call last): File "", line 1, in ImportError: /usr/local/lib/python3.4/dist-packages/pylirc.cpython-34m.so: undefined symbol: Py_InitModule

ghost commented 8 years ago

To make pylirc work with python3 I added at then end of pylircmodule.c file

if PY_MAJOR_VERSION >= 3

struct module_state { PyObject *error; };

define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))

static int pylircTraverse(PyObject m, visitproc visit, void arg) { Py_VISIT(GETSTATE(m)->error); return 0; }

static int pylircClear(PyObject *m) { Py_CLEAR(GETSTATE(m)->error); return 0; }

static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "pylirc", NULL, sizeof(struct module_state), pylircMethods, NULL, pylircTraverse, pylircClear, NULL };

PyObject * PyInit_pylirc(void) { PyObject module = PyModule_Create(&moduledef); if (module == NULL) return NULL; struct module_state st = GETSTATE(module);

st->error = PyErr_NewException("pylirc.Error", NULL, NULL);
if (st->error == NULL) {
    Py_DECREF(module);
    return NULL;
}

return module;

}

endif

And you there is a need to avoid warning pylircmodule.c:271:5: warning: implicit declaration of function ‘Py_InitModule’ [-Wimplicit-function-declaration] place // Python init function void initpylirc(void) { (void) Py_InitModule("pylirc", pylircMethods); } to else at the end like this

... return module; }

else

// Python init function void initpylirc(void) { (void) Py_InitModule("pylirc", pylircMethods); }

endif

ghost commented 8 years ago

For me this is work on raspbian

lsb_release -a No LSB modules are available. Distributor ID: Raspbian Description: Raspbian GNU/Linux 8.0 (jessie) Release: 8.0 Codename: jessie

ghost commented 8 years ago

But also there is a need to rename /usr/local/lib/python3.4/dist-packages/pylircmodule.cpython-34m.so -> /usr/local/lib/python3.4/dist-packages/pylirc.cpython-34m.so

jpstotz commented 7 years ago

Could you please clone the repository and commit your changes? I tried to follow the changes you described, however I don't understand what you mean in the section about "avoid warning pylircmodule.c:271:5:".