Skip to content

Commit 9215b9d

Browse files
committed
refactor: updated for igraph 1.0.0
1 parent d801058 commit 9215b9d

File tree

7 files changed

+239
-61
lines changed

7 files changed

+239
-61
lines changed

src/_igraph/convert.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,60 @@ int igraphmodule_PyObject_to_integer_t(PyObject *object, igraph_integer_t *v) {
10231023
return 1;
10241024
}
10251025

1026+
/**
1027+
* \brief Converts a Python object to an igraph \c igraph_integer_t when it is
1028+
* used as a limit on the number of results for some function.
1029+
*
1030+
* This is different from \ref igraphmodule_PyObject_to_integer_t such that it
1031+
* converts None and positive infinity to \c IGRAPH_UNLIMITED, and it does not
1032+
* accept negative values.
1033+
*
1034+
* Raises suitable Python exceptions when needed.
1035+
*
1036+
* \param object the Python object to be converted
1037+
* \param v the result is stored here
1038+
* \return 0 if everything was OK, 1 otherwise
1039+
*/
1040+
int igraphmodule_PyObject_to_max_results_t(PyObject *object, igraph_integer_t *v) {
1041+
int retval;
1042+
igraph_integer_t num;
1043+
1044+
if (object != NULL) {
1045+
if (object == Py_None) {
1046+
*v = IGRAPH_UNLIMITED;
1047+
return 0;
1048+
}
1049+
1050+
if (PyNumber_Check(object)) {
1051+
PyObject *flt = PyNumber_Float(object);
1052+
if (flt == NULL) {
1053+
return 1;
1054+
}
1055+
1056+
if (PyFloat_AsDouble(flt) == IGRAPH_INFINITY) {
1057+
Py_DECREF(flt);
1058+
*v = IGRAPH_UNLIMITED;
1059+
return 0;
1060+
}
1061+
1062+
Py_DECREF(flt);
1063+
}
1064+
}
1065+
1066+
retval = igraphmodule_PyObject_to_integer_t(object, &num);
1067+
if (retval) {
1068+
return retval;
1069+
}
1070+
1071+
if (num < 0) {
1072+
PyErr_SetString(PyExc_ValueError, "expected non-negative integer, None or infinity");
1073+
return 1;
1074+
}
1075+
1076+
*v = num;
1077+
return 0;
1078+
}
1079+
10261080
/**
10271081
* \brief Converts a Python object to an igraph \c igraph_real_t
10281082
*

src/_igraph/convert.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ int igraphmodule_PyObject_to_integer_t(PyObject *object, igraph_integer_t *v);
104104
int igraphmodule_PyObject_to_real_t(PyObject *object, igraph_real_t *v);
105105
int igraphmodule_PyObject_to_igraph_t(PyObject *o, igraph_t **result);
106106

107+
int igraphmodule_PyObject_to_max_results_t(PyObject *object, igraph_integer_t *v);
108+
107109
int igraphmodule_PyObject_to_vector_t(PyObject *list, igraph_vector_t *v,
108110
igraph_bool_t need_non_negative);
109111
int igraphmodule_PyObject_float_to_vector_t(PyObject *list, igraph_vector_t *v);

0 commit comments

Comments
 (0)