aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source/pyargs.cpp
blob: 1b204273dd51a636770f6419a81dc624cbc990b0 (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
/* 

py/pyext - python external object for PD and MaxMSP

Copyright (c)2002-2004 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 "main.h"

static PyObject *MakePyAtom(const t_atom &at)
{
	if(flext::IsSymbol(at)) return PyString_FromString(flext::GetString(at));
//	else if(flext::IsPointer(at)) return NULL; // not handled
    else if(flext::CanbeInt(at) && flext::CanbeFloat(at)) {
        // if a number can be an integer... let at be an integer!
        int ival = flext::GetAInt(at);
        double fval = flext::GetAFloat(at);
        return (double)ival == fval?PyInt_FromLong(ival):PyFloat_FromDouble(fval);
    }
    // these following should never happen
    else if(flext::IsFloat(at)) return PyFloat_FromDouble((D)flext::GetFloat(at));
	else if(flext::IsInt(at)) return PyInt_FromLong(flext::GetInt(at));
  
    return NULL;
}

PyObject *py::MakePyArgs(const t_symbol *s,int argc,const t_atom *argv,I inlet,BL withself)
{
	PyObject *pArgs;

/*
    if(!s && args.Count() == 1) {
        pArgs = MakePyAtom(args[0]);
        if(!pArgs)
			post("py/pyext: cannot convert argument(s)");
    }
    else 
*/
    {
	    BL any = IsAnything(s);
	    pArgs = PyTuple_New(argc+(any?1:0)+(inlet >= 0?1:0));

	    I pix = 0;

	    if(inlet >= 0) {
		    PyObject *pValue = PyInt_FromLong(inlet); 
    		
		    // reference stolen:
		    PyTuple_SetItem(pArgs, pix++, pValue); 
	    }

	    I ix;
	    PyObject *tmp;
	    if(!withself || argc < (any?1:2)) tmp = pArgs,ix = pix;
	    else tmp = PyTuple_New(argc+(any?1:0)),ix = 0;

	    if(any) {
		    PyObject *pValue = PyString_FromString(GetString(s));

		    // reference stolen here: 
		    PyTuple_SetItem(tmp, ix++, pValue); 
	    }

	    for(I i = 0; i < argc; ++i) {
		    PyObject *pValue = MakePyAtom(argv[i]);
		    if(!pValue) {
			    post("py/pyext: cannot convert argument %i",any?i+1:i);
			    continue;
		    }

		    /* pValue reference stolen here: */
		    PyTuple_SetItem(tmp, ix++, pValue); 
	    }

	    if(tmp != pArgs) {
		    PyTuple_SetItem(pArgs, pix++, tmp); 
    #if PY_VERSION_HEX >= 0x02020000
		    _PyTuple_Resize(&pArgs,pix);
    #else
		    _PyTuple_Resize(&pArgs,pix,0);
    #endif
	    }
    }

	return pArgs;
}

flext::AtomList *py::GetPyArgs(PyObject *pValue,PyObject **self)
{
	if(pValue == NULL) return NULL; 
	AtomList *ret = NULL;

	// analyze return value or tuple

	I rargc = 0;
	BL ok = true;
	retval tp = nothing;

	if(PyString_Check(pValue)) {
		rargc = 1;
		tp = atom;
	}
	else if(PySequence_Check(pValue)) {
		rargc = PySequence_Size(pValue);
		tp = sequ;
	}
	else {
		rargc = 1;
		tp = atom;
	}

	ret = new AtomList(rargc);

	for(I ix = 0; ix < rargc; ++ix) {
		PyObject *arg;
		switch(tp) {
			case sequ: arg = PySequence_GetItem(pValue,ix); break;
			default: arg = pValue;
		}

		if(PyInt_Check(arg)) SetInt((*ret)[ix],PyInt_AsLong(arg));
		else if(PyLong_Check(arg)) SetInt((*ret)[ix],PyLong_AsLong(arg));
		else if(PyFloat_Check(arg)) SetFloat((*ret)[ix],(F)PyFloat_AsDouble(arg));
		else if(PyString_Check(arg)) SetString((*ret)[ix],PyString_AsString(arg));
		else if(ix == 0 && self && PyInstance_Check(arg)) {
			// assumed to be self ... that should be checked _somehow_ !!!
			*self = arg;
		}
		else {
			PyObject *tp = PyObject_Type(arg);
			PyObject *stp = tp?PyObject_Str(tp):NULL;
			C *tmp = "";
			if(stp) tmp = PyString_AsString(stp);
			post("py/pyext: Could not convert argument %s",tmp);
			Py_XDECREF(stp);
			Py_XDECREF(tp);
			ok = false;
		}
		// No DECREF for arg -> borrowed from pValue!
	}

	if(!ok) { 
		delete ret; 
		ret = NULL; 
	}
	return ret;
}