Removed
Link Here
|
1 |
https://git.alsa-project.org/?p=alsa-lib.git;a=commitdiff;h=345843fc24b3 |
2 |
|
3 |
--- modules/mixer/simple/python.c.orig 2016-08-02 17:48:38 UTC |
4 |
+++ modules/mixer/simple/python.c |
5 |
@@ -25,6 +25,10 @@ |
6 |
#include "asoundlib.h" |
7 |
#include "mixer_abst.h" |
8 |
|
9 |
+#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) |
10 |
+#pragma GCC diagnostic ignored "-Wstrict-aliasing" |
11 |
+#endif |
12 |
+ |
13 |
struct python_priv { |
14 |
int py_initialized; |
15 |
PyObject *py_event_func; |
16 |
@@ -56,20 +60,49 @@ struct pymixer { |
17 |
|
18 |
static PyInterpreterState *main_interpreter; |
19 |
|
20 |
+#if PY_MAJOR_VERSION >= 3 |
21 |
+ #define PyInt_FromLong PyLong_FromLong |
22 |
+#endif |
23 |
+ |
24 |
+static inline int get_long(PyObject *o, long *val) |
25 |
+{ |
26 |
+#if PY_MAJOR_VERSION < 3 |
27 |
+ if (PyInt_Check(o)) { |
28 |
+ *val = PyInt_AsLong(o); |
29 |
+ return 0; |
30 |
+ } |
31 |
+#endif |
32 |
+ if (PyLong_Check(o)) { |
33 |
+ *val = PyLong_AsLong(o); |
34 |
+ return 0; |
35 |
+ } |
36 |
+ return 1; |
37 |
+} |
38 |
+ |
39 |
+static inline PyObject *InternFromString(const char *name) |
40 |
+{ |
41 |
+#if PY_MAJOR_VERSION < 3 |
42 |
+ return PyString_InternFromString(name); |
43 |
+#else |
44 |
+ return PyUnicode_InternFromString(name); |
45 |
+#endif |
46 |
+} |
47 |
+ |
48 |
static void *get_C_ptr(PyObject *obj, const char *attr) |
49 |
{ |
50 |
PyObject *o; |
51 |
+ long val; |
52 |
|
53 |
- o = PyObject_GetAttr(obj, PyString_InternFromString(attr)); |
54 |
+ o = PyObject_GetAttr(obj, InternFromString(attr)); |
55 |
if (!o) { |
56 |
PyErr_Format(PyExc_TypeError, "missing '%s' attribute", attr); |
57 |
return NULL; |
58 |
} |
59 |
- if (!PyInt_Check(o)) { |
60 |
- PyErr_Format(PyExc_TypeError, "'%s' attribute is not integer", attr); |
61 |
+ if (get_long(o, &val)) { |
62 |
+ PyErr_Format(PyExc_TypeError, "'%s' attribute is not Int or Long", attr); |
63 |
return NULL; |
64 |
} |
65 |
- return (void *)PyInt_AsLong(o); |
66 |
+ return (void *)val; |
67 |
} |
68 |
|
69 |
static struct pymelem *melem_to_pymelem(snd_mixer_elem_t *elem) |
70 |
@@ -80,11 +113,11 @@ static struct pymelem *melem_to_pymelem(snd_mixer_elem |
71 |
static int pcall(struct pymelem *pymelem, const char *attr, PyObject *args, PyObject **_res) |
72 |
{ |
73 |
PyObject *obj = (PyObject *)pymelem, *res; |
74 |
- int xres = 0; |
75 |
+ long xres = 0; |
76 |
|
77 |
if (_res) |
78 |
*_res = NULL; |
79 |
- obj = PyObject_GetAttr(obj, PyString_InternFromString(attr)); |
80 |
+ obj = PyObject_GetAttr(obj, InternFromString(attr)); |
81 |
if (!obj) { |
82 |
PyErr_Format(PyExc_TypeError, "missing '%s' attribute", attr); |
83 |
PyErr_Print(); |
84 |
@@ -103,8 +136,12 @@ static int pcall(struct pymelem *pymelem, const char * |
85 |
*_res = res; |
86 |
res = PyTuple_GetItem(res, 0); |
87 |
} |
88 |
- if (PyInt_Check(res)) { |
89 |
+ if (PyLong_Check(res)) { |
90 |
+ xres = PyLong_AsLong(res); |
91 |
+#if PY_MAJOR_VERSION < 3 |
92 |
+ } else if (PyInt_Check(res)) { |
93 |
xres = PyInt_AsLong(res); |
94 |
+#endif |
95 |
} else if (res == Py_None) { |
96 |
xres = 0; |
97 |
} else if (PyBool_Check(res)) { |
98 |
@@ -155,7 +192,7 @@ static int is_ops(snd_mixer_elem_t *elem, int dir, int |
99 |
static int get_x_range_ops(snd_mixer_elem_t *elem, int dir, |
100 |
long *min, long *max, const char *attr) |
101 |
{ |
102 |
- PyObject *obj1, *res; |
103 |
+ PyObject *obj1, *t1, *t2, *res; |
104 |
struct pymelem *pymelem = melem_to_pymelem(elem); |
105 |
int err; |
106 |
|
107 |
@@ -163,21 +200,23 @@ static int get_x_range_ops(snd_mixer_elem_t *elem, int |
108 |
PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir)); |
109 |
err = pcall(pymelem, attr, obj1, &res); |
110 |
if (err >= 0) { |
111 |
- err = !PyInt_Check(PyTuple_GetItem(res, 1)) || !PyInt_Check(PyTuple_GetItem(res, 2)); |
112 |
- if (err) { |
113 |
- err = !PyLong_Check(PyTuple_GetItem(res, 1)) || !PyLong_Check(PyTuple_GetItem(res, 2)); |
114 |
- if (err) { |
115 |
- PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
116 |
- PyErr_Print(); |
117 |
- PyErr_Clear(); |
118 |
- err = -EIO; |
119 |
- } else { |
120 |
- *min = PyLong_AsLong(PyTuple_GetItem(res, 1)); |
121 |
- *max = PyLong_AsLong(PyTuple_GetItem(res, 2)); |
122 |
- } |
123 |
- } else { |
124 |
+ t1 = PyTuple_GetItem(res, 1); |
125 |
+ t2 = PyTuple_GetItem(res, 2); |
126 |
+ if (PyLong_Check(t1) && PyLong_Check(t2)) { |
127 |
+ *min = PyLong_AsLong(PyTuple_GetItem(res, 1)); |
128 |
+ *max = PyLong_AsLong(PyTuple_GetItem(res, 2)); |
129 |
+ err = 0; |
130 |
+#if PY_MAJOR_VERSION < 3 |
131 |
+ } else if (PyInt_Check(t1) && PyInt_Check(t2)) { |
132 |
*min = PyInt_AsLong(PyTuple_GetItem(res, 1)); |
133 |
*max = PyInt_AsLong(PyTuple_GetItem(res, 2)); |
134 |
+ err = 0; |
135 |
+#endif |
136 |
+ } else { |
137 |
+ PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
138 |
+ PyErr_Print(); |
139 |
+ PyErr_Clear(); |
140 |
+ err = -EIO; |
141 |
} |
142 |
} |
143 |
Py_XDECREF(res); |
144 |
@@ -207,7 +246,7 @@ static int get_x_ops(snd_mixer_elem_t *elem, int dir, |
145 |
long channel, long *value, |
146 |
const char *attr) |
147 |
{ |
148 |
- PyObject *obj1, *res; |
149 |
+ PyObject *obj1, *t1, *res; |
150 |
struct pymelem *pymelem = melem_to_pymelem(elem); |
151 |
int err; |
152 |
|
153 |
@@ -216,19 +255,20 @@ static int get_x_ops(snd_mixer_elem_t *elem, int dir, |
154 |
PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(channel)); |
155 |
err = pcall(pymelem, attr, obj1, &res); |
156 |
if (err >= 0) { |
157 |
- err = !PyInt_Check(PyTuple_GetItem(res, 1)); |
158 |
- if (err) { |
159 |
- err = !PyLong_Check(PyTuple_GetItem(res, 1)); |
160 |
- if (err) { |
161 |
- PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
162 |
- PyErr_Print(); |
163 |
- PyErr_Clear(); |
164 |
- err = -EIO; |
165 |
- } else { |
166 |
- *value = PyLong_AsLong(PyTuple_GetItem(res, 1)); |
167 |
- } |
168 |
+ t1 = PyTuple_GetItem(res, 1); |
169 |
+ if (PyLong_Check(t1)) { |
170 |
+ *value = PyLong_AsLong(t1); |
171 |
+ err = 0; |
172 |
+#if PY_MAJOR_VERSION < 3 |
173 |
+ } else if (PyInt_Check(t1)) { |
174 |
+ *value = PyInt_AsLong(t1); |
175 |
+ err = 0; |
176 |
+#endif |
177 |
} else { |
178 |
- *value = PyInt_AsLong(PyTuple_GetItem(res, 1)); |
179 |
+ PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
180 |
+ PyErr_Print(); |
181 |
+ PyErr_Clear(); |
182 |
+ err = -EIO; |
183 |
} |
184 |
} |
185 |
Py_XDECREF(res); |
186 |
@@ -265,7 +305,7 @@ static int ask_dB_vol_ops(snd_mixer_elem_t *elem, |
187 |
long *dbValue, |
188 |
int xdir) |
189 |
{ |
190 |
- PyObject *obj1, *res; |
191 |
+ PyObject *obj1, *t1, *res; |
192 |
struct pymelem *pymelem = melem_to_pymelem(elem); |
193 |
int err; |
194 |
|
195 |
@@ -275,19 +315,20 @@ static int ask_dB_vol_ops(snd_mixer_elem_t *elem, |
196 |
PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(xdir)); |
197 |
err = pcall(pymelem, "opsGetDBVol", obj1, &res); |
198 |
if (err >= 0) { |
199 |
- err = !PyInt_Check(PyTuple_GetItem(res, 1)); |
200 |
- if (err) { |
201 |
- err = !PyLong_Check(PyTuple_GetItem(res, 1)); |
202 |
- if (err) { |
203 |
- PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
204 |
- PyErr_Print(); |
205 |
- PyErr_Clear(); |
206 |
- err = -EIO; |
207 |
- } else { |
208 |
- *dbValue = PyLong_AsLong(PyTuple_GetItem(res, 1)); |
209 |
- } |
210 |
+ t1 = PyTuple_GetItem(res, 1); |
211 |
+ if (PyLong_Check(t1)) { |
212 |
+ *dbValue = PyLong_AsLong(t1); |
213 |
+ err = 0; |
214 |
+#if PY_MAJOR_VERSION < 3 |
215 |
+ } else if (PyInt_Check(t1)) { |
216 |
+ *dbValue = PyInt_AsLong(t1); |
217 |
+ err = 0; |
218 |
+#endif |
219 |
} else { |
220 |
- *dbValue = PyInt_AsLong(PyTuple_GetItem(res, 1)); |
221 |
+ PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
222 |
+ PyErr_Print(); |
223 |
+ PyErr_Clear(); |
224 |
+ err = -EIO; |
225 |
} |
226 |
} |
227 |
Py_XDECREF(res); |
228 |
@@ -353,7 +394,7 @@ static int enum_item_name_ops(snd_mixer_elem_t *elem, |
229 |
unsigned int item, |
230 |
size_t maxlen, char *buf) |
231 |
{ |
232 |
- PyObject *obj1, *res; |
233 |
+ PyObject *obj1, *obj2, *t1, *res; |
234 |
struct pymelem *pymelem = melem_to_pymelem(elem); |
235 |
int err; |
236 |
unsigned int len; |
237 |
@@ -363,19 +404,35 @@ static int enum_item_name_ops(snd_mixer_elem_t *elem, |
238 |
PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(item)); |
239 |
err = pcall(pymelem, "opsGetEnumItemName", obj1, &res); |
240 |
if (err >= 0) { |
241 |
- err = !PyString_Check(PyTuple_GetItem(res, 1)); |
242 |
- if (err) { |
243 |
- PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
244 |
- PyErr_Print(); |
245 |
- PyErr_Clear(); |
246 |
- err = -EIO; |
247 |
- } else { |
248 |
- s = PyString_AsString(PyTuple_GetItem(res, 1)); |
249 |
+ t1 = PyTuple_GetItem(res, 1); |
250 |
+ if (PyUnicode_Check(t1)) { |
251 |
+ obj2 = PyUnicode_AsEncodedString(t1, "utf-8", "strict"); |
252 |
+ if (obj2) { |
253 |
+ s = PyBytes_AsString(obj2); |
254 |
+ len = strlen(s); |
255 |
+ if (maxlen - 1 > len) |
256 |
+ len = maxlen - 1; |
257 |
+ memcpy(buf, s, len); |
258 |
+ buf[len] = '\0'; |
259 |
+ Py_DECREF(obj2); |
260 |
+ } else { |
261 |
+ goto errlbl; |
262 |
+ } |
263 |
+#if PY_MAJOR_VERSION < 3 |
264 |
+ } else if (PyString_Check(t1)) { |
265 |
+ s = PyString_AsString(t1); |
266 |
len = strlen(s); |
267 |
if (maxlen - 1 > len) |
268 |
len = maxlen - 1; |
269 |
memcpy(buf, s, len); |
270 |
buf[len] = '\0'; |
271 |
+#endif |
272 |
+ } else { |
273 |
+errlbl: |
274 |
+ PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
275 |
+ PyErr_Print(); |
276 |
+ PyErr_Clear(); |
277 |
+ err = -EIO; |
278 |
} |
279 |
} |
280 |
Py_XDECREF(res); |
281 |
@@ -386,7 +443,7 @@ static int get_enum_item_ops(snd_mixer_elem_t *elem, |
282 |
snd_mixer_selem_channel_id_t channel, |
283 |
unsigned int *itemp) |
284 |
{ |
285 |
- PyObject *obj1, *res; |
286 |
+ PyObject *obj1, *t1, *res; |
287 |
struct pymelem *pymelem = melem_to_pymelem(elem); |
288 |
int err; |
289 |
|
290 |
@@ -394,14 +451,20 @@ static int get_enum_item_ops(snd_mixer_elem_t *elem, |
291 |
PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(channel)); |
292 |
err = pcall(pymelem, "opsGetEnumItem", obj1, &res); |
293 |
if (err >= 0) { |
294 |
- err = !PyInt_Check(PyTuple_GetItem(res, 1)); |
295 |
- if (err) { |
296 |
+ t1 = PyTuple_GetItem(res, 1); |
297 |
+ if (PyLong_Check(t1)) { |
298 |
+ *itemp = PyLong_AsLong(t1); |
299 |
+ err = 0; |
300 |
+#if PY_MAJOR_VERSION < 3 |
301 |
+ } else if (PyInt_Check(t1)) { |
302 |
+ *itemp = PyInt_AsLong(t1); |
303 |
+ err = 0; |
304 |
+#endif |
305 |
+ } else { |
306 |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)"); |
307 |
PyErr_Print(); |
308 |
PyErr_Clear(); |
309 |
err = -EIO; |
310 |
- } else { |
311 |
- *itemp = PyInt_AsLong(PyTuple_GetItem(res, 1)); |
312 |
} |
313 |
} |
314 |
Py_XDECREF(res); |
315 |
@@ -464,7 +527,7 @@ pymelem_get_caps(struct pymelem *pymelem, void *priv A |
316 |
static PyObject * |
317 |
pymelem_get_name(struct pymelem *pymelem, void *priv ATTRIBUTE_UNUSED) |
318 |
{ |
319 |
- return PyString_FromString(snd_mixer_selem_id_get_name(pymelem->selem.id)); |
320 |
+ return PyUnicode_FromString(snd_mixer_selem_id_get_name(pymelem->selem.id)); |
321 |
} |
322 |
|
323 |
static PyObject * |
324 |
@@ -476,12 +539,18 @@ pymelem_get_index(struct pymelem *pymelem, void *priv |
325 |
static int |
326 |
pymelem_set_caps(struct pymelem *pymelem, PyObject *val, void *priv ATTRIBUTE_UNUSED) |
327 |
{ |
328 |
- if (!PyInt_Check(val)) { |
329 |
- PyErr_SetString(PyExc_TypeError, "The last attribute value must be an integer"); |
330 |
- return -1; |
331 |
+ if (PyLong_Check(val)) { |
332 |
+ pymelem->selem.caps = PyLong_AsLong(val); |
333 |
+ return 0; |
334 |
} |
335 |
- pymelem->selem.caps = PyInt_AsLong(val); |
336 |
- return 0; |
337 |
+#if PY_MAJOR_VERSION < 3 |
338 |
+ if (PyInt_Check(val)) { |
339 |
+ pymelem->selem.caps = PyInt_AsLong(val); |
340 |
+ return 0; |
341 |
+ } |
342 |
+#endif |
343 |
+ PyErr_SetString(PyExc_TypeError, "The last attribute value must be an integer"); |
344 |
+ return -1; |
345 |
} |
346 |
|
347 |
static PyObject * |
348 |
@@ -588,7 +657,6 @@ static void |
349 |
pymelem_dealloc(struct pymelem *self) |
350 |
{ |
351 |
selem_free(self->melem); |
352 |
- self->ob_type->tp_free(self); |
353 |
} |
354 |
|
355 |
static PyGetSetDef pymelem_getseters[] = { |
356 |
@@ -634,7 +702,7 @@ static PyMethodDef pymelem_methods[] = { |
357 |
}; |
358 |
|
359 |
static PyTypeObject pymelem_type = { |
360 |
- PyObject_HEAD_INIT(0) |
361 |
+ PyVarObject_HEAD_INIT(NULL, 0) |
362 |
tp_name: "smixer_python.InternalMElement", |
363 |
tp_basicsize: sizeof(struct pymelem), |
364 |
tp_dealloc: (destructor)pymelem_dealloc, |
365 |
@@ -708,7 +776,7 @@ pymixer_melement_new(struct pymixer *pymixer, PyObject |
366 |
obj1 = PyTuple_New(4); |
367 |
if (PyTuple_SET_ITEM(obj1, 0, (PyObject *)pymixer)) |
368 |
Py_INCREF((PyObject *)pymixer); |
369 |
- PyTuple_SET_ITEM(obj1, 1, PyString_FromString(name)); |
370 |
+ PyTuple_SET_ITEM(obj1, 1, PyUnicode_FromString(name)); |
371 |
PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(index)); |
372 |
PyTuple_SET_ITEM(obj1, 3, PyInt_FromLong(weight)); |
373 |
obj2 = PyObject_CallObject(obj, obj1); |
374 |
@@ -800,7 +868,6 @@ static void |
375 |
pymixer_dealloc(struct pymixer *self) |
376 |
{ |
377 |
pymixer_free(self); |
378 |
- self->ob_type->tp_free(self); |
379 |
} |
380 |
|
381 |
static PyGetSetDef pymixer_getseters[] = { |
382 |
@@ -816,7 +883,7 @@ static PyMethodDef pymixer_methods[] = { |
383 |
}; |
384 |
|
385 |
static PyTypeObject pymixer_type = { |
386 |
- PyObject_HEAD_INIT(0) |
387 |
+ PyVarObject_HEAD_INIT(NULL, 0) |
388 |
tp_name: "smixer_python.InternalMixer", |
389 |
tp_basicsize: sizeof(struct pymixer), |
390 |
tp_dealloc: (destructor)pymixer_dealloc, |
391 |
@@ -910,12 +977,12 @@ int alsa_mixer_simple_event(snd_mixer_class_t *class, |
392 |
snd_hctl_elem_t *helem, snd_mixer_elem_t *melem) |
393 |
{ |
394 |
struct python_priv *priv = snd_mixer_sbasic_get_private(class); |
395 |
- PyThreadState *tstate, *origstate; |
396 |
+ PyThreadState *tstate; |
397 |
PyObject *t, *o, *r; |
398 |
int res = -ENOMEM; |
399 |
|
400 |
tstate = PyThreadState_New(main_interpreter); |
401 |
- origstate = PyThreadState_Swap(tstate); |
402 |
+ PyThreadState_Swap(tstate); |
403 |
|
404 |
t = PyTuple_New(3); |
405 |
if (t) { |
406 |
@@ -935,8 +1002,12 @@ int alsa_mixer_simple_event(snd_mixer_class_t *class, |
407 |
r = PyObject_CallObject(priv->py_event_func, t); |
408 |
Py_DECREF(t); |
409 |
if (r) { |
410 |
- if (PyInt_Check(r)) { |
411 |
+ if (PyLong_Check(r)) { |
412 |
+ res = PyLong_AsLong(r); |
413 |
+#if PY_MAJOR_VERSION < 3 |
414 |
+ } else if (PyInt_Check(r)) { |
415 |
res = PyInt_AsLong(r); |
416 |
+#endif |
417 |
} else if (r == Py_None) { |
418 |
res = 0; |
419 |
} |
420 |
@@ -966,6 +1037,71 @@ static void alsa_mixer_simple_free(snd_mixer_class_t * |
421 |
free(priv); |
422 |
} |
423 |
|
424 |
+static int alsa_mixer_simple_pyinit(struct python_priv *priv, |
425 |
+ PyObject *py_mod, |
426 |
+ FILE *fp, |
427 |
+ const char *file, |
428 |
+ snd_mixer_class_t *class, |
429 |
+ snd_mixer_t *mixer, |
430 |
+ const char *device) |
431 |
+{ |
432 |
+ PyObject *obj, *obj1, *obj2, *mdict; |
433 |
+ |
434 |
+ mdict = priv->py_mdict = PyModule_GetDict(py_mod); |
435 |
+ obj = PyUnicode_FromString(file); |
436 |
+ if (obj) |
437 |
+ PyDict_SetItemString(mdict, "__file__", obj); |
438 |
+ Py_XDECREF(obj); |
439 |
+ obj = PyUnicode_FromString(device); |
440 |
+ if (obj) |
441 |
+ PyDict_SetItemString(mdict, "device", obj); |
442 |
+ Py_XDECREF(obj); |
443 |
+ Py_INCREF(&pymelem_type); |
444 |
+ Py_INCREF(&pymixer_type); |
445 |
+ PyModule_AddObject(py_mod, "InternalMElement", (PyObject *)&pymelem_type); |
446 |
+ PyModule_AddObject(py_mod, "InternalMixer", (PyObject *)&pymixer_type); |
447 |
+ obj = PyDict_GetItemString(mdict, "InternalMixer"); |
448 |
+ if (obj) { |
449 |
+ obj1 = PyTuple_New(3); |
450 |
+ PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong((long)class)); |
451 |
+ PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong((long)mixer)); |
452 |
+ if (PyTuple_SET_ITEM(obj1, 2, mdict)) |
453 |
+ Py_INCREF(mdict); |
454 |
+ obj2 = PyObject_CallObject(obj, obj1); |
455 |
+ Py_XDECREF(obj1); |
456 |
+ PyDict_SetItemString(mdict, "mixer", obj2); |
457 |
+ priv->py_mixer = obj2; |
458 |
+ } else { |
459 |
+ SNDERR("Unable to create InternalMixer object"); |
460 |
+ return -EIO; |
461 |
+ } |
462 |
+ |
463 |
+ obj = PyRun_FileEx(fp, file, Py_file_input, mdict, mdict, 1); |
464 |
+ if (obj == NULL) |
465 |
+ PyErr_Print(); |
466 |
+ Py_XDECREF(obj); |
467 |
+ priv->py_event_func = PyDict_GetItemString(mdict, "event"); |
468 |
+ if (priv->py_event_func == NULL) { |
469 |
+ SNDERR("Unable to find python function 'event'"); |
470 |
+ return -EIO; |
471 |
+ } |
472 |
+ return 0; |
473 |
+} |
474 |
+ |
475 |
+#if PY_MAJOR_VERSION >= 3 |
476 |
+static struct PyModuleDef smixer_python_module = { |
477 |
+ PyModuleDef_HEAD_INIT, |
478 |
+ "smixer_python", |
479 |
+ NULL, |
480 |
+ 0, |
481 |
+ python_methods, |
482 |
+ NULL, |
483 |
+ NULL, |
484 |
+ NULL, |
485 |
+ NULL |
486 |
+}; |
487 |
+#endif |
488 |
+ |
489 |
int alsa_mixer_simple_finit(snd_mixer_class_t *class, |
490 |
snd_mixer_t *mixer, |
491 |
const char *device) |
492 |
@@ -973,7 +1109,7 @@ int alsa_mixer_simple_finit(snd_mixer_class_t *class, |
493 |
struct python_priv *priv; |
494 |
FILE *fp; |
495 |
const char *file; |
496 |
- PyObject *obj, *obj1, *obj2, *py_mod, *mdict; |
497 |
+ PyObject *obj, *py_mod; |
498 |
|
499 |
priv = calloc(1, sizeof(*priv)); |
500 |
if (priv == NULL) |
501 |
@@ -993,54 +1129,21 @@ int alsa_mixer_simple_finit(snd_mixer_class_t *class, |
502 |
} |
503 |
|
504 |
Py_Initialize(); |
505 |
- if (PyType_Ready(&pymelem_type) < 0) |
506 |
+ if (PyType_Ready(&pymelem_type) < 0 || |
507 |
+ PyType_Ready(&pymixer_type) < 0) { |
508 |
+ fclose(fp); |
509 |
return -EIO; |
510 |
- if (PyType_Ready(&pymixer_type) < 0) |
511 |
- return -EIO; |
512 |
+ } |
513 |
+#if PY_MAJOR_VERSION < 3 |
514 |
Py_InitModule("smixer_python", python_methods); |
515 |
+#else |
516 |
+ PyModule_Create(&smixer_python_module); |
517 |
+#endif |
518 |
priv->py_initialized = 1; |
519 |
main_interpreter = PyThreadState_Get()->interp; |
520 |
obj = PyImport_GetModuleDict(); |
521 |
py_mod = PyDict_GetItemString(obj, "__main__"); |
522 |
- if (py_mod) { |
523 |
- mdict = priv->py_mdict = PyModule_GetDict(py_mod); |
524 |
- obj = PyString_FromString(file); |
525 |
- if (obj) |
526 |
- PyDict_SetItemString(mdict, "__file__", obj); |
527 |
- Py_XDECREF(obj); |
528 |
- obj = PyString_FromString(device); |
529 |
- if (obj) |
530 |
- PyDict_SetItemString(mdict, "device", obj); |
531 |
- Py_XDECREF(obj); |
532 |
- Py_INCREF(&pymixer_type); |
533 |
- PyModule_AddObject(py_mod, "InternalMElement", (PyObject *)&pymelem_type); |
534 |
- PyModule_AddObject(py_mod, "InternalMixer", (PyObject *)&pymixer_type); |
535 |
- obj = PyDict_GetItemString(mdict, "InternalMixer"); |
536 |
- if (obj) { |
537 |
- obj1 = PyTuple_New(3); |
538 |
- PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong((long)class)); |
539 |
- PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong((long)mixer)); |
540 |
- if (PyTuple_SET_ITEM(obj1, 2, mdict)) |
541 |
- Py_INCREF(mdict); |
542 |
- obj2 = PyObject_CallObject(obj, obj1); |
543 |
- Py_XDECREF(obj1); |
544 |
- PyDict_SetItemString(mdict, "mixer", obj2); |
545 |
- priv->py_mixer = obj2; |
546 |
- } else { |
547 |
- SNDERR("Unable to create InternalMixer object"); |
548 |
- return -EIO; |
549 |
- } |
550 |
- |
551 |
- |
552 |
- obj = PyRun_FileEx(fp, file, Py_file_input, mdict, mdict, 1); |
553 |
- if (obj == NULL) |
554 |
- PyErr_Print(); |
555 |
- Py_XDECREF(obj); |
556 |
- priv->py_event_func = PyDict_GetItemString(mdict, "event"); |
557 |
- if (priv->py_event_func == NULL) { |
558 |
- SNDERR("Unable to find python function 'event'"); |
559 |
- return -EIO; |
560 |
- } |
561 |
- } |
562 |
+ if (py_mod) |
563 |
+ alsa_mixer_simple_pyinit(priv, py_mod, fp, file, class, mixer, device); |
564 |
return 0; |
565 |
} |