comparison native/PyPluginObject.cpp @ 112:9343eee50605

Update to Python 3. Currently crashes during tests (and also, two tests are now failing, even with Py2).
author Chris Cannam
date Wed, 17 Jun 2015 12:35:41 +0100
parents 4f590fc46ace
children 899095c8760f
comparison
equal deleted inserted replaced
111:4f590fc46ace 112:9343eee50605
49 49
50 #include "structmember.h" 50 #include "structmember.h"
51 51
52 #include "FloatConversion.h" 52 #include "FloatConversion.h"
53 #include "VectorConversion.h" 53 #include "VectorConversion.h"
54 #include "StringConversion.h"
54 #include "PyRealTime.h" 55 #include "PyRealTime.h"
55 56
56 #include <string> 57 #include <string>
57 #include <vector> 58 #include <vector>
58 #include <cstddef> 59 #include <cstddef>
74 "Invalid or already deleted plugin handle."); 75 "Invalid or already deleted plugin handle.");
75 return 0; 76 return 0;
76 } else { 77 } else {
77 return pd; 78 return pd;
78 } 79 }
79 }
80
81 static
82 PyObject *
83 pystr(const string &s)
84 {
85 #if PY_MAJOR_VERSION < 3
86 return PyString_FromString(s.c_str());
87 #else
88 return PyUnicode_FromString(s.c_str());
89 #endif
90 }
91
92 static
93 string
94 py2str(PyObject *obj)
95 {
96 #if PY_MAJOR_VERSION < 3
97 return PyString_AsString(obj);
98 #else
99 return PyBytes_AsString(PyUnicode_AsUTF8String(obj));
100 #endif
101 } 80 }
102 81
103 PyObject * 82 PyObject *
104 PyPluginObject_From_Plugin(Plugin *plugin) 83 PyPluginObject_From_Plugin(Plugin *plugin)
105 { 84 {
109 pd->isInitialised = false; 88 pd->isInitialised = false;
110 pd->channels = 0; 89 pd->channels = 0;
111 pd->blockSize = 0; 90 pd->blockSize = 0;
112 pd->stepSize = 0; 91 pd->stepSize = 0;
113 92
93 StringConversion strconv;
94
114 PyObject *infodict = PyDict_New(); 95 PyObject *infodict = PyDict_New();
115 PyDict_SetItemString 96 PyDict_SetItemString
116 (infodict, "apiVersion", PyLong_FromLong(plugin->getVampApiVersion())); 97 (infodict, "apiVersion", PyLong_FromLong(plugin->getVampApiVersion()));
117 PyDict_SetItemString 98 PyDict_SetItemString
118 (infodict, "pluginVersion", PyLong_FromLong(plugin->getPluginVersion())); 99 (infodict, "pluginVersion", PyLong_FromLong(plugin->getPluginVersion()));
119 PyDict_SetItemString 100 PyDict_SetItemString
120 (infodict, "identifier", pystr(plugin->getIdentifier())); 101 (infodict, "identifier", strconv.string2py(plugin->getIdentifier()));
121 PyDict_SetItemString 102 PyDict_SetItemString
122 (infodict, "name", pystr(plugin->getName())); 103 (infodict, "name", strconv.string2py(plugin->getName()));
123 PyDict_SetItemString 104 PyDict_SetItemString
124 (infodict, "description", pystr(plugin->getDescription())); 105 (infodict, "description", strconv.string2py(plugin->getDescription()));
125 PyDict_SetItemString 106 PyDict_SetItemString
126 (infodict, "maker", pystr(plugin->getMaker())); 107 (infodict, "maker", strconv.string2py(plugin->getMaker()));
127 PyDict_SetItemString 108 PyDict_SetItemString
128 (infodict, "copyright", pystr(plugin->getCopyright())); 109 (infodict, "copyright", strconv.string2py(plugin->getCopyright()));
129 pd->info = infodict; 110 pd->info = infodict;
130 111
131 pd->inputDomain = plugin->getInputDomain(); 112 pd->inputDomain = plugin->getInputDomain();
132 113
133 VectorConversion conv; 114 VectorConversion conv;
136 PyObject *params = PyList_New(pl.size()); 117 PyObject *params = PyList_New(pl.size());
137 118
138 for (int i = 0; i < (int)pl.size(); ++i) { 119 for (int i = 0; i < (int)pl.size(); ++i) {
139 PyObject *paramdict = PyDict_New(); 120 PyObject *paramdict = PyDict_New();
140 PyDict_SetItemString 121 PyDict_SetItemString
141 (paramdict, "identifier", pystr(pl[i].identifier)); 122 (paramdict, "identifier", strconv.string2py(pl[i].identifier));
142 PyDict_SetItemString 123 PyDict_SetItemString
143 (paramdict, "name", pystr(pl[i].name)); 124 (paramdict, "name", strconv.string2py(pl[i].name));
144 PyDict_SetItemString 125 PyDict_SetItemString
145 (paramdict, "description", pystr(pl[i].description)); 126 (paramdict, "description", strconv.string2py(pl[i].description));
146 PyDict_SetItemString 127 PyDict_SetItemString
147 (paramdict, "unit", pystr(pl[i].unit)); 128 (paramdict, "unit", strconv.string2py(pl[i].unit));
148 PyDict_SetItemString 129 PyDict_SetItemString
149 (paramdict, "minValue", PyFloat_FromDouble(pl[i].minValue)); 130 (paramdict, "minValue", PyFloat_FromDouble(pl[i].minValue));
150 PyDict_SetItemString 131 PyDict_SetItemString
151 (paramdict, "maxValue", PyFloat_FromDouble(pl[i].maxValue)); 132 (paramdict, "maxValue", PyFloat_FromDouble(pl[i].maxValue));
152 PyDict_SetItemString 133 PyDict_SetItemString
172 153
173 Plugin::ProgramList prl = plugin->getPrograms(); 154 Plugin::ProgramList prl = plugin->getPrograms();
174 PyObject *progs = PyList_New(prl.size()); 155 PyObject *progs = PyList_New(prl.size());
175 156
176 for (int i = 0; i < (int)prl.size(); ++i) { 157 for (int i = 0; i < (int)prl.size(); ++i) {
177 PyList_SET_ITEM(progs, i, pystr(prl[i])); 158 PyList_SET_ITEM(progs, i, strconv.string2py(prl[i]));
178 } 159 }
179 160
180 pd->programs = progs; 161 pd->programs = progs;
181 162
182 return (PyObject *)pd; 163 return (PyObject *)pd;
191 172
192 static PyObject * 173 static PyObject *
193 convertOutput(const Plugin::OutputDescriptor &desc, int ix) 174 convertOutput(const Plugin::OutputDescriptor &desc, int ix)
194 { 175 {
195 VectorConversion conv; 176 VectorConversion conv;
177 StringConversion strconv;
196 178
197 PyObject *outdict = PyDict_New(); 179 PyObject *outdict = PyDict_New();
198 PyDict_SetItemString 180 PyDict_SetItemString
199 (outdict, "identifier", pystr(desc.identifier)); 181 (outdict, "identifier", strconv.string2py(desc.identifier));
200 PyDict_SetItemString 182 PyDict_SetItemString
201 (outdict, "name", pystr(desc.name)); 183 (outdict, "name", strconv.string2py(desc.name));
202 PyDict_SetItemString 184 PyDict_SetItemString
203 (outdict, "description", pystr(desc.description)); 185 (outdict, "description", strconv.string2py(desc.description));
204 PyDict_SetItemString 186 PyDict_SetItemString
205 (outdict, "unit", pystr(desc.unit)); 187 (outdict, "unit", strconv.string2py(desc.unit));
206 PyDict_SetItemString 188 PyDict_SetItemString
207 (outdict, "hasFixedBinCount", PyLong_FromLong(desc.hasFixedBinCount)); 189 (outdict, "hasFixedBinCount", PyLong_FromLong(desc.hasFixedBinCount));
208 if (desc.hasFixedBinCount) { 190 if (desc.hasFixedBinCount) {
209 PyDict_SetItemString 191 PyDict_SetItemString
210 (outdict, "binCount", PyLong_FromLong(desc.binCount)); 192 (outdict, "binCount", PyLong_FromLong(desc.binCount));
251 get_output(PyObject *self, PyObject *args) 233 get_output(PyObject *self, PyObject *args)
252 { 234 {
253 PyPluginObject *pd = getPluginObject(self); 235 PyPluginObject *pd = getPluginObject(self);
254 if (!pd) return 0; 236 if (!pd) return 0;
255 237
256 int n = -1; 238 ssize_t n = -1;
257 PyObject *pyId = 0; 239 PyObject *pyId = 0;
258 240
259 if (!PyArg_ParseTuple(args, "n", &n) && 241 if (!PyArg_ParseTuple(args, "n", &n) &&
260 !PyArg_ParseTuple(args, "S", &pyId)) { 242 !PyArg_ParseTuple(args,
243 #if (PY_MAJOR_VERSION >= 3)
244 "U",
245 #else
246 "S",
247 #endif
248 &pyId)) {
261 PyErr_SetString(PyExc_TypeError, 249 PyErr_SetString(PyExc_TypeError,
262 "get_output takes either output id (string) or output index (int) argument"); 250 "get_output takes either output id (string) or output index (int) argument");
263 return 0; 251 return 0;
264 } 252 }
265 253
266 PyErr_Clear(); 254 PyErr_Clear();
267 255
268 Plugin::OutputList ol = pd->plugin->getOutputDescriptors(); 256 Plugin::OutputList ol = pd->plugin->getOutputDescriptors();
269 257
258 StringConversion strconv;
259
270 if (pyId) { 260 if (pyId) {
271 string id = py2str(pyId); 261 string id = strconv.py2string(pyId);
272 for (int i = 0; i < int(ol.size()); ++i) { 262 for (int i = 0; i < int(ol.size()); ++i) {
273 if (ol[i].identifier == id) { 263 if (ol[i].identifier == id) {
274 return convertOutput(ol[i], i); 264 return convertOutput(ol[i], i);
275 } 265 }
276 } 266 }
302 } 292 }
303 293
304 static PyObject * 294 static PyObject *
305 initialise(PyObject *self, PyObject *args) 295 initialise(PyObject *self, PyObject *args)
306 { 296 {
307 size_t channels, blockSize, stepSize; 297 ssize_t channels, blockSize, stepSize;
308 298
309 if (!PyArg_ParseTuple (args, "nnn", 299 if (!PyArg_ParseTuple (args, "nnn",
310 (size_t) &channels, 300 &channels,
311 (size_t) &stepSize, 301 &stepSize,
312 (size_t) &blockSize)) { 302 &blockSize)) {
313 PyErr_SetString(PyExc_TypeError, 303 PyErr_SetString(PyExc_TypeError,
314 "initialise() takes channel count, step size, and block size arguments"); 304 "initialise() takes channel count, step size, and block size arguments");
315 return 0; 305 return 0;
316 } 306 }
317 307
365 static PyObject * 355 static PyObject *
366 get_parameter_value(PyObject *self, PyObject *args) 356 get_parameter_value(PyObject *self, PyObject *args)
367 { 357 {
368 PyObject *pyParam; 358 PyObject *pyParam;
369 359
370 if (!PyArg_ParseTuple(args, "S", &pyParam)) { 360 if (!PyArg_ParseTuple(args,
361 #if (PY_MAJOR_VERSION >= 3)
362 "U",
363 #else
364 "S",
365 #endif
366 &pyParam)) {
371 PyErr_SetString(PyExc_TypeError, 367 PyErr_SetString(PyExc_TypeError,
372 "get_parameter_value() takes parameter id (string) argument"); 368 "get_parameter_value() takes parameter id (string) argument");
373 return 0; } 369 return 0; }
374 370
375 PyPluginObject *pd = getPluginObject(self); 371 PyPluginObject *pd = getPluginObject(self);
376 if (!pd) return 0; 372 if (!pd) return 0;
377 373
378 string param = py2str(pyParam); 374 StringConversion strconv;
375
376 string param = strconv.py2string(pyParam);
379 377
380 if (!hasParameter(pd, param)) { 378 if (!hasParameter(pd, param)) {
381 PyErr_SetString(PyExc_Exception, 379 PyErr_SetString(PyExc_Exception,
382 (string("Unknown parameter id \"") + param + "\"").c_str()); 380 (string("Unknown parameter id \"") + param + "\"").c_str());
383 return 0; 381 return 0;
391 set_parameter_value(PyObject *self, PyObject *args) 389 set_parameter_value(PyObject *self, PyObject *args)
392 { 390 {
393 PyObject *pyParam; 391 PyObject *pyParam;
394 float value; 392 float value;
395 393
396 if (!PyArg_ParseTuple(args, "Sf", &pyParam, &value)) { 394 if (!PyArg_ParseTuple(args,
395 #if (PY_MAJOR_VERSION >= 3)
396 "Uf",
397 #else
398 "Sf",
399 #endif
400 &pyParam, &value)) {
397 PyErr_SetString(PyExc_TypeError, 401 PyErr_SetString(PyExc_TypeError,
398 "set_parameter_value() takes parameter id (string), and value (float) arguments"); 402 "set_parameter_value() takes parameter id (string), and value (float) arguments");
399 return 0; } 403 return 0; }
400 404
401 PyPluginObject *pd = getPluginObject(self); 405 PyPluginObject *pd = getPluginObject(self);
402 if (!pd) return 0; 406 if (!pd) return 0;
403 407
404 string param = py2str(pyParam); 408 StringConversion strconv;
409
410 string param = strconv.py2string(pyParam);
405 411
406 if (!hasParameter(pd, param)) { 412 if (!hasParameter(pd, param)) {
407 PyErr_SetString(PyExc_Exception, 413 PyErr_SetString(PyExc_Exception,
408 (string("Unknown parameter id \"") + param + "\"").c_str()); 414 (string("Unknown parameter id \"") + param + "\"").c_str());
409 return 0; 415 return 0;
452 if (!value || !FloatConversion::check(value)) { 458 if (!value || !FloatConversion::check(value)) {
453 PyErr_SetString(PyExc_TypeError, 459 PyErr_SetString(PyExc_TypeError,
454 "Parameter dict values must be convertible to float"); 460 "Parameter dict values must be convertible to float");
455 return 0; 461 return 0;
456 } 462 }
457 string param = py2str(key); 463 StringConversion strconv;
464 string param = strconv.py2string(key);
458 if (paramIds.find(param) == paramIds.end()) { 465 if (paramIds.find(param) == paramIds.end()) {
459 PyErr_SetString(PyExc_Exception, 466 PyErr_SetString(PyExc_Exception,
460 (string("Unknown parameter id \"") + param + "\"").c_str()); 467 (string("Unknown parameter id \"") + param + "\"").c_str());
461 return 0; 468 return 0;
462 } 469 }
469 static PyObject * 476 static PyObject *
470 select_program(PyObject *self, PyObject *args) 477 select_program(PyObject *self, PyObject *args)
471 { 478 {
472 PyObject *pyParam; 479 PyObject *pyParam;
473 480
474 if (!PyArg_ParseTuple(args, "S", &pyParam)) { 481 if (!PyArg_ParseTuple(args,
482 #if (PY_MAJOR_VERSION >= 3)
483 "U",
484 #else
485 "S",
486 #endif
487 &pyParam)) {
475 PyErr_SetString(PyExc_TypeError, 488 PyErr_SetString(PyExc_TypeError,
476 "select_program() takes parameter id (string) argument"); 489 "select_program() takes parameter id (string) argument");
477 return 0; } 490 return 0; }
478 491
479 PyPluginObject *pd = getPluginObject(self); 492 PyPluginObject *pd = getPluginObject(self);
480 if (!pd) return 0; 493 if (!pd) return 0;
481 494
482 pd->plugin->selectProgram(py2str(pyParam)); 495 StringConversion strconv;
496
497 pd->plugin->selectProgram(strconv.py2string(pyParam));
483 return Py_True; 498 return Py_True;
484 } 499 }
485 500
486 static 501 static
487 PyObject * 502 PyObject *
513 if (f.hasDuration) { 528 if (f.hasDuration) {
514 PyDict_SetItemString 529 PyDict_SetItemString
515 (pyF, "duration", PyRealTime_FromRealTime(f.duration)); 530 (pyF, "duration", PyRealTime_FromRealTime(f.duration));
516 } 531 }
517 532
533 StringConversion strconv;
534
518 PyDict_SetItemString 535 PyDict_SetItemString
519 (pyF, "label", pystr(f.label)); 536 (pyF, "label", strconv.string2py(f.label));
520 537
521 if (!f.values.empty()) { 538 if (!f.values.empty()) {
522 PyDict_SetItemString 539 PyDict_SetItemString
523 (pyF, "values", conv.PyArray_From_FloatVector(f.values)); 540 (pyF, "values", conv.PyArray_From_FloatVector(f.values));
524 } 541 }
762 }; 779 };
763 780
764 /* Doc:: 10.3 Type Objects */ /* static */ 781 /* Doc:: 10.3 Type Objects */ /* static */
765 PyTypeObject Plugin_Type = 782 PyTypeObject Plugin_Type =
766 { 783 {
767 PyObject_HEAD_INIT(NULL) 784 PyVarObject_HEAD_INIT(NULL, 0)
768 0, /*ob_size*/
769 "vampyhost.Plugin", /*tp_name*/ 785 "vampyhost.Plugin", /*tp_name*/
770 sizeof(PyPluginObject), /*tp_basicsize*/ 786 sizeof(PyPluginObject), /*tp_basicsize*/
771 0, /*tp_itemsize*/ 787 0, /*tp_itemsize*/
772 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/ 788 (destructor)PyPluginObject_dealloc, /*tp_dealloc*/
773 0, /*tp_print*/ 789 0, /*tp_print*/