From 693859eec29bba257ba864ed60a446f0bc665ad4 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Fri, 14 Apr 2017 16:07:23 +0200 Subject: [PATCH] Cython API: fixed conversion bugs when setting and getting values (especially NA values) in OBI_CHAR, OBI_STR and OBI_SEQ columns --- .../dms/column/typed_column/char.pyx | 24 ++++++++++------- .../obitools3/dms/column/typed_column/seq.pyx | 22 ++++++++++----- .../obitools3/dms/column/typed_column/str.pyx | 27 +++++++++++++------ 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/python/obitools3/dms/column/typed_column/char.pyx b/python/obitools3/dms/column/typed_column/char.pyx index 8c08f67..9611c49 100644 --- a/python/obitools3/dms/column/typed_column/char.pyx +++ b/python/obitools3/dms/column/typed_column/char.pyx @@ -31,7 +31,6 @@ cdef class Column_char(Column): elements_names=elements_names, comments=comments) - cpdef object get_line(self, index_t line_nb): cdef obichar_t value cdef object result @@ -43,14 +42,16 @@ cdef class Column_char(Column): if value == OBIChar_NA : result = None else : - result = value # TODO return bytes or str? + result = value # TODO return bytes or str? return result - cpdef set_line(self, index_t line_nb, object value): + cdef obichar_t value_b if value is None : - value = OBIChar_NA - if obi_set_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, tobytes(value)[0]) < 0: + value_b = OBIChar_NA + else : + value_b = tobytes(value)[0] + if obi_set_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value_b) < 0: raise Exception("Problem setting a value in a column") @@ -70,7 +71,7 @@ cdef class Column_multi_elts_char(Column_multi_elts): if value == OBIChar_NA : result = None else : - result = value # TODO return bytes or str? + result = value # TODO return bytes or str? return result cpdef object get_line(self, index_t line_nb) : @@ -88,7 +89,7 @@ cdef class Column_multi_elts_char(Column_multi_elts): if value == OBIChar_NA : value_in_result = None else : - value_in_result = value # TODO return bytes or str? + value_in_result = value # TODO return bytes or str? result[self.elements_names[i]] = value_in_result if all_NA and (value_in_result is not None) : all_NA = False @@ -98,14 +99,17 @@ cdef class Column_multi_elts_char(Column_multi_elts): cpdef set_item(self, index_t line_nb, object elt_id, object value) : cdef bytes elt_name + cdef obichar_t value_b if value is None : - value = OBIChar_NA + value_b = OBIChar_NA + else : + value_b = tobytes(value)[0] if type(elt_id) == int : - if obi_set_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, tobytes(value)[0]) < 0 : + if obi_set_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value_b) < 0 : raise Exception("Problem setting a value in a column") else : elt_name = tobytes(elt_id) - if obi_set_char_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, tobytes(value)[0]) < 0 : + if obi_set_char_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value_b) < 0 : raise Exception("Problem setting a value in a column") diff --git a/python/obitools3/dms/column/typed_column/seq.pyx b/python/obitools3/dms/column/typed_column/seq.pyx index e44dd91..7b9b8ad 100644 --- a/python/obitools3/dms/column/typed_column/seq.pyx +++ b/python/obitools3/dms/column/typed_column/seq.pyx @@ -51,11 +51,15 @@ cdef class Column_seq(Column): cpdef set_line(self, index_t line_nb, object value): - cdef bytes value_b + cdef char* value_b + cdef bytes value_bytes + if value is None : - value_b = OBISeq_NA + value_b = OBISeq_NA else : - value_b = tobytes(value) + value_bytes = tobytes(value) + value_b = value_bytes + if obi_set_seq_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value_b) < 0: raise Exception("Problem setting a value in a column") @@ -112,15 +116,21 @@ cdef class Column_multi_elts_seq(Column_multi_elts): cpdef set_item(self, index_t line_nb, object elt_id, object value): - cdef bytes value_b + cdef bytes elt_name + cdef char* value_b + cdef bytes value_bytes + if value is None : - value_b = OBISeq_NA + value_b = OBISeq_NA else : - value_b = tobytes(value) + value_bytes = tobytes(value) + value_b = value_bytes + if type(elt_id) == int : if obi_set_seq_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value_b) < 0: raise Exception("Problem setting a value in a column") + else : elt_name = tobytes(elt_id) if obi_set_seq_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value_b) < 0: diff --git a/python/obitools3/dms/column/typed_column/str.pyx b/python/obitools3/dms/column/typed_column/str.pyx index d39ceab..d44b160 100644 --- a/python/obitools3/dms/column/typed_column/str.pyx +++ b/python/obitools3/dms/column/typed_column/str.pyx @@ -38,6 +38,7 @@ cdef class Column_str(Column): value = obi_get_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0) if obi_errno > 0 : raise IndexError(line_nb) + print(value == NULL) if value == OBIStr_NA : result = None else : @@ -46,11 +47,15 @@ cdef class Column_str(Column): cpdef set_line(self, index_t line_nb, object value): - cdef bytes value_b + cdef char* value_b + cdef bytes value_bytes + if value is None : - value_b = OBIStr_NA + value_b = OBIStr_NA else : - value_b = tobytes(value) + value_bytes = tobytes(value) + value_b = value_bytes + if obi_set_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value_b) < 0: raise Exception("Problem setting a value in a column") @@ -66,6 +71,7 @@ cdef class Column_multi_elts_str(Column_multi_elts): else : elt_name = tobytes(elt_id) value = obi_get_str_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name) + print(value == NULL) if obi_errno > 0 : raise IndexError(line_nb, elt_id) if value == OBIStr_NA : @@ -86,6 +92,7 @@ cdef class Column_multi_elts_str(Column_multi_elts): all_NA = True for i in range(self.nb_elements_per_line) : value = obi_get_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i) + print(value == NULL) if obi_errno > 0 : raise IndexError(line_nb) if value == OBIStr_NA : @@ -101,18 +108,22 @@ cdef class Column_multi_elts_str(Column_multi_elts): cpdef set_item(self, index_t line_nb, object elt_id, object value): - cdef bytes value_b cdef bytes elt_name + cdef char* value_b + cdef bytes value_bytes + if value is None : - value_b = OBIStr_NA + value_b = OBIStr_NA else : - value_b = tobytes(value) + value_bytes = tobytes(value) + value_b = value_bytes + if type(elt_id) == int : - if obi_set_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value_b) < 0: + if obi_set_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value_b) < 0: raise Exception("Problem setting a value in a column") else : elt_name = tobytes(elt_id) - if obi_set_str_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value_b) < 0: + if obi_set_str_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value_b) < 0: raise Exception("Problem setting a value in a column")