# HG changeset patch # User mas01mc # Date 1290448707 0 # Node ID e5f96902afaf7569837880bfa0f49c589b077e18 # Parent fbf16508421f566626d0c9f72567f25a079d39e8 Added wrappers for liszt, retrieve_datum, solved Py_XDECREF by copying source data and freeing retrieved memory diff -r fbf16508421f -r e5f96902afaf bindings/python/pyadb.py --- 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 diff -r fbf16508421f -r e5f96902afaf bindings/python/pyadbmodule.c --- 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; }