From 1bf570e7220467d45002e671f560bb9c106588e6 Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Mon, 26 Mar 2018 18:32:03 +0200 Subject: [PATCH] initial python3 support Signed-off-by: Jaroslav Kysela --- pyalsa/alsacard.c | 28 +- pyalsa/alsacontrol.c | 71 ++--- pyalsa/alsahcontrol.c | 155 +++++---- pyalsa/alsamixer.c | 86 ++--- pyalsa/alsaseq.c | 716 ++++++++++++++---------------------------- pyalsa/common.h | 128 ++++++++ setup.py | 44 ++- test/alsamemdebug.py | 8 +- test/cardtest1.py | 16 +- test/ctltest1.py | 22 +- test/hctltest1.py | 48 +-- test/hctltest2.py | 20 +- test/mixertest1.py | 62 ++-- 13 files changed, 612 insertions(+), 792 deletions(-) create mode 100644 pyalsa/common.h diff --git a/pyalsa/alsacard.c b/pyalsa/alsacard.c index 48129f4..1165736 100644 --- a/pyalsa/alsacard.c +++ b/pyalsa/alsacard.c @@ -19,26 +19,11 @@ * */ -#include "Python.h" -#include "structmember.h" -#include "frameobject.h" -#ifndef PY_LONG_LONG - #define PY_LONG_LONG LONG_LONG -#endif +#include "common.h" #include "sys/poll.h" #include "stdlib.h" #include "alsa/asoundlib.h" -#ifndef Py_RETURN_NONE -#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None -#endif -#ifndef Py_RETURN_TRUE -#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True -#endif -#ifndef Py_RETURN_FALSE -#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False -#endif - static PyObject *module; /* @@ -189,7 +174,7 @@ device_name_hint(PyObject *self, PyObject *args, PyObject *kwds) str = snd_device_name_get_hint(*hint, *id); if (str == NULL) break; - v = PyString_FromString(str); + v = PyUnicode_FromString(str); free(str); if (v == NULL) goto err1; @@ -227,13 +212,14 @@ static PyMethodDef pyalsacardparse_methods[] = { {NULL} }; -PyMODINIT_FUNC -initalsacard(void) +MOD_INIT(alsacard) { - module = Py_InitModule3("alsacard", pyalsacardparse_methods, "libasound alsacard wrapper"); + MOD_DEF(module, "alsacard", "libasound alsacard wrapper", pyalsacardparse_methods); if (module == NULL) - return; + return MOD_ERROR_VAL; if (PyErr_Occurred()) Py_FatalError("Cannot initialize module alsacard"); + + return MOD_SUCCESS_VAL(module); } diff --git a/pyalsa/alsacontrol.c b/pyalsa/alsacontrol.c index 9c13e84..d01d32e 100644 --- a/pyalsa/alsacontrol.c +++ b/pyalsa/alsacontrol.c @@ -19,24 +19,13 @@ * */ -#include "Python.h" -#include "structmember.h" -#include "frameobject.h" -#ifndef PY_LONG_LONG - #define PY_LONG_LONG LONG_LONG -#endif +#include "common.h" #include "stdlib.h" #include "alsa/asoundlib.h" -#ifndef Py_RETURN_NONE -#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None -#endif -#ifndef Py_RETURN_TRUE -#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True -#endif -#ifndef Py_RETURN_FALSE -#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False -#endif +/* + * + */ static PyObject *module; #if 0 @@ -47,25 +36,14 @@ static PyObject *buildin; * */ -#define PYCTL(v) (((v) == Py_None) ? NULL : \ - ((struct pyalsacontrol *)(v))) +#define PYCTL(v) \ + (((v) == Py_None) ? NULL : ((struct pyalsacontrol *)(v))) struct pyalsacontrol { PyObject_HEAD snd_ctl_t *handle; }; -static inline PyObject *get_bool(int val) -{ - if (val) { - Py_INCREF(Py_True); - return Py_True; - } else { - Py_INCREF(Py_False); - return Py_False; - } -} - PyDoc_STRVAR(cardinfo__doc__, "card_info() -- Return a dictionary with card specific information."); @@ -84,13 +62,13 @@ pyalsacontrol_cardinfo(struct pyalsacontrol *self, PyObject *args) } d = PyDict_New(); if (d) { - PyDict_SetItem(d, PyString_FromString("card"), PyInt_FromLong(snd_ctl_card_info_get_card(info))); - PyDict_SetItem(d, PyString_FromString("id"), PyString_FromString(snd_ctl_card_info_get_id(info))); - PyDict_SetItem(d, PyString_FromString("driver"), PyString_FromString(snd_ctl_card_info_get_driver(info))); - PyDict_SetItem(d, PyString_FromString("name"), PyString_FromString(snd_ctl_card_info_get_driver(info))); - PyDict_SetItem(d, PyString_FromString("longname"), PyString_FromString(snd_ctl_card_info_get_longname(info))); - PyDict_SetItem(d, PyString_FromString("mixername"), PyString_FromString(snd_ctl_card_info_get_mixername(info))); - PyDict_SetItem(d, PyString_FromString("components"), PyString_FromString(snd_ctl_card_info_get_components(info))); + PyDict_SetItem(d, PyUnicode_FromString("card"), PyInt_FromLong(snd_ctl_card_info_get_card(info))); + PyDict_SetItem(d, PyUnicode_FromString("id"), PyUnicode_FromString(snd_ctl_card_info_get_id(info))); + PyDict_SetItem(d, PyUnicode_FromString("driver"), PyUnicode_FromString(snd_ctl_card_info_get_driver(info))); + PyDict_SetItem(d, PyUnicode_FromString("name"), PyUnicode_FromString(snd_ctl_card_info_get_driver(info))); + PyDict_SetItem(d, PyUnicode_FromString("longname"), PyUnicode_FromString(snd_ctl_card_info_get_longname(info))); + PyDict_SetItem(d, PyUnicode_FromString("mixername"), PyUnicode_FromString(snd_ctl_card_info_get_mixername(info))); + PyDict_SetItem(d, PyUnicode_FromString("components"), PyUnicode_FromString(snd_ctl_card_info_get_components(info))); } return d; } @@ -180,8 +158,6 @@ pyalsacontrol_dealloc(struct pyalsacontrol *self) { if (self->handle != NULL) snd_ctl_close(self->handle); - - self->ob_type->tp_free(self); } static PyGetSetDef pyalsacontrol_getseters[] = { @@ -199,7 +175,7 @@ static PyMethodDef pyalsacontrol_methods[] = { }; static PyTypeObject pyalsacontrol_type = { - PyObject_HEAD_INIT(0) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsacontrol.Control", tp_basicsize: sizeof(struct pyalsacontrol), tp_dealloc: (destructor)pyalsacontrol_dealloc, @@ -221,25 +197,26 @@ static PyMethodDef pyalsacontrolparse_methods[] = { {NULL} }; -PyMODINIT_FUNC -initalsacontrol(void) +MOD_INIT(alsacontrol) { PyObject *d, *d1, *l1, *o; int i; + pyalsacontrol_type.tp_free = PyObject_GC_Del; + if (PyType_Ready(&pyalsacontrol_type) < 0) - return; + return MOD_ERROR_VAL; - module = Py_InitModule3("alsacontrol", pyalsacontrolparse_methods, "libasound control wrapper"); + MOD_DEF(module, "alsacontrol", "libasound control wrapper", pyalsacontrolparse_methods); if (module == NULL) - return; + return MOD_ERROR_VAL; #if 0 buildin = PyImport_AddModule("__buildin__"); if (buildin == NULL) - return; + return MOD_ERROR_VAL; if (PyObject_SetAttrString(module, "__buildins__", buildin) < 0) - return; + return MOD_ERROR_VAL; #endif Py_INCREF(&pyalsacontrol_type); @@ -273,7 +250,7 @@ initalsacontrol(void) l1 = PyList_New(0); for (i = 0; i <= SND_CTL_ELEM_IFACE_LAST; i++) { - o = PyString_FromString(snd_ctl_elem_iface_name(i)); + o = PyUnicode_FromString(snd_ctl_elem_iface_name(i)); PyList_Append(l1, o); Py_DECREF(o); } @@ -301,4 +278,6 @@ initalsacontrol(void) if (PyErr_Occurred()) Py_FatalError("Cannot initialize module alsacontrol"); + + return MOD_SUCCESS_VAL(module); } diff --git a/pyalsa/alsahcontrol.c b/pyalsa/alsahcontrol.c index 6442924..cffdc59 100644 --- a/pyalsa/alsahcontrol.c +++ b/pyalsa/alsahcontrol.c @@ -19,26 +19,11 @@ * */ -#include "Python.h" -#include "structmember.h" -#include "frameobject.h" -#ifndef PY_LONG_LONG - #define PY_LONG_LONG LONG_LONG -#endif +#include "common.h" #include "sys/poll.h" #include "stdlib.h" #include "alsa/asoundlib.h" -#ifndef Py_RETURN_NONE -#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None -#endif -#ifndef Py_RETURN_TRUE -#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True -#endif -#ifndef Py_RETURN_FALSE -#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False -#endif - static int element_callback(snd_hctl_elem_t *elem, unsigned int mask); static PyObject *module; @@ -59,17 +44,6 @@ struct pyalsahcontrol { snd_hctl_t *handle; }; -static inline PyObject *get_bool(int val) -{ - if (val) { - Py_INCREF(Py_True); - return Py_True; - } else { - Py_INCREF(Py_False); - return Py_False; - } -} - static PyObject *id_to_python(snd_ctl_elem_id_t *id) { PyObject *v; @@ -81,7 +55,7 @@ static PyObject *id_to_python(snd_ctl_elem_id_t *id) PyTuple_SET_ITEM(v, 1, PyInt_FromLong(snd_ctl_elem_id_get_interface(id))); PyTuple_SET_ITEM(v, 2, PyInt_FromLong(snd_ctl_elem_id_get_device(id))); PyTuple_SET_ITEM(v, 3, PyInt_FromLong(snd_ctl_elem_id_get_subdevice(id))); - PyTuple_SET_ITEM(v, 4, PyString_FromString(snd_ctl_elem_id_get_name(id))); + PyTuple_SET_ITEM(v, 4, PyUnicode_FromString(snd_ctl_elem_id_get_name(id))); PyTuple_SET_ITEM(v, 5, PyInt_FromLong(snd_ctl_elem_id_get_index(id))); return v; } @@ -180,7 +154,7 @@ pyalsahcontrol_registerpoll(struct pyalsahcontrol *self, PyObject *args) if (count <= 0) Py_RETURN_NONE; - reg = PyObject_GetAttr(pollObj, PyString_InternFromString("register")); + reg = PyObject_GetAttr(pollObj, InternFromString("register")); for (i = 0; i < count; i++) { t = PyTuple_New(2); @@ -273,7 +247,7 @@ PyDoc_STRVAR(elementnew__doc__, static PyObject * pyalsahcontrol_elementnew(struct pyalsahcontrol *self, PyObject *args) { - snd_ctl_elem_type_t type; + long type; PyObject *o; unsigned int count; long min, max, step; @@ -288,12 +262,11 @@ pyalsahcontrol_elementnew(struct pyalsahcontrol *self, PyObject *args) return NULL; } o = PyTuple_GetItem(args, 0); - if (!PyInt_Check(o)) { + if (get_long1(o, &type)) { PyErr_SetString(PyExc_TypeError, "type argument is not integer"); return NULL; } Py_INCREF(o); - type = PyInt_AsLong(o); o = PyTuple_GetItem(args, 1); if (!PyTuple_Check(o)) { PyErr_SetString(PyExc_TypeError, "id argument is not tuple"); @@ -317,7 +290,7 @@ pyalsahcontrol_elementnew(struct pyalsahcontrol *self, PyObject *args) return NULL; break; default: - PyErr_Format(PyExc_TypeError, "type %i is not supported yet", type); + PyErr_Format(PyExc_TypeError, "type %li is not supported yet", type); return NULL; } @@ -342,7 +315,7 @@ pyalsahcontrol_elementnew(struct pyalsahcontrol *self, PyObject *args) break; } if (res < 0) { - PyErr_Format(PyExc_IOError, "new element of type %i create error: %s", type, snd_strerror(-res)); + PyErr_Format(PyExc_IOError, "new element of type %li create error: %s", type, snd_strerror(-res)); return NULL; } Py_RETURN_NONE; @@ -424,8 +397,6 @@ pyalsahcontrol_dealloc(struct pyalsahcontrol *self) { if (self->handle != NULL) snd_hctl_close(self->handle); - - self->ob_type->tp_free(self); } static PyGetSetDef pyalsahcontrol_getseters[] = { @@ -450,7 +421,7 @@ static PyMethodDef pyalsahcontrol_methods[] = { }; static PyTypeObject pyalsahcontrol_type = { - PyObject_HEAD_INIT(0) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsahcontrol.HControl", tp_basicsize: sizeof(struct pyalsahcontrol), tp_dealloc: (destructor)pyalsahcontrol_dealloc, @@ -482,7 +453,7 @@ struct pyalsahcontrolelement { static PyObject * pyalsahcontrolelement_getname(struct pyalsahcontrolelement *pyhelem, void *priv) { - return PyString_FromString(snd_hctl_elem_get_name(pyhelem->elem)); + return PyUnicode_FromString(snd_hctl_elem_get_name(pyhelem->elem)); } typedef unsigned int (*fcn1)(void *); @@ -588,10 +559,16 @@ pyalsahcontrolelement_init(struct pyalsahcontrolelement *pyhelem, PyObject *args if (PyFloat_Check(first)) { if (!PyArg_ParseTuple(args, "Ofi", &hctl, &f, &helem)) return -1; + } else if (PyLong_Check(first)) { + if (!PyArg_ParseTuple(args, "Oi", &hctl, &numid)) + return -1; + snd_ctl_elem_id_set_numid(id, numid); +#if PY_MAJOR_VERSION < 3 } else if (PyInt_Check(first)) { if (!PyArg_ParseTuple(args, "Oi", &hctl, &numid)) return -1; snd_ctl_elem_id_set_numid(id, numid); +#endif } else if (PyTuple_Check(first)) { if (!PyArg_ParseTuple(args, "OO", &hctl, &first)) return -1; @@ -649,8 +626,6 @@ pyalsahcontrolelement_dealloc(struct pyalsahcontrolelement *self) if (self->pyhandle) { Py_XDECREF(self->pyhandle); } - - self->ob_type->tp_free(self); } static PyGetSetDef pyalsahcontrolelement_getseters[] = { @@ -677,7 +652,7 @@ static PyMethodDef pyalsahcontrolelement_methods[] = { }; static PyTypeObject pyalsahcontrolelement_type = { - PyObject_HEAD_INIT(0) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsahcontrol.Element", tp_basicsize: sizeof(struct pyalsahcontrolelement), tp_dealloc: (destructor)pyalsahcontrolelement_dealloc, @@ -773,7 +748,7 @@ typedef const char * (*fcn5)(void *); static PyObject * pyalsahcontrolinfo_str(struct pyalsahcontrolinfo *pyinfo, void *fcn) { - return PyString_FromString(((fcn5)fcn)(pyinfo->info)); + return PyUnicode_FromString(((fcn5)fcn)(pyinfo->info)); } static PyObject * @@ -790,7 +765,7 @@ static PyObject * pyalsahcontrolinfo_dimensions(struct pyalsahcontrolinfo *pyinfo, void *priv) { int dims = snd_ctl_elem_info_get_dimensions(pyinfo->info); - unsigned int i; + int i; PyObject *t; if (dims <= 0) @@ -809,7 +784,7 @@ pyalsahcontrolinfo_itemnames(struct pyalsahcontrolinfo *pyinfo, void *priv) { int items; int res; - unsigned int i; + int i; PyObject *t; if (snd_ctl_elem_info_get_type(pyinfo->info) != SND_CTL_ELEM_TYPE_ENUMERATED) { @@ -829,7 +804,7 @@ pyalsahcontrolinfo_itemnames(struct pyalsahcontrolinfo *pyinfo, void *priv) Py_INCREF(Py_None); PyTuple_SET_ITEM(t, i, Py_None); } else { - PyTuple_SET_ITEM(t, i, PyString_FromString(snd_ctl_elem_info_get_item_name(pyinfo->info))); + PyTuple_SET_ITEM(t, i, PyUnicode_FromString(snd_ctl_elem_info_get_item_name(pyinfo->info))); } } return t; @@ -883,8 +858,6 @@ pyalsahcontrolinfo_dealloc(struct pyalsahcontrolinfo *self) if (self->pyelem) { Py_XDECREF(self->pyelem); } - - self->ob_type->tp_free(self); } static PyGetSetDef pyalsahcontrolinfo_getseters[] = { @@ -934,7 +907,7 @@ static PyMethodDef pyalsahcontrolinfo_methods[] = { }; static PyTypeObject pyalsahcontrolinfo_type = { - PyObject_HEAD_INIT(0) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsahcontrol.Info", tp_basicsize: sizeof(struct pyalsahcontrolinfo), tp_dealloc: (destructor)pyalsahcontrolinfo_dealloc, @@ -982,7 +955,7 @@ typedef const char * (*fcn11)(void *); static PyObject * pyalsahcontrolvalue_str(struct pyalsahcontrolvalue *pyvalue, void *fcn) { - return PyString_FromString(((fcn5)fcn)(pyvalue->value)); + return PyUnicode_FromString(((fcn5)fcn)(pyvalue->value)); } static PyObject * @@ -1080,13 +1053,13 @@ pyalsahcontrolvalue_get1(struct pyalsahcontrolvalue *self, PyObject *args, int l } snd_ctl_elem_value_get_iec958(self->value, iec958); if (!list) { - PyTuple_SET_ITEM(t, 0, PyString_FromStringAndSize((char *)iec958->status, sizeof(iec958->status))); - PyTuple_SET_ITEM(t, 1, PyString_FromStringAndSize((char *)iec958->subcode, sizeof(iec958->subcode))); - PyTuple_SET_ITEM(t, 2, PyString_FromStringAndSize((char *)iec958->dig_subframe, sizeof(iec958->dig_subframe))); + PyTuple_SET_ITEM(t, 0, PyUnicode_FromStringAndSize((char *)iec958->status, sizeof(iec958->status))); + PyTuple_SET_ITEM(t, 1, PyUnicode_FromStringAndSize((char *)iec958->subcode, sizeof(iec958->subcode))); + PyTuple_SET_ITEM(t, 2, PyUnicode_FromStringAndSize((char *)iec958->dig_subframe, sizeof(iec958->dig_subframe))); } else { - PyList_SetItem(t, 0, PyString_FromStringAndSize((char *)iec958->status, sizeof(iec958->status))); - PyList_SetItem(t, 1, PyString_FromStringAndSize((char *)iec958->subcode, sizeof(iec958->subcode))); - PyList_SetItem(t, 2, PyString_FromStringAndSize((char *)iec958->dig_subframe, sizeof(iec958->dig_subframe))); + PyList_SetItem(t, 0, PyUnicode_FromStringAndSize((char *)iec958->status, sizeof(iec958->status))); + PyList_SetItem(t, 1, PyUnicode_FromStringAndSize((char *)iec958->subcode, sizeof(iec958->subcode))); + PyList_SetItem(t, 2, PyUnicode_FromStringAndSize((char *)iec958->dig_subframe, sizeof(iec958->dig_subframe))); } free(iec958); break; @@ -1124,7 +1097,7 @@ pyalsahcontrolvalue_settuple(struct pyalsahcontrolvalue *self, PyObject *args) { int type, list; Py_ssize_t len; - long i, count; + long i, count, lval; snd_aes_iec958_t *iec958; PyObject *t, *v; char *str; @@ -1147,7 +1120,9 @@ pyalsahcontrolvalue_settuple(struct pyalsahcontrolvalue *self, PyObject *args) if (v == Py_None) continue; Py_INCREF(v); - snd_ctl_elem_value_set_boolean(self->value, i, PyInt_AsLong(v)); + if (get_long(v, &lval)) + break; + snd_ctl_elem_value_set_boolean(self->value, i, lval); } break; case SND_CTL_ELEM_TYPE_INTEGER: @@ -1156,7 +1131,9 @@ pyalsahcontrolvalue_settuple(struct pyalsahcontrolvalue *self, PyObject *args) if (v == Py_None) continue; Py_INCREF(v); - snd_ctl_elem_value_set_integer(self->value, i, PyInt_AsLong(v)); + if (get_long(v, &lval)) + break; + snd_ctl_elem_value_set_integer(self->value, i, lval); } break; case SND_CTL_ELEM_TYPE_INTEGER64: @@ -1174,7 +1151,9 @@ pyalsahcontrolvalue_settuple(struct pyalsahcontrolvalue *self, PyObject *args) if (v == Py_None) continue; Py_INCREF(v); - snd_ctl_elem_value_set_enumerated(self->value, i, PyInt_AsLong(v)); + if (get_long(v, &lval)) + break; + snd_ctl_elem_value_set_enumerated(self->value, i, lval); } break; case SND_CTL_ELEM_TYPE_BYTES: @@ -1183,7 +1162,9 @@ pyalsahcontrolvalue_settuple(struct pyalsahcontrolvalue *self, PyObject *args) if (v == Py_None) continue; Py_INCREF(v); - snd_ctl_elem_value_set_byte(self->value, i, PyInt_AsLong(v)); + if (get_long(v, &lval)) + break; + snd_ctl_elem_value_set_byte(self->value, i, lval); } break; case SND_CTL_ELEM_TYPE_IEC958: @@ -1199,25 +1180,25 @@ pyalsahcontrolvalue_settuple(struct pyalsahcontrolvalue *self, PyObject *args) len = 0; v = !list ? PyTuple_GET_ITEM(t, 0) : PyList_GetItem(t, 0); Py_INCREF(v); - if (PyString_AsStringAndSize(v, &str, &len)) + if (PyBytes_AsStringAndSize(v, &str, &len)) goto err1; - if (len > sizeof(iec958->status)) + if (len > (Py_ssize_t)sizeof(iec958->status)) len = sizeof(iec958->status); memcpy(iec958->status, str, len); len = 0; v = !list ? PyTuple_GET_ITEM(t, 1) : PyList_GetItem(t, 1); Py_INCREF(v); - if (PyString_AsStringAndSize(v, &str, &len)) + if (PyBytes_AsStringAndSize(v, &str, &len)) goto err1; - if (len > sizeof(iec958->subcode)) + if (len > (Py_ssize_t)sizeof(iec958->subcode)) len = sizeof(iec958->subcode); memcpy(iec958->subcode, str, len); len = 0; v = !list ? PyTuple_GET_ITEM(t, 2) : PyList_GetItem(t, 2); Py_INCREF(v); - if (PyString_AsStringAndSize(v, &str, &len)) + if (PyBytes_AsStringAndSize(v, &str, &len)) goto err1; - if (len > sizeof(iec958->dig_subframe)) + if (len > (Py_ssize_t)sizeof(iec958->dig_subframe)) len = sizeof(iec958->dig_subframe); memcpy(iec958->dig_subframe, str, len); free(iec958); @@ -1311,8 +1292,6 @@ pyalsahcontrolvalue_dealloc(struct pyalsahcontrolvalue *self) if (self->pyelem) { Py_XDECREF(self->pyelem); } - - self->ob_type->tp_free(self); } static PyGetSetDef pyalsahcontrolvalue_getseters[] = { @@ -1345,7 +1324,7 @@ static PyMethodDef pyalsahcontrolvalue_methods[] = { }; static PyTypeObject pyalsahcontrolvalue_type = { - PyObject_HEAD_INIT(0) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsahcontrol.Value", tp_basicsize: sizeof(struct pyalsahcontrolvalue), tp_dealloc: (destructor)pyalsahcontrolvalue_dealloc, @@ -1367,31 +1346,35 @@ static PyMethodDef pyalsahcontrolparse_methods[] = { {NULL} }; -PyMODINIT_FUNC -initalsahcontrol(void) +MOD_INIT(alsahcontrol) { PyObject *d, *d1, *l1, *o; int i; + pyalsahcontrol_type.tp_free = PyObject_GC_Del; + pyalsahcontrolelement_type.tp_free = PyObject_GC_Del; + pyalsahcontrolinfo_type.tp_free = PyObject_GC_Del; + pyalsahcontrolvalue_type.tp_free = PyObject_GC_Del; + if (PyType_Ready(&pyalsahcontrol_type) < 0) - return; + return MOD_ERROR_VAL; if (PyType_Ready(&pyalsahcontrolelement_type) < 0) - return; + return MOD_ERROR_VAL; if (PyType_Ready(&pyalsahcontrolinfo_type) < 0) - return; + return MOD_ERROR_VAL; if (PyType_Ready(&pyalsahcontrolvalue_type) < 0) - return; + return MOD_ERROR_VAL; - module = Py_InitModule3("alsahcontrol", pyalsahcontrolparse_methods, "libasound hcontrol wrapper"); + MOD_DEF(module, "alsahcontrol", "libasound hcontrol wrapper", pyalsahcontrolparse_methods); if (module == NULL) - return; + return MOD_ERROR_VAL; #if 0 buildin = PyImport_AddModule("__buildin__"); if (buildin == NULL) - return; + return MOD_ERROR_VAL; if (PyObject_SetAttrString(module, "__buildins__", buildin) < 0) - return; + return MOD_ERROR_VAL; #endif Py_INCREF(&pyalsahcontrol_type); @@ -1434,7 +1417,7 @@ initalsahcontrol(void) l1 = PyList_New(0); for (i = 0; i <= SND_CTL_ELEM_IFACE_LAST; i++) { - o = PyString_FromString(snd_ctl_elem_iface_name(i)); + o = PyUnicode_FromString(snd_ctl_elem_iface_name(i)); PyList_Append(l1, o); Py_DECREF(o); } @@ -1468,7 +1451,7 @@ initalsahcontrol(void) l1 = PyList_New(0); for (i = 0; i <= SND_CTL_ELEM_TYPE_LAST; i++) { - o = PyString_FromString(snd_ctl_elem_type_name(i)); + o = PyUnicode_FromString(snd_ctl_elem_type_name(i)); PyList_Append(l1, o); Py_DECREF(o); } @@ -1534,6 +1517,8 @@ initalsahcontrol(void) if (PyErr_Occurred()) Py_FatalError("Cannot initialize module alsahcontrol"); + + return MOD_SUCCESS_VAL(module); } /* @@ -1556,7 +1541,7 @@ static int element_callback(snd_hctl_elem_t *elem, unsigned int mask) tstate = PyThreadState_New(main_interpreter); origstate = PyThreadState_Swap(tstate); - o = PyObject_GetAttr(pyhelem->callback, PyString_InternFromString("callback")); + o = PyObject_GetAttr(pyhelem->callback, InternFromString("callback")); if (!o) { PyErr_Clear(); o = pyhelem->callback; @@ -1572,8 +1557,12 @@ static int element_callback(snd_hctl_elem_t *elem, unsigned int mask) Py_DECREF(t); if (r) { - if (PyInt_Check(r)) { + if (PyLong_Check(r)) { + res = PyLong_AsLong(r); +#if PY_MAJOR_VERSION < 3 + } else if (PyInt_Check(r)) { res = PyInt_AsLong(r); +#endif } else if (r == Py_None) { res = 0; } diff --git a/pyalsa/alsamixer.c b/pyalsa/alsamixer.c index 5fe1db9..1c06e3f 100644 --- a/pyalsa/alsamixer.c +++ b/pyalsa/alsamixer.c @@ -19,26 +19,11 @@ * */ -#include "Python.h" -#include "structmember.h" -#include "frameobject.h" -#ifndef PY_LONG_LONG - #define PY_LONG_LONG LONG_LONG -#endif +#include "common.h" #include "sys/poll.h" #include "stdlib.h" #include "alsa/asoundlib.h" -#ifndef Py_RETURN_NONE -#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None -#endif -#ifndef Py_RETURN_TRUE -#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True -#endif -#ifndef Py_RETURN_FALSE -#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False -#endif - static int element_callback(snd_mixer_elem_t *elem, unsigned int mask); static PyObject *module; @@ -59,17 +44,6 @@ struct pyalsamixer { snd_mixer_t *handle; }; -static inline PyObject *get_bool(int val) -{ - if (val) { - Py_INCREF(Py_True); - return Py_True; - } else { - Py_INCREF(Py_False); - return Py_False; - } -} - static PyObject * pyalsamixer_getcount(struct pyalsamixer *self, void *priv) { @@ -169,7 +143,7 @@ pyalsamixer_registerpoll(struct pyalsamixer *self, PyObject *args) if (count <= 0) Py_RETURN_NONE; - reg = PyObject_GetAttr(pollObj, PyString_InternFromString("register")); + reg = PyObject_GetAttr(pollObj, InternFromString("register")); for (i = 0; i < count; i++) { t = PyTuple_New(2); @@ -237,7 +211,7 @@ pyalsamixer_list(struct pyalsamixer *self, PyObject *args) v = NULL; if (elem) { v = PyTuple_New(2); - PyTuple_SET_ITEM(v, 0, PyString_FromString(snd_mixer_selem_get_name(elem))); + PyTuple_SET_ITEM(v, 0, PyUnicode_FromString(snd_mixer_selem_get_name(elem))); PyTuple_SET_ITEM(v, 1, PyInt_FromLong(snd_mixer_selem_get_index(elem))); } if (v == NULL || elem == NULL) { @@ -281,8 +255,6 @@ pyalsamixer_dealloc(struct pyalsamixer *self) { if (self->handle != NULL) snd_mixer_close(self->handle); - - self->ob_type->tp_free(self); } static PyGetSetDef pyalsamixer_getseters[] = { @@ -305,7 +277,7 @@ static PyMethodDef pyalsamixer_methods[] = { }; static PyTypeObject pyalsamixer_type = { - PyObject_HEAD_INIT(0) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsamixer.Mixer", tp_basicsize: sizeof(struct pyalsamixer), tp_dealloc: (destructor)pyalsamixer_dealloc, @@ -337,7 +309,7 @@ struct pyalsamixerelement { static PyObject * pyalsamixerelement_getname(struct pyalsamixerelement *pyelem, void *priv) { - return PyString_FromString(snd_mixer_selem_get_name(pyelem->elem)); + return PyUnicode_FromString(snd_mixer_selem_get_name(pyelem->elem)); } static PyObject * @@ -727,11 +699,8 @@ pyalsamixerelement_setvolumetuple(struct pyalsamixerelement *pyelem, PyObject *a o = PyTuple_GetItem(t, i); if (o == Py_None) continue; - if (!PyInt_Check(o)) { - PyErr_Format(PyExc_RuntimeError, "Only None or Int types are expected!"); + if (get_long(o, &val)) break; - } - val = PyInt_AsLong(o); if (dir == 0) res = snd_mixer_selem_set_playback_volume(pyelem->elem, i, val); else @@ -744,11 +713,8 @@ pyalsamixerelement_setvolumetuple(struct pyalsamixerelement *pyelem, PyObject *a o = PyList_GetItem(t, i); if (o == Py_None) continue; - if (!PyInt_Check(o)) { - PyErr_Format(PyExc_RuntimeError, "Only None or Int types are expected!"); + if (get_long(o, &val)) break; - } - val = PyInt_AsLong(o); if (dir == 0) res = snd_mixer_selem_set_playback_volume(pyelem->elem, i, val); else @@ -1162,8 +1128,6 @@ pyalsamixerelement_dealloc(struct pyalsamixerelement *self) if (self->pyhandle) { Py_XDECREF(self->pyhandle); } - - self->ob_type->tp_free(self); } static PyGetSetDef pyalsamixerelement_getseters[] = { @@ -1219,7 +1183,7 @@ static PyMethodDef pyalsamixerelement_methods[] = { }; static PyTypeObject pyalsamixerelement_type = { - PyObject_HEAD_INIT(0) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsamixer.Element", tp_basicsize: sizeof(struct pyalsamixerelement), tp_dealloc: (destructor)pyalsamixerelement_dealloc, @@ -1241,27 +1205,29 @@ static PyMethodDef pyalsamixerparse_methods[] = { {NULL} }; -PyMODINIT_FUNC -initalsamixer(void) +MOD_INIT(alsamixer) { PyObject *d, *d1, *l1, *o; int i; + pyalsamixer_type.tp_free = PyObject_GC_Del; + pyalsamixerelement_type.tp_free = PyObject_GC_Del; + if (PyType_Ready(&pyalsamixer_type) < 0) - return; + return MOD_ERROR_VAL; if (PyType_Ready(&pyalsamixerelement_type) < 0) - return; + return MOD_ERROR_VAL; - module = Py_InitModule3("alsamixer", pyalsamixerparse_methods, "libasound mixer wrapper"); + MOD_DEF(module, "alsamixer", "libasound mixer wrapper", pyalsamixerparse_methods); if (module == NULL) - return; + return MOD_ERROR_VAL; #if 0 buildin = PyImport_AddModule("__buildin__"); if (buildin == NULL) - return; + return MOD_ERROR_VAL; if (PyObject_SetAttrString(module, "__buildins__", buildin) < 0) - return; + return MOD_ERROR_VAL; #endif Py_INCREF(&pyalsamixer_type); @@ -1302,7 +1268,7 @@ initalsamixer(void) l1 = PyList_New(0); for (i = 0; i <= SND_MIXER_SCHN_LAST; i++) { - o = PyString_FromString(snd_mixer_selem_channel_name(i)); + o = PyUnicode_FromString(snd_mixer_selem_channel_name(i)); PyList_Append(l1, o); Py_DECREF(o); } @@ -1352,6 +1318,8 @@ initalsamixer(void) if (PyErr_Occurred()) Py_FatalError("Cannot initialize module alsamixer"); + + return MOD_SUCCESS_VAL(module); } /* @@ -1374,7 +1342,7 @@ static int element_callback(snd_mixer_elem_t *elem, unsigned int mask) tstate = PyThreadState_New(main_interpreter); origstate = PyThreadState_Swap(tstate); - o = PyObject_GetAttr(pyelem->callback, PyString_InternFromString("callback")); + o = PyObject_GetAttr(pyelem->callback, InternFromString("callback")); if (!o) { PyErr_Clear(); o = pyelem->callback; @@ -1390,9 +1358,15 @@ static int element_callback(snd_mixer_elem_t *elem, unsigned int mask) Py_DECREF(t); if (r) { - if (PyInt_Check(r)) { + if (PyLong_Check(r)) { + res = PyLong_AsLong(r); + } +#if PY_MAJOR_VERSION < 3 + else if (PyInt_Check(r)) { res = PyInt_AsLong(r); - } else if (r == Py_None) { + } +#endif + else if (r == Py_None) { res = 0; } Py_DECREF(r); diff --git a/pyalsa/alsaseq.c b/pyalsa/alsaseq.c index acefbdc..acc6984 100644 --- a/pyalsa/alsaseq.c +++ b/pyalsa/alsaseq.c @@ -18,66 +18,20 @@ * */ -#include "Python.h" +#include "common.h" #include #include +/* + * + */ - - -////////////////////////////////////////////////////////////////////////////// -// some helper #define go here... -////////////////////////////////////////////////////////////////////////////// - -/* temporal debug (will be deleted in last patch, promise!) */ -#if 1 +#if 0 #define ddebug(x, args...) fprintf(stderr, x "\n",##args); #else #define ddebug(x, args...) #endif -/* checks if passed argument is an integer and can't be deleted. - returns -1 if fails and raises TypeError or AttributeError. */ -#define SETCHECKPYINT(attr, val) \ - if (val == NULL) { \ - PyErr_SetString(PyExc_AttributeError, \ - "attribute " attr " can't be deleted!"); \ - return -1; \ - } \ - if (!PyInt_Check(val)) { \ - PyErr_SetString(PyExc_TypeError, \ - "integer value expected for " attr); \ - return -1; \ - } - -/* checks if passed argument is a string and can't be deleted. - returns -1 if fails and raises TypeError or AttributeError. */ -#define SETCHECKPYSTR(attr, val) \ - if (val == NULL) { \ - PyErr_SetString(PyExc_AttributeError, \ - "attribute " attr " can't be deleted!"); \ - return -1; \ - } \ - if (!PyString_Check(val)) { \ - PyErr_SetString(PyExc_TypeError, \ - "string value expected for " attr); \ - return -1; \ - } - -/* checks if passed argument is a list and can't be deleted. - returns -1 if fails and raises TypeError or AttributeError. */ -#define SETCHECKPYLIST(attr, val) \ - if (val == NULL) { \ - PyErr_SetString(PyExc_AttributeError, \ - "attribute " attr " can't be deleted!"); \ - return -1; \ - } \ - if (!PyList_Check(val)) { \ - PyErr_SetString(PyExc_TypeError, \ - "list value expected for " attr); \ - return -1; \ - } - /* frees only if pointer is not NULL. */ #define FREECHECKED(name, pointer) \ if (pointer != NULL) { \ @@ -108,10 +62,10 @@ #define TCONSTDICTADD(module, subtype, name) \ _dictPYALSASEQ_CONST_##subtype = PyDict_New(); \ if (TDICT(subtype) == NULL) { \ - return; \ + return MOD_ERROR_VAL; \ } \ if (PyModule_AddObject(module, name, TDICT(subtype)) < 0) { \ - return; \ + return MOD_ERROR_VAL; \ } /* creates a typed constant and add it to the module and dictionary */ @@ -119,10 +73,10 @@ PyObject *tmp = \ Constant_create(name, value, TTYPE(subtype)); \ if (tmp == NULL) { \ - return; \ + return MOD_ERROR_VAL; \ } \ if (PyModule_AddObject(module, name, tmp) < 0) { \ - return; \ + return MOD_ERROR_VAL; \ } \ PyObject *key = PyInt_FromLong(value); \ PyDict_SetItem(TDICT(subtype), key, tmp); \ @@ -161,13 +115,12 @@ static PyObject * \ Constant_##name (PyObject *v, PyObject *w) { \ int type = 0; \ - long val = 0; \ - /* both have to be a int */ \ - if (!PyInt_Check(v) || !PyInt_Check(w)) { \ + long val1, val2, val; \ + if (get_long1(v, &val1) || get_long1(w, &val2)) { \ Py_INCREF(Py_NotImplemented); \ return Py_NotImplemented; \ } \ - val = PyInt_AS_LONG(v) oper PyInt_AS_LONG(w); \ + val = val1 oper val2; \ /* always asume left will be the type */ \ if (PyObject_TypeCheck(v, &ConstantType)) { \ type = ((ConstantObject *) v)->type; \ @@ -183,12 +136,12 @@ static PyObject * \ Constant_##name (PyObject *v) { \ int type = 0; \ - long val = 0; \ - if (!PyInt_Check(v)) { \ + long val1, val; \ + if (get_long1(v, &val1)) { \ Py_INCREF(Py_NotImplemented); \ return Py_NotImplemented; \ } \ - val = oper PyInt_AS_LONG(v); \ + val = oper val1; \ if (PyObject_TypeCheck(v, &ConstantType)) { \ type = ((ConstantObject *) v)->type; \ } \ @@ -287,7 +240,7 @@ #define SETDICT_EXT { \ snd_seq_ev_ext_t *data = &(event->data.ext); \ PyObject *list = PyList_New(data->len); \ - int i = 0; \ + unsigned i = 0; \ unsigned char *t = (unsigned char *) data->ptr; \ for (i = 0; i < data->len; i++) { \ PyList_SetItem(list, i, PyInt_FromLong(t[i])); \ @@ -297,15 +250,14 @@ } /* gets integer from python param */ -#define GETDICTINT(name, param) { \ - PyObject *value = PyDict_GetItemString(dict, name); \ - if (value != NULL) { \ - if (!PyInt_Check(value)) { \ - PyErr_SetString(PyExc_TypeError, name " must be a integer"); \ - return NULL; \ - } \ - param = PyInt_AsLong(value); \ - } \ +#define GETDICTINT(name, param) { \ + PyObject *value = PyDict_GetItemString(dict, name); \ + if (value != NULL) { \ + long val; \ + if (get_long(value, &val)) \ + return NULL; \ + param = val; \ + } \ } /* gets float from python param */ @@ -343,9 +295,10 @@ self->event->data.ext.len = len; \ if (len > 0) { \ int i; \ + long val; \ for (i = 0; i < len; i++) { \ PyObject *item = PyList_GetItem(list, i); \ - if (!PyInt_Check(item)) { \ + if (get_long1(item, &val)) { \ PyErr_SetString(PyExc_TypeError, \ name " must be a list of integers"); \ self->event->data.ext.len = 0; \ @@ -359,10 +312,8 @@ self->event->data.ext.len = 0; \ return NULL; \ } \ - for (i = 0; i < len; i++) { \ - PyObject *item = PyList_GetItem(list, i); \ - self->buff[i] = PyInt_AsLong(item); \ - } \ + for (i = 0; i < len; i++) \ + self->buff[i] = val; \ self->event->data.ext.ptr = self->buff; \ } \ } \ @@ -474,7 +425,7 @@ Constant_create(const char *name, long value, int type) { /** alsaseq.Constant tp_repr */ static PyObject * Constant_repr(ConstantObject *self) { - return PyString_FromFormat("%s(0x%x)", + return PyUnicode_FromFormat("%s(0x%x)", self->name, (unsigned int)self->value); } @@ -482,7 +433,7 @@ Constant_repr(ConstantObject *self) { /** alsaseq.Constant tp_str */ static PyObject * Constant_str(ConstantObject *self) { - return PyString_FromFormat("%s", + return PyUnicode_FromFormat("%s", self->name); } @@ -506,14 +457,22 @@ static PyNumberMethods Constant_as_number = { /** alsaseq.Constant type */ static PyTypeObject ConstantType = { - PyObject_HEAD_INIT(NULL) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsaseq.Constant", +#if PY_MAJOR_VERSION < 3 tp_base: &PyInt_Type, +#else + tp_base: &PyLong_Type, +#endif tp_basicsize: sizeof(ConstantObject), tp_flags: +#if PY_MAJOR_VERSION < 3 Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_CLASS | Py_TPFLAGS_CHECKTYPES, +#else + 0, +#endif tp_doc: Constant__doc__, tp_as_number: &Constant_as_number, tp_free: PyObject_Del, @@ -752,8 +711,6 @@ static void SeqEvent_dealloc(SeqEventObject *self) { FREECHECKED("event", self->event); FREECHECKED("buff", self->buff); - - self->ob_type->tp_free(self); } /** alsaseq.SeqEvent type attribute: __doc__ */ @@ -775,9 +732,12 @@ SeqEvent_get_type(SeqEventObject *self) { static int SeqEvent_set_type(SeqEventObject *self, PyObject *val) { - SETCHECKPYINT("type", val); + long v; + + if (get_long(val, &v)) + return -1; - return _SeqEvent_set_type(self, PyInt_AsLong(val)); + return _SeqEvent_set_type(self, v); } /** alsaseq.SeqEvent tag attribute: __doc__ */ @@ -799,9 +759,9 @@ SeqEvent_set_tag(SeqEventObject *self, PyObject *val) { long tag; - SETCHECKPYINT("tag", val); + if (get_long(val, &tag)) + return -1; - tag = PyInt_AsLong(val); if (tag < 0 || tag > 255) { PyErr_Format(PyExc_ValueError, "invalid value '%ld'; allowed range: 0 - 255", @@ -837,9 +797,12 @@ SeqEvent_get_timestamp(SeqEventObject *self) { static int SeqEvent_set_timestamp(SeqEventObject *self, PyObject *val) { - SETCHECKPYINT("timestamp", val); + long v; + + if (get_long(val, &v)) + return -1; - return _SeqEvent_set_timestamp(self, PyInt_AsLong(val)); + return _SeqEvent_set_timestamp(self, v); } /** alsaseq.SeqEvent timemode attribute: __doc__ */ @@ -867,9 +830,12 @@ SeqEvent_get_timemode(SeqEventObject *self) { static int SeqEvent_set_timemode(SeqEventObject *self, PyObject *val) { - SETCHECKPYINT("timemode", val); + long v; - return _SeqEvent_set_timemode(self, PyInt_AsLong(val)); + if (get_long(val, &v)) + return -1; + + return _SeqEvent_set_timemode(self, v); } /** alsaseq.SeqEvent queue attribute: __doc__ */ @@ -889,9 +855,12 @@ SeqEvent_get_queue(SeqEventObject *self) { static int SeqEvent_set_queue(SeqEventObject *self, PyObject *val) { - SETCHECKPYINT("queue", val); + long v; + + if (get_long(val, &v)) + return -1; - self->event->queue = PyInt_AsLong(val); + self->event->queue = v; return 0; } @@ -926,8 +895,9 @@ SeqEvent_get_time(SeqEventObject *self) { static int SeqEvent_set_time(SeqEventObject *self, PyObject *val) { - int is_int = PyInt_Check(val); - int is_float = PyFloat_Check(val); + long lval = 0; + const int is_float = PyFloat_Check(val); + const int is_int = is_float ? 0 : get_long1(val, &lval); if (!(is_int || is_float)) { PyErr_Format(PyExc_TypeError, @@ -937,7 +907,7 @@ SeqEvent_set_time(SeqEventObject *self, if (snd_seq_ev_is_real(self->event)) { if (is_int) { - double time = PyInt_AsLong(val); + double time = lval; self->event->time.time.tv_sec = time; self->event->time.time.tv_nsec = 0; } else { @@ -948,7 +918,7 @@ SeqEvent_set_time(SeqEventObject *self, } } else if (snd_seq_ev_is_tick(self->event)) { if (is_int) { - self->event->time.tick = PyInt_AsLong(val); + self->event->time.tick = lval; } else { self->event->time.tick = PyFloat_AsDouble(val); } @@ -989,21 +959,21 @@ SeqEvent_get_source(SeqEventObject *self) { static int SeqEvent_set_source(SeqEventObject *self, PyObject *val) { - PyObject *client; - PyObject *port; + long client; + long port; if (!PyTuple_Check(val) || PyTuple_Size(val) != 2) { PyErr_SetString(PyExc_TypeError, "expected tuple (client,port)"); return -1; } - client = PyTuple_GetItem(val, 0); - port = PyTuple_GetItem(val, 1); - SETCHECKPYINT("source client", client); - SETCHECKPYINT("source port", port); + if (get_long(PyTuple_GetItem(val, 0), &client)) + return -1; + if (get_long(PyTuple_GetItem(val, 1), &port)) + return -1; - self->event->source.client = PyInt_AsLong(client); - self->event->source.port = PyInt_AsLong(port); + self->event->source.client = client; + self->event->source.port = port; return 0; } @@ -1038,21 +1008,21 @@ SeqEvent_get_dest(SeqEventObject *self) { static int SeqEvent_set_dest(SeqEventObject *self, PyObject *val) { - PyObject *client; - PyObject *port; + long client; + long port; if (!PyTuple_Check(val) || PyTuple_Size(val) != 2) { PyErr_SetString(PyExc_TypeError, "expected tuple (client,port)"); return -1; } - client = PyTuple_GetItem(val, 0); - port = PyTuple_GetItem(val, 1); - SETCHECKPYINT("dest client", client); - SETCHECKPYINT("dest port", port); + if (get_long(PyTuple_GetItem(val, 0), &client)) + return -1;; + if (get_long(PyTuple_GetItem(val, 1), &port)) + return -1; - self->event->dest.client = PyInt_AsLong(client); - self->event->dest.port = PyInt_AsLong(port); + self->event->dest.client = client; + self->event->dest.port = port; return 0; } @@ -1771,7 +1741,7 @@ SeqEvent_repr(SeqEventObject *self) { dtime = self->event->time.tick; } - return PyString_FromFormat("", typestr, @@ -1992,7 +1962,7 @@ static PyMethodDef SeqEvent_methods[] = { /** alsaseq.SeEvent type */ static PyTypeObject SeqEventType = { - PyObject_HEAD_INIT(NULL) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsaseq.SeqEvent", tp_basicsize: sizeof(SeqEventObject), tp_dealloc: (destructor) SeqEvent_dealloc, @@ -2129,8 +2099,6 @@ Sequencer_dealloc(SequencerObject *self) { snd_seq_close(self->handle); self->handle = NULL; } - - self->ob_type->tp_free(self); } /** alsaseq.Sequencer name attribute: __doc__ */ @@ -2144,7 +2112,7 @@ PyDoc_STRVAR(Sequencer_name__doc__, /** alsaseq.Sequencer name attribute: tp_getset getter() */ static PyObject * Sequencer_get_name(SequencerObject *self) { - return PyString_FromString(snd_seq_name(self->handle)); + return PyUnicode_FromString(snd_seq_name(self->handle)); } /** alsaseq.Sequencer clientname attribute: __doc__ */ @@ -2162,16 +2130,20 @@ Sequencer_get_clientname(SequencerObject *self) { snd_seq_client_info_alloca(&cinfo); snd_seq_get_client_info(self->handle, cinfo); - return PyString_FromString(snd_seq_client_info_get_name(cinfo)); + return PyUnicode_FromString(snd_seq_client_info_get_name(cinfo)); } /** alsaseq.Sequencer clientname attribute: tp_getset setter() */ static int Sequencer_set_clientname(SequencerObject *self, PyObject *val) { - SETCHECKPYSTR("clientname", val); + char *s; + + if (get_utf8_string(val, &s)) + return -1; - snd_seq_set_client_name(self->handle, PyString_AsString(val)); + snd_seq_set_client_name(self->handle, s); + free(s); return 0; } @@ -2209,11 +2181,11 @@ static PyObject *Sequencer_get_mode(SequencerObject *self) { static int Sequencer_set_mode(SequencerObject *self, PyObject *val) { - int ret, mode; - - SETCHECKPYINT("mode", val); + int ret; + long mode; - mode = (int) PyInt_AsLong(val); + if (get_long(val, &mode)) + return -1; if (mode != 0 && mode != SND_SEQ_NONBLOCK) { PyErr_SetString(PyExc_ValueError, "Invalid value for mode."); @@ -2286,7 +2258,7 @@ Sequencer_repr(SequencerObject *self) { snd_seq_client_info_alloca(&cinfo); snd_seq_get_client_info(self->handle, cinfo); - return PyString_FromFormat("", snd_seq_name(self->handle), snd_seq_client_info_get_client(cinfo), @@ -2440,7 +2412,7 @@ Sequencer_connection_list(SequencerObject *self, PyObject *tuple = PyTuple_New(3); PyObject *portlist = PyList_New(0); - name = PyString_FromFormat("%s", snd_seq_client_info_get_name(cinfo)); + name = PyUnicode_FromFormat("%s", snd_seq_client_info_get_name(cinfo)); client = PyInt_FromLong(snd_seq_client_info_get_client(cinfo)); PyTuple_SetItem(tuple, 0, name); PyTuple_SetItem(tuple, 1, client); @@ -2449,7 +2421,7 @@ Sequencer_connection_list(SequencerObject *self, /* create tuple for port info */ PyObject *porttuple = PyTuple_New(3); - name = PyString_FromFormat("%s", snd_seq_port_info_get_name(pinfo)); + name = PyUnicode_FromFormat("%s", snd_seq_port_info_get_name(pinfo)); port = PyInt_FromLong(snd_seq_port_info_get_port(pinfo)); PyTuple_SetItem(porttuple, 0, name); @@ -3321,7 +3293,7 @@ static PyMethodDef Sequencer_methods[] = { /** alsaseq.Sequencer type */ static PyTypeObject SequencerType = { - PyObject_HEAD_INIT(NULL) + PyVarObject_HEAD_INIT(NULL, 0) tp_name: "alsaseq.Sequencer", tp_basicsize: sizeof(SequencerObject), tp_dealloc: (destructor) Sequencer_dealloc, @@ -3354,32 +3326,31 @@ static PyMethodDef alsaseq_methods[] = { { NULL }, }; -PyMODINIT_FUNC -initalsaseq(void) { +MOD_INIT(alsaseq) +{ PyObject *module; - if (PyType_Ready(&ConstantType) < 0) { - return; - } + ConstantType.tp_free = PyObject_GC_Del; + SeqEventType.tp_free = PyObject_GC_Del; + SequencerType.tp_free = PyObject_GC_Del; - if (PyType_Ready(&SeqEventType) < 0) { - return; - } + if (PyType_Ready(&ConstantType) < 0) + return MOD_ERROR_VAL; - if (PyType_Ready(&SequencerType) < 0) { - return; - } + if (PyType_Ready(&SeqEventType) < 0) + return MOD_ERROR_VAL; - module = Py_InitModule3("alsaseq", alsaseq_methods, alsaseq__doc__); + if (PyType_Ready(&SequencerType) < 0) + return MOD_ERROR_VAL; - if (module == NULL) { - return; - } + MOD_DEF(module, "alsaseq", alsaseq__doc__, alsaseq_methods); + + if (module == NULL) + return MOD_ERROR_VAL; SequencerError = PyErr_NewException("alsaseq.SequencerError", NULL, NULL); - if (SequencerError == NULL) { - return; - } + if (SequencerError == NULL) + return MOD_ERROR_VAL; Py_INCREF(SequencerError); PyModule_AddObject(module, "SequencerError", SequencerError); @@ -3399,360 +3370,141 @@ initalsaseq(void) { SND_LIB_VERSION_STR); /* add Constant dictionaries to module */ - TCONSTDICTADD(module, STREAMS, - "_dstreams"); - TCONSTDICTADD(module, MODE, - "_dmode"); - TCONSTDICTADD(module, QUEUE, - "_dqueue"); - TCONSTDICTADD(module, CLIENT_TYPE, - "_dclienttype"); - TCONSTDICTADD(module, PORT_CAP, - "_dportcap"); - TCONSTDICTADD(module, PORT_TYPE, - "_dporttype"); - TCONSTDICTADD(module, EVENT_TYPE, - "_deventtype"); - TCONSTDICTADD(module, EVENT_TIMESTAMP, - "_deventtimestamp"); - TCONSTDICTADD(module, EVENT_TIMEMODE, - "_deventtimemode"); - TCONSTDICTADD(module, ADDR_CLIENT, - "_dclient"); - TCONSTDICTADD(module, ADDR_PORT, - "_dport"); + TCONSTDICTADD(module, STREAMS, "_dstreams"); + TCONSTDICTADD(module, MODE, "_dmode"); + TCONSTDICTADD(module, QUEUE, "_dqueue"); + TCONSTDICTADD(module, CLIENT_TYPE, "_dclienttype"); + TCONSTDICTADD(module, PORT_CAP, "_dportcap"); + TCONSTDICTADD(module, PORT_TYPE, "_dporttype"); + TCONSTDICTADD(module, EVENT_TYPE, "_deventtype"); + TCONSTDICTADD(module, EVENT_TIMESTAMP, "_deventtimestamp"); + TCONSTDICTADD(module, EVENT_TIMEMODE, "_deventtimemode"); + TCONSTDICTADD(module, ADDR_CLIENT, "_dclient"); + TCONSTDICTADD(module, ADDR_PORT, "_dport"); /* Sequencer streams */ - TCONSTADD(module, STREAMS, - "SEQ_OPEN_OUTPUT", - SND_SEQ_OPEN_OUTPUT); - TCONSTADD(module, STREAMS, - "SEQ_OPEN_INPUT", - SND_SEQ_OPEN_INPUT); - TCONSTADD(module, STREAMS, - "SEQ_OPEN_DUPLEX", - SND_SEQ_OPEN_DUPLEX); + TCONSTADD(module, STREAMS, "SEQ_OPEN_OUTPUT", SND_SEQ_OPEN_OUTPUT); + TCONSTADD(module, STREAMS, "SEQ_OPEN_INPUT", SND_SEQ_OPEN_INPUT); + TCONSTADD(module, STREAMS, "SEQ_OPEN_DUPLEX", SND_SEQ_OPEN_DUPLEX); /* Sequencer blocking mode */ - TCONSTADD(module, MODE, - "SEQ_BLOCK", - 0); - TCONSTADD(module, MODE, - "SEQ_NONBLOCK", - SND_SEQ_NONBLOCK); + TCONSTADD(module, MODE, "SEQ_BLOCK", 0); + TCONSTADD(module, MODE, "SEQ_NONBLOCK", SND_SEQ_NONBLOCK); /* Known queue id */ - TCONSTADD(module, QUEUE, - "SEQ_QUEUE_DIRECT", - SND_SEQ_QUEUE_DIRECT); + TCONSTADD(module, QUEUE, "SEQ_QUEUE_DIRECT", SND_SEQ_QUEUE_DIRECT); /* client types */ - TCONSTADD(module, CLIENT_TYPE, - "SEQ_USER_CLIENT", - SND_SEQ_USER_CLIENT); - TCONSTADD(module, CLIENT_TYPE, - "SEQ_KERNEL_CLIENT", - SND_SEQ_KERNEL_CLIENT); + TCONSTADD(module, CLIENT_TYPE, "SEQ_USER_CLIENT", SND_SEQ_USER_CLIENT); + TCONSTADD(module, CLIENT_TYPE, "SEQ_KERNEL_CLIENT", SND_SEQ_KERNEL_CLIENT); /* Sequencer port cap */ - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_NONE", - 0); - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_WRITE", - SND_SEQ_PORT_CAP_WRITE); - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_SYNC_WRITE", - SND_SEQ_PORT_CAP_SYNC_WRITE); - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_SYNC_READ", - SND_SEQ_PORT_CAP_SYNC_READ); - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_SUBS_WRITE", - SND_SEQ_PORT_CAP_SUBS_WRITE); - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_SUBS_READ", - SND_SEQ_PORT_CAP_SUBS_READ); - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_READ", - SND_SEQ_PORT_CAP_READ); - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_NO_EXPORT", - SND_SEQ_PORT_CAP_NO_EXPORT); - TCONSTADD(module, PORT_CAP, - "SEQ_PORT_CAP_DUPLEX", - SND_SEQ_PORT_CAP_DUPLEX); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_NONE", 0); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_WRITE", SND_SEQ_PORT_CAP_WRITE); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_SYNC_WRITE", SND_SEQ_PORT_CAP_SYNC_WRITE); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_SYNC_READ", SND_SEQ_PORT_CAP_SYNC_READ); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_SUBS_WRITE", SND_SEQ_PORT_CAP_SUBS_WRITE); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_SUBS_READ", SND_SEQ_PORT_CAP_SUBS_READ); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_READ", SND_SEQ_PORT_CAP_READ); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_NO_EXPORT", SND_SEQ_PORT_CAP_NO_EXPORT); + TCONSTADD(module, PORT_CAP, "SEQ_PORT_CAP_DUPLEX", SND_SEQ_PORT_CAP_DUPLEX); /* Sequencer port type */ - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_SYNTHESIZER", - SND_SEQ_PORT_TYPE_SYNTHESIZER); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_SYNTH", - SND_SEQ_PORT_TYPE_SYNTH); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_SPECIFIC", - SND_SEQ_PORT_TYPE_SPECIFIC); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_SOFTWARE", - SND_SEQ_PORT_TYPE_SOFTWARE); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_SAMPLE", - SND_SEQ_PORT_TYPE_SAMPLE); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_PORT", - SND_SEQ_PORT_TYPE_PORT); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_MIDI_XG", - SND_SEQ_PORT_TYPE_MIDI_XG); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_MIDI_MT32", - SND_SEQ_PORT_TYPE_MIDI_MT32); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_MIDI_GS", - SND_SEQ_PORT_TYPE_MIDI_GS); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_MIDI_GM2", - SND_SEQ_PORT_TYPE_MIDI_GM2); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_MIDI_GM", - SND_SEQ_PORT_TYPE_MIDI_GM); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_MIDI_GENERIC", - SND_SEQ_PORT_TYPE_MIDI_GENERIC); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_HARDWARE", - SND_SEQ_PORT_TYPE_HARDWARE); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_DIRECT_SAMPLE", - SND_SEQ_PORT_TYPE_DIRECT_SAMPLE); - TCONSTADD(module, PORT_TYPE, - "SEQ_PORT_TYPE_APPLICATION", - SND_SEQ_PORT_TYPE_APPLICATION); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_SYNTHESIZER", SND_SEQ_PORT_TYPE_SYNTHESIZER); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_SYNTH", SND_SEQ_PORT_TYPE_SYNTH); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_SPECIFIC", SND_SEQ_PORT_TYPE_SPECIFIC); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_SOFTWARE", SND_SEQ_PORT_TYPE_SOFTWARE); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_SAMPLE", SND_SEQ_PORT_TYPE_SAMPLE); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_PORT", SND_SEQ_PORT_TYPE_PORT); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_MIDI_XG", SND_SEQ_PORT_TYPE_MIDI_XG); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_MIDI_MT32", SND_SEQ_PORT_TYPE_MIDI_MT32); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_MIDI_GS", SND_SEQ_PORT_TYPE_MIDI_GS); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_MIDI_GM2", SND_SEQ_PORT_TYPE_MIDI_GM2); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_MIDI_GM", SND_SEQ_PORT_TYPE_MIDI_GM); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_MIDI_GENERIC", SND_SEQ_PORT_TYPE_MIDI_GENERIC); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_HARDWARE", SND_SEQ_PORT_TYPE_HARDWARE); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_DIRECT_SAMPLE", SND_SEQ_PORT_TYPE_DIRECT_SAMPLE); + TCONSTADD(module, PORT_TYPE, "SEQ_PORT_TYPE_APPLICATION", SND_SEQ_PORT_TYPE_APPLICATION); /* SeqEvent event type */ - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_SYSTEM", - SND_SEQ_EVENT_SYSTEM); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_RESULT", - SND_SEQ_EVENT_RESULT); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_NOTE", - SND_SEQ_EVENT_NOTE); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_NOTEON", - SND_SEQ_EVENT_NOTEON); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_NOTEOFF", - SND_SEQ_EVENT_NOTEOFF); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_KEYPRESS", - SND_SEQ_EVENT_KEYPRESS); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_CONTROLLER", - SND_SEQ_EVENT_CONTROLLER); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_PGMCHANGE", - SND_SEQ_EVENT_PGMCHANGE); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_CHANPRESS", - SND_SEQ_EVENT_CHANPRESS); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_PITCHBEND", - SND_SEQ_EVENT_PITCHBEND); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_CONTROL14", - SND_SEQ_EVENT_CONTROL14); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_NONREGPARAM", - SND_SEQ_EVENT_NONREGPARAM); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_REGPARAM", - SND_SEQ_EVENT_REGPARAM); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_SONGPOS", - SND_SEQ_EVENT_SONGPOS); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_SONGSEL", - SND_SEQ_EVENT_SONGSEL); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_QFRAME", - SND_SEQ_EVENT_QFRAME); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_TIMESIGN", - SND_SEQ_EVENT_TIMESIGN); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_KEYSIGN", - SND_SEQ_EVENT_KEYSIGN); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_START", - SND_SEQ_EVENT_START); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_CONTINUE", - SND_SEQ_EVENT_CONTINUE); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_STOP", - SND_SEQ_EVENT_STOP); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_SETPOS_TICK", - SND_SEQ_EVENT_SETPOS_TICK); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_SETPOS_TIME", - SND_SEQ_EVENT_SETPOS_TIME); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_TEMPO", - SND_SEQ_EVENT_TEMPO); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_CLOCK", - SND_SEQ_EVENT_CLOCK); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_TICK", - SND_SEQ_EVENT_TICK); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_QUEUE_SKEW", - SND_SEQ_EVENT_QUEUE_SKEW); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_SYNC_POS", - SND_SEQ_EVENT_SYNC_POS); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_TUNE_REQUEST", - SND_SEQ_EVENT_TUNE_REQUEST); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_RESET", - SND_SEQ_EVENT_RESET); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_SENSING", - SND_SEQ_EVENT_SENSING); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_ECHO", - SND_SEQ_EVENT_ECHO); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_OSS", - SND_SEQ_EVENT_OSS); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_CLIENT_START", - SND_SEQ_EVENT_CLIENT_START); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_CLIENT_EXIT", - SND_SEQ_EVENT_CLIENT_EXIT); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_CLIENT_CHANGE", - SND_SEQ_EVENT_CLIENT_CHANGE); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_PORT_START", - SND_SEQ_EVENT_PORT_START); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_PORT_EXIT", - SND_SEQ_EVENT_PORT_EXIT); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_PORT_CHANGE", - SND_SEQ_EVENT_PORT_CHANGE); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_PORT_SUBSCRIBED", - SND_SEQ_EVENT_PORT_SUBSCRIBED); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_PORT_UNSUBSCRIBED", - SND_SEQ_EVENT_PORT_UNSUBSCRIBED); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR0", - SND_SEQ_EVENT_USR0); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR1", - SND_SEQ_EVENT_USR1); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR2", - SND_SEQ_EVENT_USR2); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR3", - SND_SEQ_EVENT_USR3); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR4", - SND_SEQ_EVENT_USR4); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR5", - SND_SEQ_EVENT_USR5); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR6", - SND_SEQ_EVENT_USR6); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR7", - SND_SEQ_EVENT_USR7); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR8", - SND_SEQ_EVENT_USR8); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR9", - SND_SEQ_EVENT_USR9); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_SYSEX", - SND_SEQ_EVENT_SYSEX); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_BOUNCE", - SND_SEQ_EVENT_BOUNCE); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR_VAR0", - SND_SEQ_EVENT_USR_VAR0); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR_VAR1", - SND_SEQ_EVENT_USR_VAR1); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR_VAR2", - SND_SEQ_EVENT_USR_VAR2); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR_VAR3", - SND_SEQ_EVENT_USR_VAR3); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_USR_VAR4", - SND_SEQ_EVENT_USR_VAR4); - TCONSTADD(module, EVENT_TYPE, - "SEQ_EVENT_NONE", - SND_SEQ_EVENT_NONE); - + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_SYSTEM", SND_SEQ_EVENT_SYSTEM); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_RESULT", SND_SEQ_EVENT_RESULT); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_NOTE", SND_SEQ_EVENT_NOTE); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_NOTEON", SND_SEQ_EVENT_NOTEON); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_NOTEOFF", SND_SEQ_EVENT_NOTEOFF); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_KEYPRESS", SND_SEQ_EVENT_KEYPRESS); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_CONTROLLER", SND_SEQ_EVENT_CONTROLLER); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_PGMCHANGE", SND_SEQ_EVENT_PGMCHANGE); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_CHANPRESS", SND_SEQ_EVENT_CHANPRESS); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_PITCHBEND", SND_SEQ_EVENT_PITCHBEND); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_CONTROL14", SND_SEQ_EVENT_CONTROL14); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_NONREGPARAM", SND_SEQ_EVENT_NONREGPARAM); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_REGPARAM", SND_SEQ_EVENT_REGPARAM); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_SONGPOS", SND_SEQ_EVENT_SONGPOS); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_SONGSEL", SND_SEQ_EVENT_SONGSEL); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_QFRAME", SND_SEQ_EVENT_QFRAME); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_TIMESIGN", SND_SEQ_EVENT_TIMESIGN); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_KEYSIGN", SND_SEQ_EVENT_KEYSIGN); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_START", SND_SEQ_EVENT_START); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_CONTINUE", SND_SEQ_EVENT_CONTINUE); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_STOP", SND_SEQ_EVENT_STOP); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_SETPOS_TICK", SND_SEQ_EVENT_SETPOS_TICK); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_SETPOS_TIME", SND_SEQ_EVENT_SETPOS_TIME); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_TEMPO", SND_SEQ_EVENT_TEMPO); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_CLOCK", SND_SEQ_EVENT_CLOCK); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_TICK", SND_SEQ_EVENT_TICK); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_QUEUE_SKEW", SND_SEQ_EVENT_QUEUE_SKEW); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_SYNC_POS", SND_SEQ_EVENT_SYNC_POS); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_TUNE_REQUEST", SND_SEQ_EVENT_TUNE_REQUEST); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_RESET", SND_SEQ_EVENT_RESET); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_SENSING", SND_SEQ_EVENT_SENSING); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_ECHO", SND_SEQ_EVENT_ECHO); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_OSS", SND_SEQ_EVENT_OSS); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_CLIENT_START", SND_SEQ_EVENT_CLIENT_START); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_CLIENT_EXIT", SND_SEQ_EVENT_CLIENT_EXIT); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_CLIENT_CHANGE", SND_SEQ_EVENT_CLIENT_CHANGE); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_PORT_START", SND_SEQ_EVENT_PORT_START); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_PORT_EXIT", SND_SEQ_EVENT_PORT_EXIT); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_PORT_CHANGE", SND_SEQ_EVENT_PORT_CHANGE); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_PORT_SUBSCRIBED", SND_SEQ_EVENT_PORT_SUBSCRIBED); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_PORT_UNSUBSCRIBED", SND_SEQ_EVENT_PORT_UNSUBSCRIBED); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR0", SND_SEQ_EVENT_USR0); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR1", SND_SEQ_EVENT_USR1); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR2", SND_SEQ_EVENT_USR2); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR3", SND_SEQ_EVENT_USR3); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR4", SND_SEQ_EVENT_USR4); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR5", SND_SEQ_EVENT_USR5); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR6", SND_SEQ_EVENT_USR6); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR7", SND_SEQ_EVENT_USR7); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR8", SND_SEQ_EVENT_USR8); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR9", SND_SEQ_EVENT_USR9); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_SYSEX", SND_SEQ_EVENT_SYSEX); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_BOUNCE", SND_SEQ_EVENT_BOUNCE); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR_VAR0", SND_SEQ_EVENT_USR_VAR0); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR_VAR1", SND_SEQ_EVENT_USR_VAR1); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR_VAR2", SND_SEQ_EVENT_USR_VAR2); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR_VAR3", SND_SEQ_EVENT_USR_VAR3); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_USR_VAR4", SND_SEQ_EVENT_USR_VAR4); + TCONSTADD(module, EVENT_TYPE, "SEQ_EVENT_NONE", SND_SEQ_EVENT_NONE); /* SeqEvent event timestamp flags */ - TCONSTADD(module, EVENT_TIMESTAMP, - "SEQ_TIME_STAMP_TICK", - SND_SEQ_TIME_STAMP_TICK); - TCONSTADD(module, EVENT_TIMESTAMP, - "SEQ_TIME_STAMP_REAL", - SND_SEQ_TIME_STAMP_REAL); + TCONSTADD(module, EVENT_TIMESTAMP, "SEQ_TIME_STAMP_TICK", SND_SEQ_TIME_STAMP_TICK); + TCONSTADD(module, EVENT_TIMESTAMP, "SEQ_TIME_STAMP_REAL", SND_SEQ_TIME_STAMP_REAL); /* SeqEvent event timemode flags */ - TCONSTADD(module, EVENT_TIMEMODE, - "SEQ_TIME_MODE_ABS", - SND_SEQ_TIME_MODE_ABS); - TCONSTADD(module, EVENT_TIMEMODE, - "SEQ_TIME_MODE_REL", - SND_SEQ_TIME_MODE_REL); + TCONSTADD(module, EVENT_TIMEMODE, "SEQ_TIME_MODE_ABS", SND_SEQ_TIME_MODE_ABS); + TCONSTADD(module, EVENT_TIMEMODE, "SEQ_TIME_MODE_REL", SND_SEQ_TIME_MODE_REL); /* SeqEvent event addresses */ - TCONSTADD(module, ADDR_CLIENT, - "SEQ_CLIENT_SYSTEM", - SND_SEQ_CLIENT_SYSTEM); - TCONSTADD(module, ADDR_CLIENT, - "SEQ_ADDRESS_BROADCAST", - SND_SEQ_ADDRESS_BROADCAST); - TCONSTADD(module, ADDR_CLIENT, - "SEQ_ADDRESS_SUBSCRIBERS", - SND_SEQ_ADDRESS_SUBSCRIBERS); - TCONSTADD(module, ADDR_CLIENT, - "SEQ_ADDRESS_UNKNOWN", - SND_SEQ_ADDRESS_UNKNOWN); - TCONSTADD(module, ADDR_PORT, - "SEQ_PORT_SYSTEM_TIMER", - SND_SEQ_PORT_SYSTEM_TIMER); - TCONSTADD(module, ADDR_PORT, - "SEQ_PORT_SYSTEM_ANNOUNCE", - SND_SEQ_PORT_SYSTEM_ANNOUNCE); - TCONSTADD(module, ADDR_PORT, - "SEQ_ADDRESS_BROADCAST", - SND_SEQ_ADDRESS_BROADCAST); - TCONSTADD(module, ADDR_PORT, - "SEQ_ADDRESS_SUBSCRIBERS", - SND_SEQ_ADDRESS_SUBSCRIBERS); - TCONSTADD(module, ADDR_PORT, - "SEQ_ADDRESS_UNKNOWN", - SND_SEQ_ADDRESS_UNKNOWN); - + TCONSTADD(module, ADDR_CLIENT, "SEQ_CLIENT_SYSTEM", SND_SEQ_CLIENT_SYSTEM); + TCONSTADD(module, ADDR_CLIENT, "SEQ_ADDRESS_BROADCAST", SND_SEQ_ADDRESS_BROADCAST); + TCONSTADD(module, ADDR_CLIENT, "SEQ_ADDRESS_SUBSCRIBERS", SND_SEQ_ADDRESS_SUBSCRIBERS); + TCONSTADD(module, ADDR_CLIENT, "SEQ_ADDRESS_UNKNOWN", SND_SEQ_ADDRESS_UNKNOWN); + TCONSTADD(module, ADDR_PORT, "SEQ_PORT_SYSTEM_TIMER", SND_SEQ_PORT_SYSTEM_TIMER); + TCONSTADD(module, ADDR_PORT, "SEQ_PORT_SYSTEM_ANNOUNCE", SND_SEQ_PORT_SYSTEM_ANNOUNCE); + TCONSTADD(module, ADDR_PORT, "SEQ_ADDRESS_BROADCAST", SND_SEQ_ADDRESS_BROADCAST); + TCONSTADD(module, ADDR_PORT, "SEQ_ADDRESS_SUBSCRIBERS", SND_SEQ_ADDRESS_SUBSCRIBERS); + TCONSTADD(module, ADDR_PORT, "SEQ_ADDRESS_UNKNOWN", SND_SEQ_ADDRESS_UNKNOWN); + + return MOD_SUCCESS_VAL(module); } diff --git a/pyalsa/common.h b/pyalsa/common.h new file mode 100644 index 0000000..e0fcb2e --- /dev/null +++ b/pyalsa/common.h @@ -0,0 +1,128 @@ +/* + * Python binding for the ALSA library - Common macros + * Copyright (c) 2018 by Jaroslav Kysela + * + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __PYALSA_COMMON_H +#define __PYALSA_COMMON_H + +#include "Python.h" +#include "structmember.h" +#include "frameobject.h" +#ifndef PY_LONG_LONG + #define PY_LONG_LONG LONG_LONG +#endif + +#ifndef Py_RETURN_NONE + #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None +#endif +#ifndef Py_RETURN_TRUE + #define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True +#endif +#ifndef Py_RETURN_FALSE + #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False +#endif + +#if PY_MAJOR_VERSION >= 3 + #define PyInt_FromLong PyLong_FromLong +#endif + +/* + * + */ + +#if PY_MAJOR_VERSION >= 3 + #define MOD_ERROR_VAL NULL + #define MOD_SUCCESS_VAL(val) val + #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void) + #define MOD_DEF(ob, name, doc, methods) \ + static struct PyModuleDef moduledef = { \ + PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \ + ob = PyModule_Create(&moduledef); +#else + #define MOD_ERROR_VAL + #define MOD_SUCCESS_VAL(val) + #define MOD_INIT(name) void init##name(void) + #define MOD_DEF(ob, name, doc, methods) \ + ob = Py_InitModule3(name, methods, doc); +#endif + +/* + * + */ + +static inline PyObject *get_bool(int val) +{ + if (val) { + Py_INCREF(Py_True); + return Py_True; + } else { + Py_INCREF(Py_False); + return Py_False; + } +} + +static inline int get_long1(PyObject *o, long *val) +{ +#if PY_MAJOR_VERSION < 3 + if (PyInt_Check(o)) { + *val = PyInt_AsLong(o); + return 0; + } +#endif + if (PyLong_Check(o)) { + *val = PyLong_AsLong(o); + return 0; + } + return 1; +} + +static inline int get_long(PyObject *o, long *val) +{ + if (!get_long1(o, val)) + return 0; +#if PY_MAJOR_VERSION < 3 + PyErr_Format(PyExc_TypeError, "Only None, Int or Long types are expected!"); +#else + PyErr_Format(PyExc_TypeError, "Only None or Long types are expected!"); +#endif + return 1; +} + +static inline int get_utf8_string(PyObject *o, char **str) +{ + PyObject *e = PyUnicode_AsEncodedString(o, "utf-8", "strict"); + char *s = e ? PyBytes_AsString(e) : NULL; + if (s) + s = strdup(s); + *str = s; + Py_XDECREF(e); + return s == NULL; +} + +static inline PyObject *InternFromString(const char *name) +{ +#if PY_MAJOR_VERSION < 3 + return PyString_InternFromString("register"); +#else + return PyUnicode_InternFromString("register"); +#endif +} + +#endif /* __PYALSA_COMMON_H */ diff --git a/setup.py b/setup.py index 925d4eb..fe43277 100755 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -#! /usr/bin/python +#!/usr/bin/env python3 # -*- Python -*- import os @@ -59,19 +59,31 @@ setup( scripts=[] ) +SOFILES = [ +'alsacard', +'alsacontrol', +'alsahcontrol', +'alsamixer', +'alsaseq' +] + uname = os.uname() -a = 'build/lib.%s-%s-%s' % (uname[0].lower(), uname[4], sys.version[:3]) -for f in ['alsacard.so', 'alsacontrol.so', 'alsahcontrol.so', - 'alsamixer.so', 'alsaseq.so']: - if not os.path.exists('pyalsa/%s' % f): - a = '../build/lib.%s-%s-%s/pyalsa/%s' % \ - (uname[0].lower(), uname[4], sys.version[:3], f) - print a, f - p = 'pyalsa/' + f - try: - st = os.lstat(p) - if stat.S_ISLNK(st.st_mode): - os.remove(p) - except: - pass - os.symlink(a, 'pyalsa/%s' % f) +dir = 'build/lib.%s-%s-%s/pyalsa' % (uname[0].lower(), uname[4], sys.version[:3]) +files = os.listdir(dir) +for f in SOFILES: + path = '' + for f2 in files: + if f2.startswith(f + '.') and f2.endswith('.so'): + path = dir + '/' + f2 + break + if not path or not os.path.exists(path): + continue + p = 'pyalsa/%s.so' % f + print("%s -> %s" % (p, path)) + try: + st = os.lstat(p) + if stat.S_ISLNK(st.st_mode): + os.remove(p) + except: + pass + os.symlink('../' + path, 'pyalsa/%s.so' % f) diff --git a/test/alsamemdebug.py b/test/alsamemdebug.py index 7347019..59a7f49 100644 --- a/test/alsamemdebug.py +++ b/test/alsamemdebug.py @@ -1,4 +1,4 @@ -#! /usr/bin/python +#!/usr/bin/env python3 # -*- Python -*- import sys @@ -6,7 +6,7 @@ import gc def debuginit(): gc.set_debug(gc.DEBUG_LEAK | gc.DEBUG_STATS) - print "GC DEBUG: init." + print("GC DEBUG: init.") def debug(what): from sys import getrefcount @@ -14,9 +14,9 @@ def debug(what): for o in what: if getrefcount(o) - 3 != 1 or \ len(get_referrers(o)) - 2 != 1: - print 'GD DEBUG LEAK:', o, hex(id(o)), type(o), getrefcount(o)-3, len(get_referrers(o))-2 + print('GD DEBUG LEAK:', o, hex(id(o)), type(o), getrefcount(o)-3, len(get_referrers(o))-2) def debugdone(): gc.collect() - print "GC DEBUG LEAK done." + print("GC DEBUG LEAK done.") None diff --git a/test/cardtest1.py b/test/cardtest1.py index 5dd389d..045707a 100755 --- a/test/cardtest1.py +++ b/test/cardtest1.py @@ -1,4 +1,4 @@ -#! /usr/bin/python +#!/usr/bin/env python3 # -*- Python -*- import sys @@ -6,11 +6,11 @@ sys.path.insert(0, '../pyalsa') del sys import alsacard -print 'asoundlibVersion:', alsacard.asoundlib_version() -print 'cardLoad:', alsacard.card_load(0) -print 'cardList:', alsacard.card_list() -print 'deviceNameHint for all cards:' -print alsacard.device_name_hint(-1, "pcm") +print('asoundlibVersion:', alsacard.asoundlib_version()) +print('cardLoad:', alsacard.card_load(0)) +print('cardList:', alsacard.card_list()) +print('deviceNameHint for all cards:') +print(alsacard.device_name_hint(-1, "pcm")) for card in alsacard.card_list(): - print 'deviceNameHint for card #%i:' % card - print alsacard.device_name_hint(card, "pcm") + print('deviceNameHint for card #%i:' % card) + print(alsacard.device_name_hint(card, "pcm")) diff --git a/test/ctltest1.py b/test/ctltest1.py index b75beb9..53abbe9 100755 --- a/test/ctltest1.py +++ b/test/ctltest1.py @@ -1,4 +1,4 @@ -#! /usr/bin/python +#!/usr/bin/env python3 # -*- Python -*- import sys @@ -7,16 +7,16 @@ del sys import alsacontrol ctl = alsacontrol.Control() -print 'Card info:', ctl.card_info() +print('Card info:', ctl.card_info()) try: - print 'Hwdep devices:', ctl.hwdep_devices() -except IOError, msg: - print 'No hwdep devices:', msg + print('Hwdep devices:', ctl.hwdep_devices()) +except IOError as msg: + print('No hwdep devices:', msg) try: - print 'PCM devices:', ctl.pcm_devices() -except IOError, msg: - print 'No PCM devices:', msg + print('PCM devices:', ctl.pcm_devices()) +except IOError as msg: + print('No PCM devices:', msg) try: - print 'Rawmidi devices:', ctl.rawmidi_devices() -except IOError, msg: - print 'No rawmidi devices:', msg + print('Rawmidi devices:', ctl.rawmidi_devices()) +except IOError as msg: + print('No rawmidi devices:', msg) diff --git a/test/hctltest1.py b/test/hctltest1.py index a169500..7d0bf15 100755 --- a/test/hctltest1.py +++ b/test/hctltest1.py @@ -1,4 +1,4 @@ -#! /usr/bin/python +#!/usr/bin/env python3 # -*- Python -*- import sys @@ -23,7 +23,7 @@ def info(element): extra = '' if a == 'type': extra = ' (%s)' % alsahcontrol.element_type_name[info.type] - print ' %s: %s%s' % (a, getattr(info, a), extra) + print(' %s: %s%s' % (a, getattr(info, a), extra)) def value(element): info = alsahcontrol.Info(element) @@ -31,39 +31,39 @@ def value(element): for a in dir(value): if a.startswith('__'): continue - print ' %s: %s' % (a, getattr(value, a)) + print(' %s: %s' % (a, getattr(value, a))) values = value.get_tuple(info.type, info.count) - print ' Values: ', values + print(' Values:', values) value.set_tuple(info.type, values) value.read() if info.is_writable: value.write() -print 'interface_id:' -print ' ', alsahcontrol.interface_id -print 'interface_name:' -print ' ', alsahcontrol.interface_name -print 'element_type:' -print ' ', alsahcontrol.element_type -print 'element_type_name:' -print ' ', alsahcontrol.element_type_name -print 'event_class:' -print ' ', alsahcontrol.event_class -print 'event_mask:' -print ' ', alsahcontrol.event_mask -print 'event_mask_remove:', alsahcontrol.event_mask_remove -print ' ', alsahcontrol.open_mode -print 'event_mask_remove:', alsahcontrol.open_mode +print('interface_id:') +print(' ', alsahcontrol.interface_id) +print('interface_name:') +print(' ', alsahcontrol.interface_name) +print('element_type:') +print(' ', alsahcontrol.element_type) +print('element_type_name:') +print(' ', alsahcontrol.element_type_name) +print('event_class:') +print(' ', alsahcontrol.event_class) +print('event_mask:') +print(' ', alsahcontrol.event_mask) +print('event_mask_remove:', alsahcontrol.event_mask_remove) +print(' ', alsahcontrol.open_mode) +print('event_mask_remove:', alsahcontrol.open_mode) hctl = alsahcontrol.HControl() -print 'Count: ', hctl.count +print('Count: ', hctl.count) list = hctl.list() -print 'List:' -print list +print('List:') +print(list) for l in list: - print '*****' + print('*****') element1 = alsahcontrol.Element(hctl, l[1:]) info(element1) - print '-----' + print('-----') value(element1) del hctl diff --git a/test/hctltest2.py b/test/hctltest2.py index b388550..f34ee63 100755 --- a/test/hctltest2.py +++ b/test/hctltest2.py @@ -1,4 +1,4 @@ -#! /usr/bin/python +#!/usr/bin/env python3 # -*- Python -*- import sys @@ -22,8 +22,8 @@ def event_callback(element, events): info = alsahcontrol.Info(element) value = alsahcontrol.Value(element) - print 'CALLBACK (DEF)! [%s] %s:%i' % (parse_event_mask(events), element.name, element.index) - print ' ', value.get_tuple(info.type, info.count) + print('CALLBACK (DEF)! [%s] %s:%i' % (parse_event_mask(events), element.name, element.index)) + print(' ', value.get_tuple(info.type, info.count)) class MyElementEvent: @@ -31,8 +31,8 @@ class MyElementEvent: def callback(self, element, events): info = alsahcontrol.Info(element) value = alsahcontrol.Value(element) - print 'CALLBACK (CLASS)! [%s] %s:%i' % (parse_event_mask(events), element.name, element.index) - print ' ', value.get_tuple(info.type, info.count) + print('CALLBACK (CLASS)! [%s] %s:%i' % (parse_event_mask(events), element.name, element.index)) + print(' ', value.get_tuple(info.type, info.count)) hctl = alsahcontrol.HControl(mode=alsahcontrol.open_mode['NONBLOCK']) list = hctl.list() @@ -46,18 +46,18 @@ try: except IOError: pass hctl.element_new(alsahcontrol.element_type['INTEGER'], - nelementid, 2, 0, 100, 1) + nelementid, 2, 0, 100, 1) hctl.element_unlock(nelementid) # handleEvents() must be here to update internal alsa-lib's element list hctl.handle_events() element3 = alsahcontrol.Element(hctl, nelementid) element3.set_callback(event_callback) -print 'Watching (DEF): %s,%i' % (element1.name, element1.index) -print 'Watching (CLASS): %s,%i' % (element2.name, element2.index) -print 'Watching (DEF): %s,%i' % (element3.name, element3.index) +print('Watching (DEF): %s,%i' % (element1.name, element1.index)) +print('Watching (CLASS): %s,%i' % (element2.name, element2.index)) +print('Watching (DEF): %s,%i' % (element3.name, element3.index)) poller = select.poll() hctl.register_poll(poller) while True: poller.poll() - print 'Poll OK!' + print('Poll OK!') hctl.handle_events() diff --git a/test/mixertest1.py b/test/mixertest1.py index 1618a44..7693692 100755 --- a/test/mixertest1.py +++ b/test/mixertest1.py @@ -1,4 +1,4 @@ -#! /usr/bin/python +#!/usr/bin/env python3 # -*- Python -*- import sys @@ -10,59 +10,59 @@ import alsamixer def print_elem(e): direction = ["playback", "capture"] - print "Mixer Element '%s:%i':" % (e.name, e.index) - print ' is_active: %s' % e.is_active - print ' is_enumerated: %s' % e.is_enumerated - print ' has_common_volume: %s' % e.has_common_volume - print ' has_common_switch: %s' % e.has_common_switch - print ' has_capture_switch_exclusive: %s' % e.has_capture_switch_exclusive + print("Mixer Element '%s:%i':" % (e.name, e.index)) + print(' is_active: %s' % e.is_active) + print(' is_enumerated: %s' % e.is_enumerated) + print(' has_common_volume: %s' % e.has_common_volume) + print(' has_common_switch: %s' % e.has_common_switch) + print(' has_capture_switch_exclusive: %s' % e.has_capture_switch_exclusive) if e.has_switch(True): - print ' getCaptureGroup: %s' % e.get_capture_group + print(' getCaptureGroup: %s' % e.get_capture_group) for capture in [False, True]: - print ' is_%s_mono: %s' % (direction[capture], e.is_mono(capture)) - print ' has_%s_volume: %s' % (direction[capture], e.has_volume(capture)) + print(' is_%s_mono: %s' % (direction[capture], e.is_mono(capture))) + print(' has_%s_volume: %s' % (direction[capture], e.has_volume(capture))) if e.has_volume(capture): - print ' get_%s_volume_range: %s' % (direction[capture], e.get_volume_range(capture)) - print ' get_%s_volume_range_dB: %s' % (direction[capture], e.get_volume_range_dB(capture)) - print ' get_%s_volume_tuple: %s' % (direction[capture], e.get_volume_tuple(capture)) - print ' get_%s_volume_array: %s' % (direction[capture], e.get_volume_array(capture)) + print(' get_%s_volume_range: %s' % (direction[capture], e.get_volume_range(capture))) + print(' get_%s_volume_range_dB: %s' % (direction[capture], e.get_volume_range_dB(capture))) + print(' get_%s_volume_tuple: %s' % (direction[capture], e.get_volume_tuple(capture))) + print(' get_%s_volume_array: %s' % (direction[capture], e.get_volume_array(capture))) vrange = e.get_volume_range(capture) if vrange: for vol in range(vrange[0], vrange[1]+1): - print ' vol %i == %idB' % (vol, e.ask_volume_dB(vol, capture)) + print(' vol %i == %idB' % (vol, e.ask_volume_dB(vol, capture))) dbrange = e.get_volume_range_dB(capture) if dbrange: for vol in range(dbrange[0], dbrange[1]+1): - print ' dBvol %i == %i' % (vol, e.ask_dB_volume(vol, -1, capture)) - print ' has_%s_switch: %s' % (direction[capture], e.has_switch(capture)) + print(' dBvol %i == %i' % (vol, e.ask_dB_volume(vol, -1, capture))) + print(' has_%s_switch: %s' % (direction[capture], e.has_switch(capture))) if e.has_switch(capture): - print ' get_%s_switch_tuple: %s' % (direction[capture], e.get_switch_tuple(capture)) + print(' get_%s_switch_tuple: %s' % (direction[capture], e.get_switch_tuple(capture))) for channel in range(alsamixer.channel_id['LAST']+1): if e.has_channel(channel, capture): - print ' has_%s_channel%s: %s' % (direction[capture], channel, alsamixer.channel_name[channel]) + print( ' has_%s_channel%s: %s' % (direction[capture], channel, alsamixer.channel_name[channel])) debuginit() -print 'channel_id:' -print alsamixer.channel_id +print('channel_id:') +print(alsamixer.channel_id) -print 'channel_name:' -print alsamixer.channel_name +print('channel_name:') +print(alsamixer.channel_name) -print 'regopt_abstracts:' -print alsamixer.regopt_abstract +print('regopt_abstracts:') +print(alsamixer.regopt_abstract) -print 'event_mask:' -print alsamixer.event_mask +print('event_mask:') +print(alsamixer.event_mask) -print 'event_mask_remove:', alsamixer.event_mask_remove +print('event_mask_remove:', alsamixer.event_mask_remove) mixer = alsamixer.Mixer() mixer.attach() mixer.load() -print 'Element Count:', mixer.count -print 'Elements:' -print mixer.list() +print('Element Count:', mixer.count) +print('Elements:') +print(mixer.list()) element = alsamixer.Element(mixer, "PCM") element.set_volume_array([128, 128]) print_elem(element) -- 2.47.1