Skip to content

Commit

Permalink
finish error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgen-lentz committed Oct 30, 2024
1 parent f77f3c8 commit f313d81
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
12 changes: 5 additions & 7 deletions amplpy/dataframe.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cdef class Row(object):

def __getitem__(self, key):
cdef campl.AMPL_VARIANT* v
campl.AMPL_DataFrameElement(self._df, self._index, key, &v)
PY_AMPL_CALL(campl.AMPL_DataFrameElement(self._df, self._index, key, &v))
return to_py_variant(v)

def to_string(self):
Expand Down Expand Up @@ -74,9 +74,9 @@ cdef class Column(object):
cdef campl.AMPL_VARIANT* v
cdef size_t rowindex
cdef size_t i
campl.AMPL_DataFrameGetNumRows(self._df, &rowindex)
PY_AMPL_CALL(campl.AMPL_DataFrameGetNumRows(self._df, &rowindex))
for i in range(rowindex):
campl.AMPL_DataFrameElement(self._df, i, self._index, &v)
PY_AMPL_CALL(campl.AMPL_DataFrameElement(self._df, i, self._index, &v))
py_list.append(to_py_variant(v))
return py_list

Expand Down Expand Up @@ -150,7 +150,7 @@ cdef class DataFrame(object):
temp = column_names[i - index_size].encode('utf-8')
headers[i] = strdup(temp)

campl.AMPL_DataFrameCreate(&self._c_df, index_size, column_size, headers)
PY_AMPL_CALL(campl.AMPL_DataFrameCreate(&self._c_df, index_size, column_size, headers))

for i in range(index_size+column_size):
if headers[i] != NULL:
Expand All @@ -163,8 +163,6 @@ cdef class DataFrame(object):
for col in columns:
if isinstance(col, tuple):
self._set_column(col[0], col[1])
#else:
# impl = kwargs.get("_impl", None)

cdef campl.AMPL_DATAFRAME* get_ptr(self):
return self._c_df
Expand Down Expand Up @@ -216,7 +214,7 @@ cdef class DataFrame(object):
The number of rows.
"""
cdef size_t num
campl.AMPL_DataFrameGetNumRows(self._c_df, &num)
PY_AMPL_CALL(campl.AMPL_DataFrameGetNumRows(self._c_df, &num))
return int(num)

def _get_num_indices(self):
Expand Down
30 changes: 23 additions & 7 deletions amplpy/iterators.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,28 @@ cdef class InstanceIterator(object):

@staticmethod
cdef create(campl.AMPL* ampl, char* name, campl.AMPL_ENTITYTYPE entity_class, parent):
cdef campl.AMPL_ERRORINFO* errorinfo
cdef campl.AMPL_RETCODE rc
instanceit = InstanceIterator()
cdef size_t arity
instanceit._c_ampl = ampl
instanceit._name = name
instanceit.entity_class = entity_class
instanceit._entity = parent
campl.AMPL_EntityGetIndexarity(instanceit._c_ampl, instanceit._name, &arity)
PY_AMPL_CALL(campl.AMPL_EntityGetIndexarity(instanceit._c_ampl, instanceit._name, &arity))
if arity == 0:
instanceit._size = 1
instanceit.begin = NULL
instanceit.iterator = 0
instanceit.end = NULL
return instanceit
campl.AMPL_EntityGetTuples(instanceit._c_ampl, instanceit._name, &instanceit.begin, &instanceit._size)
errorinfo = campl.AMPL_EntityGetTuples(instanceit._c_ampl, instanceit._name, &instanceit.begin, &instanceit._size)
rc = campl.AMPL_ErrorInfoGetError(errorinfo)
if rc != campl.AMPL_OK:
for i in range(instanceit._size):
campl.AMPL_TupleFree(&instanceit.begin[i])
free(instanceit.begin)
PY_AMPL_CALL(errorinfo)
if instanceit._size == 0:
instanceit.iterator = 0
instanceit.end = NULL
Expand Down Expand Up @@ -194,14 +202,22 @@ cdef class MemberRangeIterator(object):

@staticmethod
cdef create(campl.AMPL* ampl, char* name, campl.AMPL_TUPLE* index, parent):
cdef campl.AMPL_ERRORINFO* errorinfo
cdef campl.AMPL_RETCODE rc
instanceit = MemberRangeIterator()
instanceit._c_ampl = ampl
instanceit._name = name
instanceit._index = index
instanceit._entity = parent
if instanceit._entity is not None:
Py_INCREF(instanceit._entity)
campl.AMPL_SetInstanceGetValues(instanceit._c_ampl, instanceit._name, instanceit._index, &instanceit.begin, &instanceit._size)
errorinfo = campl.AMPL_SetInstanceGetValues(instanceit._c_ampl, instanceit._name, instanceit._index, &instanceit.begin, &instanceit._size)
rc = campl.AMPL_ErrorInfoGetError(errorinfo)
if rc != campl.AMPL_OK:
for i in range(instanceit._size):
campl.AMPL_TupleFree(&instanceit.begin[i])
free(instanceit.begin)
PY_AMPL_CALL(errorinfo)
instanceit.iterator = 0
if instanceit._size == 0:
instanceit.end = NULL
Expand Down Expand Up @@ -256,7 +272,7 @@ cdef class ColIterator(object):
colit._df = df
colit._index = index
colit._rowit = 0
campl.AMPL_DataFrameGetNumRows(colit._df, &colit._rowsize)
PY_AMPL_CALL(campl.AMPL_DataFrameGetNumRows(colit._df, &colit._rowsize))
return colit

def __iter__(self):
Expand All @@ -266,7 +282,7 @@ cdef class ColIterator(object):
cdef campl.AMPL_VARIANT* v
if self._rowit >= self._rowsize:
raise StopIteration
campl.AMPL_DataFrameElement(self._df, self._rowit, self._index, &v)
PY_AMPL_CALL(campl.AMPL_DataFrameElement(self._df, self._rowit, self._index, &v))
self._rowit += 1
return to_py_variant(v)

Expand All @@ -282,7 +298,7 @@ cdef class RowIterator(object):
rowit._df = df
rowit._index = index
rowit._columnit = 0
campl.AMPL_DataFrameGetNumCols(rowit._df, &rowit._columnsize)
PY_AMPL_CALL(campl.AMPL_DataFrameGetNumCols(rowit._df, &rowit._columnsize))
return rowit

def __iter__(self):
Expand All @@ -292,6 +308,6 @@ cdef class RowIterator(object):
cdef campl.AMPL_VARIANT* v
if self._columnit >= self._columnsize:
raise StopIteration
campl.AMPL_DataFrameElement(self._df, self._index, self._columnit, &v)
PY_AMPL_CALL(campl.AMPL_DataFrameElement(self._df, self._index, self._columnit, &v))
self._columnit += 1
return to_py_variant(v)

0 comments on commit f313d81

Please sign in to comment.