aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source/pyargs.cpp
blob: 38cfbc54d4279ffd23bb687b0799f66959040da8 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* 

py/pyext - python external object for PD and MaxMSP

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

*/

#include "pybase.h"
#include "pyatom.h"

static const t_symbol *symatom = flext::MakeSymbol(" py ");

static PyObject *MakePyAtom(const t_atom &at)
{
	if(flext::IsSymbol(at)) 
        return pySymbol_FromSymbol(flext::GetSymbol(at));
#if 1
    else if(flext::CanbeFloat(at)) {
        // if a number can be an integer... let it be an integer!
        int ival = flext::GetAInt(at);
        double fval = flext::GetAFloat(at);
        return (double)ival == fval?PyInt_FromLong(ival):PyFloat_FromDouble(fval);
    }
#else
    else if(flext::IsFloat(at))
        return PyFloat_FromDouble(flext::GetFloat(at));
    else if(flext::IsInt(at))
        return PyInt_FromLong(flext::GetInt(at));
#endif
    return NULL;
}

static PyObject *MakePyAtom(int argc,const t_atom *argv)
{
    if(argc != sizeof(size_t)/2) return NULL;

    size_t atom = 0;
    for(int i = sizeof(size_t)/2-1; i >= 0; --i)
        if(!flext::CanbeInt(argv[i])) { 
            atom = 0; 
            break; 
        }
        else
            atom = (atom<<16)+flext::GetAInt(argv[i]);

    if(atom) {
        PyObject *el = PyAtom::Retrieve(atom);
        if(!el) el = Py_None; // object already gone....
        Py_INCREF(el);
        return el;
    }
    else
        return NULL;
}

PyObject *pybase::MakePyArgs(const t_symbol *s,int argc,const t_atom *argv,int inlet)
{
	PyObject *ret,*el;

    if(s == symatom && (el = MakePyAtom(argc,argv)) != NULL) {
    	ret = PyTuple_New(1);
		PyTuple_SET_ITEM(ret,0,el);             
    }
    else {
	    bool any = IsAnything(s);
	    ret = PyTuple_New(argc+(any?1:0)+(inlet >= 0?1:0));

	    int pix = 0;

	    if(inlet >= 0)
		    PyTuple_SET_ITEM(ret,pix++,PyInt_FromLong(inlet)); 

	    if(any)
		    PyTuple_SET_ITEM(ret,pix++,pySymbol_FromSymbol(s)); 

	    for(int i = 0; i < argc; ++i) {
		    el = MakePyAtom(argv[i]);
		    if(!el) {
			    post("py/pyext: cannot convert argument %i",any?i+1:i);
                
                el = Py_None;
                Py_INCREF(Py_None);
            }
		    
		    PyTuple_SET_ITEM(ret,pix++,el); // reference stolen
	    }
    }

	return ret;
}

PyObject *pybase::MakePyArg(const t_symbol *s,int argc,const t_atom *argv)
{
	PyObject *ret;

    if(s == symatom && (ret = MakePyAtom(argc,argv)) != NULL) {
        // ok!
    }
    else if(argc == 1 && !IsAnything(s))
        // convert atoms and one-element lists
		ret = MakePyAtom(*argv);       
    else {
	    bool any = s != sym_list;
	    ret = PyTuple_New(argc+(any?1:0));

	    int pix = 0;
	    if(any)
		    PyTuple_SET_ITEM(ret,pix++,pySymbol_FromSymbol(s)); 

	    for(int i = 0; i < argc; ++i) {
		    PyObject *el = MakePyAtom(argv[i]);
		    if(!el) {
			    post("py/pyext: cannot convert argument %i",any?i+1:i);
                
                el = Py_None;
                Py_INCREF(Py_None);
            }
    		
		    PyTuple_SET_ITEM(ret,pix++,el); // reference stolen
	    }
    }

	return ret;
}

inline bool issym(PyObject *p)
{
    return PyString_Check(p) || pySymbol_Check(p);
}

inline bool isseq(PyObject *p)
{
    return PySequence_Check(p) && !issym(p);
}

const t_symbol *pybase::getone(t_atom &at,PyObject *arg)
{
    if(PyInt_Check(arg)) { flext::SetInt(at,PyInt_AsLong(arg)); return sym_fint; }
    else if(PyLong_Check(arg)) { flext::SetInt(at,PyLong_AsLong(arg)); return sym_fint; }
    else if(PyFloat_Check(arg)) { flext::SetFloat(at,(float)PyFloat_AsDouble(arg)); return flext::sym_float; }
    else if(pySymbol_Check(arg)) { flext::SetSymbol(at,pySymbol_AS_SYMBOL(arg)); return flext::sym_symbol; }
    else if(PyString_Check(arg)) { flext::SetString(at,PyString_AS_STRING(arg)); return flext::sym_symbol; }
	else {
		PyObject *tp = PyObject_Type(arg);
		PyObject *stp = tp?PyObject_Str(tp):NULL;
		char *tmp = "";
		if(stp) tmp = PyString_AS_STRING(stp);
		flext::post("py/pyext: Could not convert argument %s",tmp);
		Py_XDECREF(stp);
		Py_XDECREF(tp);

        flext::SetSymbol(at,flext::sym__); 
        return sym_symbol;
	}
}

const t_symbol *pybase::getlist(t_atom *lst,PyObject *seq,int cnt,int offs)
{
	for(int ix = 0; ix < cnt; ++ix) {
		PyObject *arg = PySequence_GetItem(seq,ix+offs); // new reference
        getone(lst[ix],arg);
        Py_DECREF(arg);
	}
    return flext::sym_list;
}

const t_symbol *pybase::GetPyArgs(AtomList &lst,PyObject *pValue,int offs)
{
	if(pValue == NULL) return false; 

    // output bang on None returned
    if(pValue == Py_None) return sym_bang;

	// analyze return value or tuple
    const t_symbol *sym = NULL;

	if(isseq(pValue)) {
        // Python might crash here if pValue is no "real" sequence, but rather e.g. an instance

        int rargc = PySequence_Size(pValue);

        if(rargc == 2) {
            // check if syntax is symbol/string, list -> anything message
            PyObject *s = PySequence_GetItem(pValue,0);
            PyObject *l = PySequence_GetItem(pValue,1);

            if(issym(s) && isseq(l)) {
                // is anything message
                rargc = PySequence_Size(l);
            	lst(offs+rargc);           
                getlist(lst.Atoms(),l,rargc);
                sym = pyObject_AsSymbol(s);
            }
            else {
                // (symbol,atom) list
            	lst(offs+rargc);           
	    	    sym = getlist(lst.Atoms(),pValue,rargc);
            }

            Py_DECREF(s);
            Py_DECREF(l);
        }
        else {
        	lst(offs+rargc);           
		    sym = getlist(lst.Atoms(),pValue,rargc);
        }
	}
    else {
        lst(offs+1);
        sym = getone(lst[offs],pValue);
	}

    return sym;
}


const t_symbol *pybase::GetPyAtom(AtomList &lst,PyObject *obj)
{
    size_t atom = PyAtom::Register(obj);
    size_t szat = sizeof(atom)/2;

    lst(szat);
    for(size_t i = 0; i < szat; ++i,atom >>= 16)
        flext::SetInt(lst[i],(int)(atom&((1<<16)-1)));
    return symatom;
}