1
1
from cython.operator cimport dereference as deref
2
- from python_ref cimport Py_DECREF, Py_INCREF
2
+ from cpython cimport Py_DECREF, Py_INCREF
3
3
4
4
cimport ga
5
5
cimport gau
@@ -21,14 +21,21 @@ def update():
21
21
22
22
23
23
cdef on_finish_callback(ga.Handle* in_handle, void * in_context):
24
- (< object > in_context)()
25
- print " after callback"
24
+ cdef CallbackContext context = < CallbackContext> in_context
25
+ context.callback(context.sound)
26
+ # note: it was casted on void* so we need to manually decrease
27
+ # reference counter
28
+ Py_DECREF(context)
26
29
ga.handle_destroy(in_handle)
27
30
28
31
29
- ctypedef struct CallbackContext:
30
- void * callback
31
- void * sound
32
+ cdef class CallbackContext(object ):
33
+ cdef object callback
34
+ cdef Sound sound
35
+
36
+ def __cinit__ (self , object callback , Sound sound ):
37
+ self .callback = callback
38
+ self .sound = sound
32
39
33
40
34
41
cdef class Sound(object ):
@@ -52,13 +59,20 @@ cdef class Sound(object):
52
59
53
60
def play (self , on_finish = None ):
54
61
cdef ga.Handle* handle
62
+ cdef CallbackContext context
55
63
56
64
if on_finish:
65
+ context = CallbackContext(on_finish, self )
66
+
67
+ # note: we are going to cast on void* so we need to manually
68
+ # control reference counters
69
+ Py_INCREF(context)
70
+
57
71
handle = gau.create_handle_sound(
58
72
global_mixer,
59
73
self .sound,
60
74
< ga.FinishCallback> & on_finish_callback,
61
- < void * > on_finish ,
75
+ < void * > context ,
62
76
NULL
63
77
)
64
78
else :
0 commit comments