Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pylirc is not loadable for python3 #3

Open
ghost opened this issue Jan 9, 2016 · 4 comments
Open

pylirc is not loadable for python3 #3

ghost opened this issue Jan 9, 2016 · 4 comments

Comments

@ghost
Copy link

ghost commented Jan 9, 2016

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
Copy link
Author

ghost commented Jan 9, 2016

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
Copy link
Author

ghost commented Jan 9, 2016

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
Copy link
Author

ghost commented Jan 9, 2016

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
Copy link

jpstotz commented Feb 14, 2017

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:".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant