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
This commit is contained in:
Celine Mercier
2017-12-13 22:27:36 +01:00
parent b93b982a18
commit 2df5932b67
2 changed files with 30 additions and 3 deletions

View File

@ -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)

View File

@ -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: