changeset 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 0c5a4dc04ed9
children 141d3bfe1503
files Makefile.linux native/FloatConversion.h native/PyPluginObject.cpp test/test_plugin_metadata.py test/test_process_block.py
diffstat 5 files changed, 67 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.linux	Tue Feb 10 12:08:22 2015 +0000
+++ b/Makefile.linux	Tue Feb 10 12:28:53 2015 +0000
@@ -1,6 +1,13 @@
 
 PY_INCLUDE_PATH		:= /usr/include/python2.7
 NUMPY_INCLUDE_PATH 	:= /usr/lib/python2.7/site-packages/numpy/core/include
+PY_LIB			:= python2.7
+PY_TEST			:= nosetests2
+
+#PY_INCLUDE_PATH		:= /usr/include/python3.4m
+#NUMPY_INCLUDE_PATH 	:= /usr/lib/python3.4m/site-packages/numpy/core/include
+#PY_LIB			:= python3
+#PY_TEST			:= nosetests3
 
 CFLAGS 			:= -O2 -Wall -Werror -fno-strict-aliasing -fPIC \
 			   -I$(PY_INCLUDE_PATH) -I$(NUMPY_INCLUDE_PATH)
@@ -8,9 +15,9 @@
 CXXFLAGS 		:= $(CFLAGS)
 
 LDFLAGS 		:= -shared -Wl,-Bstatic -lvamp-hostsdk -Wl,-Bdynamic \
-			   -Wl,-z,defs -lpython2.7 -ldl
+			   -Wl,-z,defs -l$(PY_LIB) -ldl
 
-NOSE			:= /usr/bin/nosetests2
+NOSE			:= $(PY_TEST)
 
 LIBRARY_EXT := .so
 
--- a/native/FloatConversion.h	Tue Feb 10 12:08:22 2015 +0000
+++ b/native/FloatConversion.h	Tue Feb 10 12:28:53 2015 +0000
@@ -48,26 +48,26 @@
 	if (pyValue && PyLong_Check(pyValue)) {
 	    return true;
 	}
+#if PY_MAJOR_VERSION < 3
 	if (pyValue && PyInt_Check(pyValue)) {
 	    return true;
 	}
+#endif
 	return false;
     }
 	
     static float convert(PyObject* pyValue) {
-	
 	if (pyValue && PyFloat_Check(pyValue)) {
 	    return (float) PyFloat_AS_DOUBLE(pyValue);
 	}
-
 	if (pyValue && PyLong_Check(pyValue)) {
 	    return (float) PyLong_AsDouble(pyValue);
 	}
-
+#if PY_MAJOR_VERSION < 3
 	if (pyValue && PyInt_Check(pyValue)) {
 	    return (float) PyInt_AsLong(pyValue);
 	}
-
+#endif
 	return 0.0;
     }
 };
--- a/native/PyPluginObject.cpp	Tue Feb 10 12:08:22 2015 +0000
+++ b/native/PyPluginObject.cpp	Tue Feb 10 12:28:53 2015 +0000
@@ -43,6 +43,10 @@
 #define NO_IMPORT_ARRAY
 #include "numpy/arrayobject.h"
 
+#if PY_MAJOR_VERSION < 3
+#include "intobject.h"
+#endif
+
 #include "structmember.h"
 
 #include "FloatConversion.h"
@@ -78,7 +82,22 @@
 PyObject *
 pystr(const string &s)
 {
+#if PY_MAJOR_VERSION < 3
     return PyString_FromString(s.c_str());
+#else
+    return PyUnicode_FromString(s.c_str());
+#endif
+}
+
+static
+string
+py2str(PyObject *obj)
+{
+#if PY_MAJOR_VERSION < 3
+    return PyString_AsString(obj);
+#else
+    return PyBytes_AsString(PyUnicode_AsUTF8String(obj));
+#endif
 }
 
 PyObject *
@@ -94,9 +113,9 @@
 
     PyObject *infodict = PyDict_New();
     PyDict_SetItemString
-        (infodict, "api_version", PyInt_FromLong(plugin->getVampApiVersion()));
+        (infodict, "api_version", PyLong_FromLong(plugin->getVampApiVersion()));
     PyDict_SetItemString
-        (infodict, "plugin_version", PyInt_FromLong(plugin->getPluginVersion()));
+        (infodict, "plugin_version", PyLong_FromLong(plugin->getPluginVersion()));
     PyDict_SetItemString
         (infodict, "identifier", pystr(plugin->getIdentifier()));
     PyDict_SetItemString
@@ -185,10 +204,10 @@
     PyDict_SetItemString
         (outdict, "unit", pystr(desc.unit));
     PyDict_SetItemString
-        (outdict, "has_fixed_bin_count", PyInt_FromLong(desc.hasFixedBinCount));
+        (outdict, "has_fixed_bin_count", PyLong_FromLong(desc.hasFixedBinCount));
     if (desc.hasFixedBinCount) {
         PyDict_SetItemString
-            (outdict, "bin_count", PyInt_FromLong(desc.binCount));
+            (outdict, "bin_count", PyLong_FromLong(desc.binCount));
         if (!desc.binNames.empty()) {
             PyDict_SetItemString
                 (outdict, "bin_names", conv.PyValue_From_StringVector(desc.binNames));
@@ -218,13 +237,13 @@
         }
     }
     PyDict_SetItemString
-        (outdict, "sample_type", PyInt_FromLong((int)desc.sampleType));
+        (outdict, "sample_type", PyLong_FromLong((int)desc.sampleType));
     PyDict_SetItemString
         (outdict, "sample_rate", PyFloat_FromDouble(desc.sampleRate));
     PyDict_SetItemString
         (outdict, "has_duration", desc.hasDuration ? Py_True : Py_False);
     PyDict_SetItemString
-        (outdict, "output_index", PyInt_FromLong(ix));
+        (outdict, "output_index", PyLong_FromLong(ix));
     return outdict;
 }
 
@@ -249,7 +268,7 @@
     Plugin::OutputList ol = pd->plugin->getOutputDescriptors();
 
     if (pyId) {
-        string id = PyString_AS_STRING(pyId);
+        string id = py2str(pyId);
         for (int i = 0; i < int(ol.size()); ++i) {
             if (ol[i].identifier == id) {
                 return convertOutput(ol[i], i);
@@ -261,7 +280,7 @@
         }
     }
 
-    PyErr_SetString(PyExc_StandardError,
+    PyErr_SetString(PyExc_Exception,
                     "unknown output id or output index out of range");
     return 0;
 }
@@ -322,7 +341,7 @@
     if (!pd) return 0;
 
     if (!pd->isInitialised) {
-        PyErr_SetString(PyExc_StandardError,
+        PyErr_SetString(PyExc_Exception,
                         "Plugin has not been initialised");
         return 0;
     }
@@ -356,10 +375,10 @@
     PyPluginObject *pd = getPluginObject(self);
     if (!pd) return 0;
 
-    string param = PyString_AS_STRING(pyParam);
+    string param = py2str(pyParam);
     
     if (!hasParameter(pd, param)) {
-        PyErr_SetString(PyExc_StandardError,
+        PyErr_SetString(PyExc_Exception,
                         (string("Unknown parameter id \"") + param + "\"").c_str());
         return 0;
     }
@@ -382,10 +401,10 @@
     PyPluginObject *pd = getPluginObject(self);
     if (!pd) return 0;
 
-    string param = PyString_AS_STRING(pyParam);
+    string param = py2str(pyParam);
     
     if (!hasParameter(pd, param)) {
-        PyErr_SetString(PyExc_StandardError,
+        PyErr_SetString(PyExc_Exception,
                         (string("Unknown parameter id \"") + param + "\"").c_str());
         return 0;
     }
@@ -421,7 +440,11 @@
     Py_ssize_t pos = 0;
     PyObject *key, *value;
     while (PyDict_Next(dict, &pos, &key, &value)) {
+#if PY_MAJOR_VERSION >= 3
+        if (!key || !PyUnicode_CheckExact(key)) {
+#else
         if (!key || !PyString_CheckExact(key)) {
+#endif
             PyErr_SetString(PyExc_TypeError,
                             "Parameter dict keys must all have string type");
             return 0;
@@ -431,9 +454,9 @@
                             "Parameter dict values must be convertible to float");
             return 0;
         }
-        string param = PyString_AS_STRING(key);
+        string param = py2str(key);
         if (paramIds.find(param) == paramIds.end()) {
-            PyErr_SetString(PyExc_StandardError,
+            PyErr_SetString(PyExc_Exception,
                             (string("Unknown parameter id \"") + param + "\"").c_str());
             return 0;
         }
@@ -456,7 +479,7 @@
     PyPluginObject *pd = getPluginObject(self);
     if (!pd) return 0;
 
-    pd->plugin->selectProgram(PyString_AS_STRING(pyParam));
+    pd->plugin->selectProgram(py2str(pyParam));
     return Py_True;
 }
 
@@ -503,7 +526,7 @@
                 PyList_SET_ITEM(pyFl, fli, pyF);
             }
 
-            PyObject *pyN = PyInt_FromLong(fno);
+            PyObject *pyN = PyLong_FromLong(fno);
             PyDict_SetItem(pyFs, pyN, pyFl);
         }
     }
@@ -588,7 +611,7 @@
     if (!pd) return 0;
 
     if (!pd->isInitialised) {
-        PyErr_SetString(PyExc_StandardError,
+        PyErr_SetString(PyExc_Exception,
                         "Plugin has not been initialised.");
         return 0;
     }
@@ -616,7 +639,7 @@
     if (!pd) return 0;
 
     if (!pd->isInitialised) {
-        PyErr_SetString(PyExc_StandardError,
+        PyErr_SetString(PyExc_Exception,
                         "Plugin has not been initialised.");
         return 0;
     }
@@ -631,7 +654,7 @@
 {
     PyPluginObject *pd = getPluginObject(self);
     if (!pd) return 0;
-    return PyInt_FromLong(pd->plugin->getPreferredBlockSize());
+    return PyLong_FromLong(pd->plugin->getPreferredBlockSize());
 }
 
 static PyObject *
@@ -639,7 +662,7 @@
 {
     PyPluginObject *pd = getPluginObject(self);
     if (!pd) return 0;
-    return PyInt_FromLong(pd->plugin->getPreferredStepSize());
+    return PyLong_FromLong(pd->plugin->getPreferredStepSize());
 }
 
 static PyObject *
@@ -647,7 +670,7 @@
 {
     PyPluginObject *pd = getPluginObject(self);
     if (!pd) return 0;
-    return PyInt_FromLong(pd->plugin->getMinChannelCount());
+    return PyLong_FromLong(pd->plugin->getMinChannelCount());
 }
 
 static PyObject *
@@ -655,7 +678,7 @@
 {
     PyPluginObject *pd = getPluginObject(self);
     if (!pd) return 0;
-    return PyInt_FromLong(pd->plugin->getMaxChannelCount());
+    return PyLong_FromLong(pd->plugin->getMaxChannelCount());
 }
     
 static PyObject *
--- a/test/test_plugin_metadata.py	Tue Feb 10 12:08:22 2015 +0000
+++ b/test/test_plugin_metadata.py	Tue Feb 10 12:28:53 2015 +0000
@@ -38,12 +38,12 @@
     try:
         out = plug.get_output("chops")
         assert False
-    except StandardError:
+    except Exception:
         pass
     try:
         out = plug.get_output("")
         assert False
-    except StandardError:
+    except Exception:
         pass
 
 def test_get_output_by_index():
@@ -55,17 +55,17 @@
     try:
         out = plug.get_output(20)
         assert False
-    except StandardError:
+    except Exception:
         pass
     try:
         out = plug.get_output(-1)
         assert False
-    except StandardError:
+    except Exception:
         pass
     try:
         out = plug.get_output(plug)
         assert False
-    except StandardError:
+    except Exception:
         pass
     
 def test_inputdomain():
@@ -102,7 +102,7 @@
     try:
         plug.set_parameter_value("steak", 0)
         assert False
-    except StandardError:
+    except Exception:
         pass
     try:
         plug.get_parameter_value(4)
@@ -112,6 +112,6 @@
     try:
         plug.get_parameter_value("steak")
         assert False
-    except StandardError:
+    except Exception:
         pass
             
--- a/test/test_process_block.py	Tue Feb 10 12:08:22 2015 +0000
+++ b/test/test_process_block.py	Tue Feb 10 12:28:53 2015 +0000
@@ -28,7 +28,7 @@
     try:
         plug.process_block([[1,2,3,4]], vh.RealTime(0, 0))
         assert False
-    except StandardError:
+    except Exception:
         pass
 
 def test_process_input_format():