changeset 748:e5f96902afaf

Added wrappers for liszt, retrieve_datum, solved Py_XDECREF by copying source data and freeing retrieved memory
author mas01mc
date Mon, 22 Nov 2010 17:58:27 +0000
parents fbf16508421f
children dd4b9fec8d85
files bindings/python/pyadb.py bindings/python/pyadbmodule.c
diffstat 2 files changed, 36 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/bindings/python/pyadb.py	Sat Nov 20 15:32:58 2010 +0000
+++ b/bindings/python/pyadb.py	Mon Nov 22 17:58:27 2010 +0000
@@ -192,6 +192,26 @@
 		def __repr__(self):
 			return repr(self.rawData)
 
+	def liszt(self):
+		'''run _pyadb_liszt to get a list of database keys'''
+		if self._db != None:
+			return _pyadb._pyadb_liszt(self._db)
+		else:
+			print "Error in liszt(): ADB database not defined"
+			return 0
+
+	def retrieve_datum(self, key, **args):
+		'''run _pyadb_retrieveDatum to retrieve data by key:
+		      features=True, to get features
+		      powers=True, to get Powers
+		      times=True, to get Times
+		'''
+		if self._db != None:
+			return _pyadb._pyadb_retrieveDatum(self._db, key=key, **args)
+		else:
+			print "Error in liszt(): ADB database not defined"
+			return 0
+		
 class untitledTests(unittest.TestCase):
 	def setUp(self):
 		pass
--- a/bindings/python/pyadbmodule.c	Sat Nov 20 15:32:58 2010 +0000
+++ b/bindings/python/pyadbmodule.c	Mon Nov 22 17:58:27 2010 +0000
@@ -683,17 +683,26 @@
 	  data = ins->times;
 	}
 
-	outgoing = PyArray_SimpleNewFromData(dims, shape, NPY_DOUBLE, data);
-	free(status);
-	free(ins); // free the malloced adb_datum_t structure though
-
+	outgoing = PyArray_SimpleNew(dims, shape, NPY_DOUBLE);
 	if (!outgoing){
+	  free(status);
+	  free(ins); // free the malloced adb_datum_t structure though
+	  Py_XDECREF(outgoing);
 	  PyErr_SetString(PyExc_TypeError, "Failed to convert retrieved datum to C-Array");
 	  return NULL;
-	}
-	// Apprently Python automatically INCREFs the data pointer, so we don't have to call
-	// audiodb_free_datum(current_db, ins);
+	}	
 
+	/* Copy the data, this allows us to free the allocated memory and let
+	 * python do the subsequent garbage collection itself.
+	 */
+	int num_items = ins->nvectors * ins->dim;
+	double* p = (double*) PyArray_DATA(outgoing);
+	double* d = data;
+	while(num_items--)
+	  *p++ = *d++;	
+	audiodb_free_datum(current_db, ins); // free the source audiodb_datum
+	free(status); // free the malloced status object
+	free(ins); // free the malloced adb_datum_t structure though
 	return outgoing; 
 }