aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source/bound.cpp
blob: e8e2599fde6c545e72803c1d516fe59839e214b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* 

py/pyext - python external object for PD and MaxMSP

Copyright (c) 2002-2003 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

#include "pyext.h"
#include "flinternal.h"

#ifndef USEFLEXTBINDING
t_class *pyext::px_class;
pyext::py_proxy *pyext::px_head,*pyext::px_tail;

void pyext::py_proxy::px_method(py_proxy *obj,const t_symbol *s,int argc,const t_atom *argv)
{
	PY_LOCK

	PyObject *args = MakePyArgs(s,AtomList(argc,argv),-1,obj->self != NULL);
	PyObject *ret = PyObject_CallObject(obj->func,args);
	if(!ret) {
		PyErr_Print();
	}
	Py_XDECREF(ret);

	PY_UNLOCK
}

#else
struct bounddata { PyObject *self,*func; };

bool pyext::boundmeth(flext_base *,const t_symbol *sym,int argc,const t_atom *argv,void *data)
{
    bounddata *obj = (bounddata *)data;

	PY_LOCK

	PyObject *args = MakePyArgs(sym,AtomList(argc,argv),-1,obj->self != NULL);
	PyObject *ret = PyObject_CallObject(obj->func,args);
	if(!ret) {
		PyErr_Print();
	}
	Py_XDECREF(ret);

	PY_UNLOCK
    return true;
}
#endif

PyObject *pyext::pyext_bind(PyObject *,PyObject *args)
{
    PyObject *self,*meth;
	C *name;
    if(!PyArg_ParseTuple(args, "OsO:pyext_bind", &self,&name,&meth))
		post("py/pyext - Wrong arguments!");
	else if(!PyInstance_Check(self) || !(PyMethod_Check(meth) || PyFunction_Check(meth))) {
		post("py/pyext - Wrong argument types!");
    }
	else {
		const t_symbol *recv = MakeSymbol(name);
/*
		if(GetBound(recv))
			post("py/pyext - Symbol \"%s\" is already hooked",GetString(recv));
*/		
		// make a proxy object

		if(PyMethod_Check(meth)) {
			PyObject *no = PyObject_GetAttrString(meth,"__name__");
			meth  = PyObject_GetAttr(self,no); 
			Py_DECREF(no);
		}
#ifndef USEFLEXTBINDING
		py_proxy *px = (py_proxy *)object_new(px_class);
		px->init(recv,self,meth);  

		// add it to the list
		if(px_tail) px_tail->nxt = px;
		else px_head = px;
		px_tail = px;

		// Do bind
		pd_bind(&px->obj.ob_pd,(t_symbol *)recv);  
#else
        bounddata *data = new bounddata;
        data->self = self;
        data->func = meth;
        GetThis(self)->BindMethod(recv,boundmeth,data);
#endif

		Py_INCREF(self); // self is borrowed reference
	}

    Py_INCREF(Py_None);
    return Py_None;
}

PyObject *pyext::pyext_unbind(PyObject *,PyObject *args)
{
    PyObject *self,*meth;
	C *name;
    if(!PyArg_ParseTuple(args, "OsO:pyext_bind", &self,&name,&meth))
		post("py/pyext - Wrong arguments!");
	else if(!PyInstance_Check(self) || !(PyMethod_Check(meth) || PyFunction_Check(meth))) {
		post("py/pyext - Wrong argument types!");
    }
	else {
		const t_symbol *recv = MakeSymbol(name);
		if(PyMethod_Check(meth)) {
			PyObject *no = PyObject_GetAttrString(meth,"__name__");
			meth  = PyObject_GetAttr(self,no); // meth is given a new reference!
			Py_DECREF(no);
		}

#ifndef USEFLEXTBINDING
		// search proxy object
		py_proxy *pp = NULL,*px = px_head;
		while(px) {
			py_proxy *pn = px->nxt;
			if(recv == px->name && self == px->self && meth == px->func) {
				if(pp)
					pp->nxt = pn;
				else
					px_head = pn;
				if(!pn) px_tail = pp;
				break;
			}
			else pp = px;
			px = pn;
		}

		// do unbind
		if(px) {
			pd_unbind(&px->obj.ob_pd,(t_symbol *)recv);  
			object_free(&px->obj);

			Py_DECREF(self);
			if(PyMethod_Check(meth)) Py_DECREF(meth);
		}
#else
        void *data = NULL;
        if(GetThis(self)->UnbindMethod(recv,boundmeth,&data)) {
            bounddata *bdt = (bounddata *)data; 
            if(bdt) {
		        Py_DECREF(bdt->self);
		        if(PyMethod_Check(bdt->func)) Py_DECREF(bdt->func);
                if(data) delete bdt; 
            }
        }
#endif
	}

    Py_INCREF(Py_None);
    return Py_None;
}


V pyext::ClearBinding()
{
#ifndef USEFLEXTBINDING
	// search proxy object
	py_proxy *pp = NULL,*px = px_head;
	while(px) {
		py_proxy *pn = px->nxt;
		if(px->self == pyobj) {
			if(pp)
				pp->nxt = pn;
			else
				px_head = pn;
			if(!pn) px_tail = pp;

			Py_DECREF(px->self);
			if(PyMethod_Check(px->func)) Py_DECREF(px->func);

			pd_unbind(&px->obj.ob_pd,(t_symbol *)px->name);  
			object_free(&px->obj);
		}
		else pp = px;	
		px = pn;
	}
#else
    void *data = NULL;
    const t_symbol *sym = NULL;

    // unbind all 
    while(GetThis(pyobj)->UnbindMethod(sym,NULL,&data)) {
        bounddata *bdt = (bounddata *)data; 
        if(bdt) {
		    Py_DECREF(bdt->self);
		    if(PyMethod_Check(bdt->func)) Py_DECREF(bdt->func);
            if(data) delete bdt; 
        }
    }
#endif
}