@@ -119,6 +119,7 @@ float getNumpyF(PyArrayObject *obj, Py_ssize_t i) {
119119 return 0 ;
120120}
121121
122+ //The calling function needs to free the result
122123char * getNumpyStr (PyArrayObject * obj , Py_ssize_t i ) {
123124 char * p , * o = NULL ;
124125 npy_intp stride , j ;
@@ -137,7 +138,7 @@ char *getNumpyStr(PyArrayObject *obj, Py_ssize_t i) {
137138 return o ;
138139 case NPY_UNICODE :
139140 o = calloc (1 , stride /4 + 1 );
140- for (j = 0 ; j < stride /4 ; j ++ ) o [j ] = (char ) ((uint32_t * )p )[4 * j ];
141+ for (j = 0 ; j < stride /4 ; j ++ ) o [j ] = (char ) ((uint32_t * )p )[j ];
141142 return o ;
142143 default :
143144 PyErr_SetString (PyExc_RuntimeError , "Received unknown data type!\n" );
@@ -908,6 +909,7 @@ int canAppend(pyBigWigFile_t *self, int desiredType, PyObject *chroms, PyObject
908909 PyObject * tmp ;
909910#ifdef WITHNUMPY
910911 void * foo ;
912+ char * chrom ;
911913#endif
912914
913915 if (self -> lastType == -1 ) return 0 ;
@@ -925,10 +927,9 @@ int canAppend(pyBigWigFile_t *self, int desiredType, PyObject *chroms, PyObject
925927 for (i = 0 ; i < sz ; i ++ ) {
926928#ifdef WITHNUMPY
927929 if (PyArray_Check (chroms )) {
928- foo = PyArray_GETPTR1 ((PyArrayObject * )chroms , i );
929- tmp = PyArray_GETITEM ((PyArrayObject * )chroms , foo );
930- tid = bwGetTid (bw , PyString_AsString (tmp ));
931- Py_DECREF (tmp );
930+ chrom = getNumpyStr ((PyArrayObject * )chroms , i );
931+ tid = bwGetTid (bw , chrom );
932+ free (chrom );
932933 } else {
933934#endif
934935 tmp = PyList_GetItem (chroms , i );
@@ -1021,10 +1022,7 @@ int PyAddIntervals(pyBigWigFile_t *self, PyObject *chroms, PyObject *starts, PyO
10211022 cchroms [i ] = PyString_AsString (PyList_GetItem (chroms , i ));
10221023#ifdef WITHNUMPY
10231024 } else {
1024- foo = PyArray_GETPTR1 ((PyArrayObject * )chroms , i );
1025- tmp = PyArray_GETITEM ((PyArrayObject * )chroms , foo );
1026- cchroms [i ] = PyString_AsString (tmp );
1027- Py_DECREF (tmp );
1025+ cchroms [i ] = getNumpyStr ((PyArrayObject * )chroms , i );
10281026#endif
10291027 }
10301028 if (PyList_Check (starts )) {
@@ -1058,6 +1056,9 @@ int PyAddIntervals(pyBigWigFile_t *self, PyObject *chroms, PyObject *starts, PyO
10581056 self -> lastTid = bwGetTid (bw , cchroms [n - 1 ]);
10591057 self -> lastStart = uends [n - 1 ];
10601058 }
1059+ if (!PyList_Check (chroms )) {
1060+ for (i = 0 ; i < n ; i ++ ) free (cchroms [i ]);
1061+ }
10611062 free (cchroms );
10621063 free (ustarts );
10631064 free (uends );
0 commit comments