From 2df5932b6731bc85d00a032a9960c8b8000b495e Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Wed, 13 Dec 2017 22:27:36 +0100 Subject: [PATCH] Cython column API: fixed a memory leak, optimized the reading of elements names, added a __len__ method to Column_line, and the API for columns with character strings to evaluate --- python/obitools3/dms/column/column.pxd | 1 + python/obitools3/dms/column/column.pyx | 32 +++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/python/obitools3/dms/column/column.pxd b/python/obitools3/dms/column/column.pxd index 0e1e0b3..6af1f84 100644 --- a/python/obitools3/dms/column/column.pxd +++ b/python/obitools3/dms/column/column.pxd @@ -18,6 +18,7 @@ cdef class Column(OBIWrapper) : cdef View _view cdef bytes _alias + cdef list elements_names cdef inline OBIDMS_column_p pointer(self) diff --git a/python/obitools3/dms/column/column.pyx b/python/obitools3/dms/column/column.pyx index 62bd6df..2f80863 100644 --- a/python/obitools3/dms/column/column.pyx +++ b/python/obitools3/dms/column/column.pyx @@ -28,6 +28,8 @@ from obitools3.utils cimport tobytes, \ from obitools3.dms.column import typed_column +from libc.stdlib cimport free + import importlib import inspect import pkgutil @@ -75,6 +77,7 @@ cdef class Column(OBIWrapper) : index_t nb_elements_per_line=1, list elements_names=None, bint tuples=False, + bint to_eval=False, object comments=b"", object alias=b""): # TODO indexer_name? @@ -114,7 +117,9 @@ cdef class Column(OBIWrapper) : nb_lines = len(view), nb_elements_per_line = nb_elements_per_line, elements_names = elements_names_p, + elt_names_formatted = False, tuples = tuples, + to_eval = to_eval, indexer_name = NULL, associated_column_name = NULL, associated_column_version = -1, @@ -155,6 +160,7 @@ cdef class Column(OBIWrapper) : column._view = view column._alias = column_name_b + column.elements_names = column._elements_names view.register(column) return column @@ -184,7 +190,9 @@ cdef class Column(OBIWrapper) : nb_lines = -1, nb_elements_per_line = -1, elements_names = NULL, + elt_names_formatted = False, tuples = False, + to_eval = False, indexer_name = NULL, associated_column_name = NULL, associated_column_version = -1, @@ -280,10 +288,17 @@ cdef class Column(OBIWrapper) : # elements_names property getter @property - def elements_names(self): + def _elements_names(self): + cdef char* elts_names_b + cdef list elts_names_list if not self.active() : raise OBIDeactivatedInstanceError() - return obi_get_elements_names(self.pointer()).split(b';') + elts_names_b = obi_get_elements_names(self.pointer()) + if elts_names_b == NULL: + raise Exception("Error reading the elements names of a column") + elts_names_list = elts_names_b.split(b';') + free(elts_names_b) + return elts_names_list # nb_elements_per_line property getter @property @@ -334,6 +349,13 @@ cdef class Column(OBIWrapper) : raise OBIDeactivatedInstanceError() return self.pointer().header.tuples + # to_eval property getter + @property + def to_eval(self): + if not self.active() : + raise OBIDeactivatedInstanceError() + return self.pointer().header.to_eval + # comments property getter @property def comments(self): @@ -395,7 +417,11 @@ cdef class Column_line : return self._column.get_item(self._index, elt_id) else: return default - + + + def __len__(self): + return self._column.nb_elements_per_line + def __contains__(self, object elt_id): if type(elt_id) == int: