comparison native/PyPluginObject.cpp @ 102:216ed5a72c36

Some preliminary changes toward py2/3 compatibility (but the module load stuff remains quite a big deal)
author Chris Cannam
date Tue, 10 Feb 2015 12:28:53 +0000
parents 4bed6bf67243
children cf56111935fa
comparison
equal deleted inserted replaced
101:0c5a4dc04ed9 102:216ed5a72c36
41 #define PY_ARRAY_UNIQUE_SYMBOL VAMPYHOST_ARRAY_API 41 #define PY_ARRAY_UNIQUE_SYMBOL VAMPYHOST_ARRAY_API
42 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 42 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
43 #define NO_IMPORT_ARRAY 43 #define NO_IMPORT_ARRAY
44 #include "numpy/arrayobject.h" 44 #include "numpy/arrayobject.h"
45 45
46 #if PY_MAJOR_VERSION < 3
47 #include "intobject.h"
48 #endif
49
46 #include "structmember.h" 50 #include "structmember.h"
47 51
48 #include "FloatConversion.h" 52 #include "FloatConversion.h"
49 #include "VectorConversion.h" 53 #include "VectorConversion.h"
50 #include "PyRealTime.h" 54 #include "PyRealTime.h"
76 80
77 static 81 static
78 PyObject * 82 PyObject *
79 pystr(const string &s) 83 pystr(const string &s)
80 { 84 {
85 #if PY_MAJOR_VERSION < 3
81 return PyString_FromString(s.c_str()); 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
82 } 101 }
83 102
84 PyObject * 103 PyObject *
85 PyPluginObject_From_Plugin(Plugin *plugin) 104 PyPluginObject_From_Plugin(Plugin *plugin)
86 { 105 {
92 pd->blockSize = 0; 111 pd->blockSize = 0;
93 pd->stepSize = 0; 112 pd->stepSize = 0;
94 113
95 PyObject *infodict = PyDict_New(); 114 PyObject *infodict = PyDict_New();
96 PyDict_SetItemString 115 PyDict_SetItemString
97 (infodict, "api_version", PyInt_FromLong(plugin->getVampApiVersion())); 116 (infodict, "api_version", PyLong_FromLong(plugin->getVampApiVersion()));
98 PyDict_SetItemString 117 PyDict_SetItemString
99 (infodict, "plugin_version", PyInt_FromLong(plugin->getPluginVersion())); 118 (infodict, "plugin_version", PyLong_FromLong(plugin->getPluginVersion()));
100 PyDict_SetItemString 119 PyDict_SetItemString
101 (infodict, "identifier", pystr(plugin->getIdentifier())); 120 (infodict, "identifier", pystr(plugin->getIdentifier()));
102 PyDict_SetItemString 121 PyDict_SetItemString
103 (infodict, "name", pystr(plugin->getName())); 122 (infodict, "name", pystr(plugin->getName()));
104 PyDict_SetItemString 123 PyDict_SetItemString
183 PyDict_SetItemString 202 PyDict_SetItemString
184 (outdict, "description", pystr(desc.description)); 203 (outdict, "description", pystr(desc.description));
185 PyDict_SetItemString 204 PyDict_SetItemString
186 (outdict, "unit", pystr(desc.unit)); 205 (outdict, "unit", pystr(desc.unit));
187 PyDict_SetItemString 206 PyDict_SetItemString
188 (outdict, "has_fixed_bin_count", PyInt_FromLong(desc.hasFixedBinCount)); 207 (outdict, "has_fixed_bin_count", PyLong_FromLong(desc.hasFixedBinCount));
189 if (desc.hasFixedBinCount) { 208 if (desc.hasFixedBinCount) {
190 PyDict_SetItemString 209 PyDict_SetItemString
191 (outdict, "bin_count", PyInt_FromLong(desc.binCount)); 210 (outdict, "bin_count", PyLong_FromLong(desc.binCount));
192 if (!desc.binNames.empty()) { 211 if (!desc.binNames.empty()) {
193 PyDict_SetItemString 212 PyDict_SetItemString
194 (outdict, "bin_names", conv.PyValue_From_StringVector(desc.binNames)); 213 (outdict, "bin_names", conv.PyValue_From_StringVector(desc.binNames));
195 } 214 }
196 } 215 }
216 PyDict_SetItemString 235 PyDict_SetItemString
217 (outdict, "is_quantized", Py_False); 236 (outdict, "is_quantized", Py_False);
218 } 237 }
219 } 238 }
220 PyDict_SetItemString 239 PyDict_SetItemString
221 (outdict, "sample_type", PyInt_FromLong((int)desc.sampleType)); 240 (outdict, "sample_type", PyLong_FromLong((int)desc.sampleType));
222 PyDict_SetItemString 241 PyDict_SetItemString
223 (outdict, "sample_rate", PyFloat_FromDouble(desc.sampleRate)); 242 (outdict, "sample_rate", PyFloat_FromDouble(desc.sampleRate));
224 PyDict_SetItemString 243 PyDict_SetItemString
225 (outdict, "has_duration", desc.hasDuration ? Py_True : Py_False); 244 (outdict, "has_duration", desc.hasDuration ? Py_True : Py_False);
226 PyDict_SetItemString 245 PyDict_SetItemString
227 (outdict, "output_index", PyInt_FromLong(ix)); 246 (outdict, "output_index", PyLong_FromLong(ix));
228 return outdict; 247 return outdict;
229 } 248 }
230 249
231 static PyObject * 250 static PyObject *
232 get_output(PyObject *self, PyObject *args) 251 get_output(PyObject *self, PyObject *args)
247 PyErr_Clear(); 266 PyErr_Clear();
248 267
249 Plugin::OutputList ol = pd->plugin->getOutputDescriptors(); 268 Plugin::OutputList ol = pd->plugin->getOutputDescriptors();
250 269
251 if (pyId) { 270 if (pyId) {
252 string id = PyString_AS_STRING(pyId); 271 string id = py2str(pyId);
253 for (int i = 0; i < int(ol.size()); ++i) { 272 for (int i = 0; i < int(ol.size()); ++i) {
254 if (ol[i].identifier == id) { 273 if (ol[i].identifier == id) {
255 return convertOutput(ol[i], i); 274 return convertOutput(ol[i], i);
256 } 275 }
257 } 276 }
259 if (n >= 0 && n < int(ol.size())) { 278 if (n >= 0 && n < int(ol.size())) {
260 return convertOutput(ol[n], n); 279 return convertOutput(ol[n], n);
261 } 280 }
262 } 281 }
263 282
264 PyErr_SetString(PyExc_StandardError, 283 PyErr_SetString(PyExc_Exception,
265 "unknown output id or output index out of range"); 284 "unknown output id or output index out of range");
266 return 0; 285 return 0;
267 } 286 }
268 287
269 static PyObject * 288 static PyObject *
320 { 339 {
321 PyPluginObject *pd = getPluginObject(self); 340 PyPluginObject *pd = getPluginObject(self);
322 if (!pd) return 0; 341 if (!pd) return 0;
323 342
324 if (!pd->isInitialised) { 343 if (!pd->isInitialised) {
325 PyErr_SetString(PyExc_StandardError, 344 PyErr_SetString(PyExc_Exception,
326 "Plugin has not been initialised"); 345 "Plugin has not been initialised");
327 return 0; 346 return 0;
328 } 347 }
329 348
330 pd->plugin->reset(); 349 pd->plugin->reset();
354 return 0; } 373 return 0; }
355 374
356 PyPluginObject *pd = getPluginObject(self); 375 PyPluginObject *pd = getPluginObject(self);
357 if (!pd) return 0; 376 if (!pd) return 0;
358 377
359 string param = PyString_AS_STRING(pyParam); 378 string param = py2str(pyParam);
360 379
361 if (!hasParameter(pd, param)) { 380 if (!hasParameter(pd, param)) {
362 PyErr_SetString(PyExc_StandardError, 381 PyErr_SetString(PyExc_Exception,
363 (string("Unknown parameter id \"") + param + "\"").c_str()); 382 (string("Unknown parameter id \"") + param + "\"").c_str());
364 return 0; 383 return 0;
365 } 384 }
366 385
367 float value = pd->plugin->getParameter(param); 386 float value = pd->plugin->getParameter(param);
380 return 0; } 399 return 0; }
381 400
382 PyPluginObject *pd = getPluginObject(self); 401 PyPluginObject *pd = getPluginObject(self);
383 if (!pd) return 0; 402 if (!pd) return 0;
384 403
385 string param = PyString_AS_STRING(pyParam); 404 string param = py2str(pyParam);
386 405
387 if (!hasParameter(pd, param)) { 406 if (!hasParameter(pd, param)) {
388 PyErr_SetString(PyExc_StandardError, 407 PyErr_SetString(PyExc_Exception,
389 (string("Unknown parameter id \"") + param + "\"").c_str()); 408 (string("Unknown parameter id \"") + param + "\"").c_str());
390 return 0; 409 return 0;
391 } 410 }
392 411
393 pd->plugin->setParameter(param, value); 412 pd->plugin->setParameter(param, value);
419 } 438 }
420 439
421 Py_ssize_t pos = 0; 440 Py_ssize_t pos = 0;
422 PyObject *key, *value; 441 PyObject *key, *value;
423 while (PyDict_Next(dict, &pos, &key, &value)) { 442 while (PyDict_Next(dict, &pos, &key, &value)) {
443 #if PY_MAJOR_VERSION >= 3
444 if (!key || !PyUnicode_CheckExact(key)) {
445 #else
424 if (!key || !PyString_CheckExact(key)) { 446 if (!key || !PyString_CheckExact(key)) {
447 #endif
425 PyErr_SetString(PyExc_TypeError, 448 PyErr_SetString(PyExc_TypeError,
426 "Parameter dict keys must all have string type"); 449 "Parameter dict keys must all have string type");
427 return 0; 450 return 0;
428 } 451 }
429 if (!value || !FloatConversion::check(value)) { 452 if (!value || !FloatConversion::check(value)) {
430 PyErr_SetString(PyExc_TypeError, 453 PyErr_SetString(PyExc_TypeError,
431 "Parameter dict values must be convertible to float"); 454 "Parameter dict values must be convertible to float");
432 return 0; 455 return 0;
433 } 456 }
434 string param = PyString_AS_STRING(key); 457 string param = py2str(key);
435 if (paramIds.find(param) == paramIds.end()) { 458 if (paramIds.find(param) == paramIds.end()) {
436 PyErr_SetString(PyExc_StandardError, 459 PyErr_SetString(PyExc_Exception,
437 (string("Unknown parameter id \"") + param + "\"").c_str()); 460 (string("Unknown parameter id \"") + param + "\"").c_str());
438 return 0; 461 return 0;
439 } 462 }
440 pd->plugin->setParameter(param, FloatConversion::convert(value)); 463 pd->plugin->setParameter(param, FloatConversion::convert(value));
441 } 464 }
454 return 0; } 477 return 0; }
455 478
456 PyPluginObject *pd = getPluginObject(self); 479 PyPluginObject *pd = getPluginObject(self);
457 if (!pd) return 0; 480 if (!pd) return 0;
458 481
459 pd->plugin->selectProgram(PyString_AS_STRING(pyParam)); 482 pd->plugin->selectProgram(py2str(pyParam));
460 return Py_True; 483 return Py_True;
461 } 484 }
462 485
463 static 486 static
464 PyObject * 487 PyObject *
501 } 524 }
502 525
503 PyList_SET_ITEM(pyFl, fli, pyF); 526 PyList_SET_ITEM(pyFl, fli, pyF);
504 } 527 }
505 528
506 PyObject *pyN = PyInt_FromLong(fno); 529 PyObject *pyN = PyLong_FromLong(fno);
507 PyDict_SetItem(pyFs, pyN, pyFl); 530 PyDict_SetItem(pyFs, pyN, pyFl);
508 } 531 }
509 } 532 }
510 533
511 return pyFs; 534 return pyFs;
586 609
587 PyPluginObject *pd = getPluginObject(self); 610 PyPluginObject *pd = getPluginObject(self);
588 if (!pd) return 0; 611 if (!pd) return 0;
589 612
590 if (!pd->isInitialised) { 613 if (!pd->isInitialised) {
591 PyErr_SetString(PyExc_StandardError, 614 PyErr_SetString(PyExc_Exception,
592 "Plugin has not been initialised."); 615 "Plugin has not been initialised.");
593 return 0; 616 return 0;
594 } 617 }
595 618
596 int channels = pd->channels; 619 int channels = pd->channels;
614 { 637 {
615 PyPluginObject *pd = getPluginObject(self); 638 PyPluginObject *pd = getPluginObject(self);
616 if (!pd) return 0; 639 if (!pd) return 0;
617 640
618 if (!pd->isInitialised) { 641 if (!pd->isInitialised) {
619 PyErr_SetString(PyExc_StandardError, 642 PyErr_SetString(PyExc_Exception,
620 "Plugin has not been initialised."); 643 "Plugin has not been initialised.");
621 return 0; 644 return 0;
622 } 645 }
623 646
624 Plugin::FeatureSet fs = pd->plugin->getRemainingFeatures(); 647 Plugin::FeatureSet fs = pd->plugin->getRemainingFeatures();
629 static PyObject * 652 static PyObject *
630 get_preferred_block_size(PyObject *self, PyObject *) 653 get_preferred_block_size(PyObject *self, PyObject *)
631 { 654 {
632 PyPluginObject *pd = getPluginObject(self); 655 PyPluginObject *pd = getPluginObject(self);
633 if (!pd) return 0; 656 if (!pd) return 0;
634 return PyInt_FromLong(pd->plugin->getPreferredBlockSize()); 657 return PyLong_FromLong(pd->plugin->getPreferredBlockSize());
635 } 658 }
636 659
637 static PyObject * 660 static PyObject *
638 get_preferred_step_size(PyObject *self, PyObject *) 661 get_preferred_step_size(PyObject *self, PyObject *)
639 { 662 {
640 PyPluginObject *pd = getPluginObject(self); 663 PyPluginObject *pd = getPluginObject(self);
641 if (!pd) return 0; 664 if (!pd) return 0;
642 return PyInt_FromLong(pd->plugin->getPreferredStepSize()); 665 return PyLong_FromLong(pd->plugin->getPreferredStepSize());
643 } 666 }
644 667
645 static PyObject * 668 static PyObject *
646 get_min_channel_count(PyObject *self, PyObject *) 669 get_min_channel_count(PyObject *self, PyObject *)
647 { 670 {
648 PyPluginObject *pd = getPluginObject(self); 671 PyPluginObject *pd = getPluginObject(self);
649 if (!pd) return 0; 672 if (!pd) return 0;
650 return PyInt_FromLong(pd->plugin->getMinChannelCount()); 673 return PyLong_FromLong(pd->plugin->getMinChannelCount());
651 } 674 }
652 675
653 static PyObject * 676 static PyObject *
654 get_max_channel_count(PyObject *self, PyObject *) 677 get_max_channel_count(PyObject *self, PyObject *)
655 { 678 {
656 PyPluginObject *pd = getPluginObject(self); 679 PyPluginObject *pd = getPluginObject(self);
657 if (!pd) return 0; 680 if (!pd) return 0;
658 return PyInt_FromLong(pd->plugin->getMaxChannelCount()); 681 return PyLong_FromLong(pd->plugin->getMaxChannelCount());
659 } 682 }
660 683
661 static PyObject * 684 static PyObject *
662 unload(PyObject *self, PyObject *) 685 unload(PyObject *self, PyObject *)
663 { 686 {