From dbccfa7d3916926d07e31f9bbfff934039dedb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sat, 28 Nov 2026 15:55:45 +0000 Subject: [PATCH 1/7] fix: parse boolean arguments with the "p" format code Boolean arguments were parsed as objects with the "O" format code and converted later with PyObject_IsTrue(). Its return value -1, which signals that the conversion raised an exception, was used as if it were true, and the exception was left pending. This happens for example when a NumPy array with more than one element is passed as a boolean argument. The call then typically fails with an unrelated error, e.g. a SystemError or a spurious out-of-memory error from simplify(), or succeeds while leaving the exception set. Parse these arguments with the "p" format code instead, which applies the same truth test during argument parsing and raises the exception there. The change is mechanical: for each affected argument, the PyObject* variable becomes an int with the same default, "O" becomes "p" at its position in the format string, and PyObject_IsTrue(x) becomes x. Only arguments that are used for nothing else were converted. Assisted-by: Claude Opus 5.5 --- src/_igraph/graphobject.c | 662 +++++++++++++++++++---------------- src/_igraph/igraphmodule.c | 31 +- tests/test_bool_arguments.py | 62 ++++ tests/utils.py | 11 + 4 files changed, 443 insertions(+), 323 deletions(-) create mode 100644 tests/test_bool_arguments.py diff --git a/src/_igraph/graphobject.c b/src/_igraph/graphobject.c index 620818ed2..c3a9273f5 100644 --- a/src/_igraph/graphobject.c +++ b/src/_igraph/graphobject.c @@ -192,7 +192,8 @@ void igraphmodule_Graph_dealloc(igraphmodule_GraphObject * self) int igraphmodule_Graph_init(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { static char *kwlist[] = { "n", "edges", "directed", "__ptr", NULL }; - PyObject *edges = NULL, *dir = Py_False, *ptr_o = 0; + int dir = 0; + PyObject *edges = NULL, *ptr_o = 0; void* ptr = 0; Py_ssize_t n = 0; igraph_vector_int_t edges_vector; @@ -203,7 +204,7 @@ int igraphmodule_Graph_init(igraphmodule_GraphObject * self, self->destructor = NULL; self->weakreflist = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|nOOO!", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|nOpO!", kwlist, &n, &edges, &dir, &PyCapsule_Type, &ptr_o)) return -1; @@ -260,7 +261,7 @@ int igraphmodule_Graph_init(igraphmodule_GraphObject * self, /* We already have an undirected graph in &self->g. Make it directed first * if needed */ - if (PyObject_IsTrue(dir) && igraph_to_directed(&self->g, IGRAPH_TO_DIRECTED_ARBITRARY) != IGRAPH_SUCCESS) { + if (dir && igraph_to_directed(&self->g, IGRAPH_TO_DIRECTED_ARBITRARY) != IGRAPH_SUCCESS) { igraphmodule_handle_igraph_error(); retval = -1; goto cleanup; @@ -570,13 +571,13 @@ PyObject *igraphmodule_Graph_is_clique(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { PyObject *list = Py_None; - PyObject *directed = Py_False; + int directed = 0; igraph_bool_t res; igraph_vs_t vs; static char *kwlist[] = { "vertices", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &list, &directed)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Op", kwlist, &list, &directed)) { return NULL; } @@ -584,7 +585,7 @@ PyObject *igraphmodule_Graph_is_clique(igraphmodule_GraphObject * self, return NULL; } - if (igraph_is_clique(&self->g, vs, PyObject_IsTrue(directed), &res)) { + if (igraph_is_clique(&self->g, vs, directed, &res)) { igraphmodule_handle_igraph_error(); igraph_vs_destroy(&vs); return NULL; @@ -818,7 +819,7 @@ PyObject *igraphmodule_Graph_degree(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { PyObject *list = Py_None; - PyObject *loops = Py_True; + int loops = 1; PyObject *dmode_o = Py_None; igraph_neimode_t dmode = IGRAPH_ALL; igraph_vector_int_t res; @@ -827,7 +828,7 @@ PyObject *igraphmodule_Graph_degree(igraphmodule_GraphObject * self, static char *kwlist[] = { "vertices", "mode", "loops", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOp", kwlist, &list, &dmode_o, &loops)) return NULL; @@ -845,7 +846,7 @@ PyObject *igraphmodule_Graph_degree(igraphmodule_GraphObject * self, } if (igraph_degree(&self->g, &res, vs, - dmode, PyObject_IsTrue(loops))) { + dmode, loops)) { igraphmodule_handle_igraph_error(); igraph_vs_destroy(&vs); igraph_vector_int_destroy(&res); @@ -949,7 +950,7 @@ PyObject *igraphmodule_Graph_strength(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { PyObject *list = Py_None; - PyObject *loops = Py_True; + int loops = 1; PyObject *dmode_o = Py_None; PyObject *weights_o = Py_None; igraph_neimode_t dmode = IGRAPH_ALL; @@ -959,7 +960,7 @@ PyObject *igraphmodule_Graph_strength(igraphmodule_GraphObject * self, static char *kwlist[] = { "vertices", "mode", "loops", "weights", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOpO", kwlist, &list, &dmode_o, &loops, &weights_o)) return NULL; @@ -985,7 +986,7 @@ PyObject *igraphmodule_Graph_strength(igraphmodule_GraphObject * self, } if (igraph_strength(&self->g, &res, vs, dmode, - PyObject_IsTrue(loops), weights)) { + loops, weights)) { igraphmodule_handle_igraph_error(); igraph_vs_destroy(&vs); igraph_vector_destroy(&res); @@ -1016,17 +1017,18 @@ PyObject *igraphmodule_Graph_density(igraphmodule_GraphObject * self, { char *kwlist[] = { "loops", "weights", NULL }; igraph_real_t res; - PyObject *loops = Py_False, *weights_o = Py_None; + int loops = 0; + PyObject *weights_o = Py_None; igraph_vector_t *weights = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &loops, &weights_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pO", kwlist, &loops, &weights_o)) return NULL; if (igraphmodule_attrib_to_vector_t(weights_o, self, &weights, ATTRIBUTE_TYPE_EDGE)) { return NULL; } - if (igraph_density(&self->g, weights, &res, PyObject_IsTrue(loops))) { + if (igraph_density(&self->g, weights, &res, loops)) { igraphmodule_handle_igraph_error(); if (weights) { igraph_vector_destroy(weights); free(weights); @@ -1051,12 +1053,12 @@ PyObject *igraphmodule_Graph_mean_degree(igraphmodule_GraphObject * self, { char *kwlist[] = { "loops", NULL }; igraph_real_t res; - PyObject *loops = Py_True; + int loops = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &loops)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|p", kwlist, &loops)) return NULL; - if (igraph_mean_degree(&self->g, &res, PyObject_IsTrue(loops))) { + if (igraph_mean_degree(&self->g, &res, loops)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -1075,14 +1077,14 @@ PyObject *igraphmodule_Graph_maxdegree(igraphmodule_GraphObject * self, PyObject *list = Py_None; igraph_neimode_t dmode = IGRAPH_ALL; PyObject *dmode_o = Py_None; - PyObject *loops = Py_False; + int loops = 0; igraph_int_t res; igraph_vs_t vs; igraph_bool_t return_single = false; static char *kwlist[] = { "vertices", "mode", "loops", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, &list, &dmode_o, &loops)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOp", kwlist, &list, &dmode_o, &loops)) return NULL; if (igraphmodule_PyObject_to_neimode_t(dmode_o, &dmode)) { @@ -1094,7 +1096,7 @@ PyObject *igraphmodule_Graph_maxdegree(igraphmodule_GraphObject * self, return NULL; } - if (igraph_maxdegree(&self->g, &res, vs, dmode, PyObject_IsTrue(loops))) { + if (igraph_maxdegree(&self->g, &res, vs, dmode, loops)) { igraphmodule_handle_igraph_error(); igraph_vs_destroy(&vs); return NULL; @@ -1212,11 +1214,11 @@ PyObject *igraphmodule_Graph_is_mutual(igraphmodule_GraphObject *self, igraph_vector_bool_t res; igraph_es_t es; igraph_bool_t return_single = false; - PyObject* loops_o = Py_True; + int loops_o = 1; static char *kwlist[] = { "edges", "loops", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &list, &loops_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Op", kwlist, &list, &loops_o)) return NULL; if (igraphmodule_PyObject_to_es_t(list, &es, &self->g, &return_single)) { @@ -1230,7 +1232,7 @@ PyObject *igraphmodule_Graph_is_mutual(igraphmodule_GraphObject *self, return NULL; } - if (igraph_is_mutual(&self->g, &res, es, PyObject_IsTrue(loops_o))) { + if (igraph_is_mutual(&self->g, &res, es, loops_o)) { igraphmodule_handle_igraph_error(); igraph_es_destroy(&es); igraph_vector_bool_destroy(&res); @@ -1328,7 +1330,8 @@ PyObject *igraphmodule_Graph_count_multiple(igraphmodule_GraphObject *self, PyObject *igraphmodule_Graph_neighbors(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { - PyObject *list, *dmode_o = Py_None, *index_o, *loops_o = Py_True, *multiple_o = Py_True; + int multiple_o = 1; + PyObject *list, *dmode_o = Py_None, *index_o, *loops_o = Py_True; igraph_neimode_t dmode = IGRAPH_ALL; igraph_loops_t loops = IGRAPH_LOOPS; igraph_bool_t multiple = 1; @@ -1337,7 +1340,7 @@ PyObject *igraphmodule_Graph_neighbors(igraphmodule_GraphObject * self, static char *kwlist[] = { "vertex", "mode", "loops", "multiple", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &index_o, &dmode_o, &loops_o, &multiple_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOp", kwlist, &index_o, &dmode_o, &loops_o, &multiple_o)) return NULL; if (igraphmodule_PyObject_to_neimode_t(dmode_o, &dmode)) { @@ -1348,7 +1351,7 @@ PyObject *igraphmodule_Graph_neighbors(igraphmodule_GraphObject * self, return NULL; } - multiple = PyObject_IsTrue(multiple_o); + multiple = multiple_o; if (igraphmodule_PyObject_to_vid(index_o, &idx, &self->g)) { return NULL; @@ -1436,15 +1439,16 @@ PyObject *igraphmodule_Graph_reciprocity(igraphmodule_GraphObject * self, char *kwlist[] = { "ignore_loops", "mode", NULL }; igraph_real_t res; igraph_reciprocity_t mode = IGRAPH_RECIPROCITY_DEFAULT; - PyObject *ignore_loops = Py_True, *mode_o = Py_None; + int ignore_loops = 1; + PyObject *mode_o = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &ignore_loops, &mode_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pO", kwlist, &ignore_loops, &mode_o)) return NULL; if (igraphmodule_PyObject_to_reciprocity_t(mode_o, &mode)) return NULL; - if (igraph_reciprocity(&self->g, &res, PyObject_IsTrue(ignore_loops), mode)) { + if (igraph_reciprocity(&self->g, &res, ignore_loops, mode)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -1541,12 +1545,12 @@ PyObject *igraphmodule_Graph_get_eid(igraphmodule_GraphObject * self, { static char *kwlist[] = { "v1", "v2", "directed", "error", NULL }; PyObject *v1, *v2; - PyObject *directed = Py_True; - PyObject *error = Py_True; + int directed = 1; + int error = 1; igraph_int_t idx1, idx2; igraph_int_t res; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist, &v1, &v2, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|pp", kwlist, &v1, &v2, &directed, &error)) return NULL; @@ -1557,7 +1561,7 @@ PyObject *igraphmodule_Graph_get_eid(igraphmodule_GraphObject * self, return NULL; if (igraph_get_eid(&self->g, &res, idx1, idx2, - PyObject_IsTrue(directed), PyObject_IsTrue(error))) + directed, error)) return igraphmodule_handle_igraph_error(); return igraphmodule_integer_t_to_PyObject(res); @@ -1572,13 +1576,13 @@ PyObject *igraphmodule_Graph_get_eids(igraphmodule_GraphObject * self, { static char *kwlist[] = { "pairs", "directed", "error", NULL }; PyObject *pairs_o = Py_None; - PyObject *directed = Py_True; - PyObject *error = Py_True; + int directed = 1; + int error = 1; PyObject *result_o = NULL; igraph_vector_int_t pairs, res; igraph_bool_t pairs_owned = false; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Opp", kwlist, &pairs_o, &directed, &error)) return NULL; @@ -1590,7 +1594,7 @@ PyObject *igraphmodule_Graph_get_eids(igraphmodule_GraphObject * self, return NULL; } - if (igraph_get_eids(&self->g, &res, &pairs, PyObject_IsTrue(directed), PyObject_IsTrue(error))) { + if (igraph_get_eids(&self->g, &res, &pairs, directed, error)) { if (pairs_owned) { igraph_vector_int_destroy(&pairs); } @@ -1622,7 +1626,7 @@ PyObject *igraphmodule_Graph_get_eids(igraphmodule_GraphObject * self, PyObject *igraphmodule_Graph_diameter(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { - PyObject *dir = Py_True, *vcount_if_unconnected = Py_True; + int dir = 1, vcount_if_unconnected = 1; PyObject *weights_o = Py_None; igraph_vector_t *weights = 0; igraph_real_t diameter; @@ -1631,7 +1635,7 @@ PyObject *igraphmodule_Graph_diameter(igraphmodule_GraphObject * self, "directed", "unconn", "weights", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ppO", kwlist, &dir, &vcount_if_unconnected, &weights_o)) return NULL; @@ -1643,7 +1647,7 @@ PyObject *igraphmodule_Graph_diameter(igraphmodule_GraphObject * self, igraph_diameter(&self->g, weights, &diameter, /* from, to, vertex_path, edge_path */ 0, 0, 0, 0, - PyObject_IsTrue(dir), PyObject_IsTrue(vcount_if_unconnected)) + dir, vcount_if_unconnected) ) { igraphmodule_handle_igraph_error(); if (weights) { @@ -1666,14 +1670,15 @@ PyObject *igraphmodule_Graph_diameter(igraphmodule_GraphObject * self, PyObject *igraphmodule_Graph_get_diameter(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { - PyObject *dir = Py_True, *vcount_if_unconnected = Py_True, *result_o; + int dir = 1, vcount_if_unconnected = 1; + PyObject *result_o; PyObject *weights_o = Py_None; igraph_vector_t *weights = 0; igraph_vector_int_t res; static char *kwlist[] = { "directed", "unconn", "weights", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ppO", kwlist, &dir, &vcount_if_unconnected, &weights_o)) return NULL; @@ -1690,7 +1695,7 @@ PyObject *igraphmodule_Graph_get_diameter(igraphmodule_GraphObject * self, igraph_diameter(&self->g, weights, 0, /* from, to, vertex_path, edge_path */ 0, 0, &res, 0, - PyObject_IsTrue(dir), PyObject_IsTrue(vcount_if_unconnected) + dir, vcount_if_unconnected ) ) { igraphmodule_handle_igraph_error(); @@ -1718,7 +1723,7 @@ PyObject *igraphmodule_Graph_get_diameter(igraphmodule_GraphObject * self, PyObject *igraphmodule_Graph_farthest_points(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { - PyObject *dir = Py_True, *vcount_if_unconnected = Py_True; + int dir = 1, vcount_if_unconnected = 1; PyObject *weights_o = Py_None; igraph_vector_t *weights = 0; igraph_int_t from, to; @@ -1726,7 +1731,7 @@ PyObject *igraphmodule_Graph_farthest_points(igraphmodule_GraphObject * self, static char *kwlist[] = { "directed", "unconn", "weights", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ppO", kwlist, &dir, &vcount_if_unconnected, &weights_o)) return NULL; @@ -1739,7 +1744,7 @@ PyObject *igraphmodule_Graph_farthest_points(igraphmodule_GraphObject * self, &self->g, weights, &len, /* from, to, vertex_path, edge_path */ &from, &to, 0, 0, - PyObject_IsTrue(dir), PyObject_IsTrue(vcount_if_unconnected) + dir, vcount_if_unconnected ) ) { igraphmodule_handle_igraph_error(); @@ -1767,12 +1772,12 @@ PyObject *igraphmodule_Graph_farthest_points(igraphmodule_GraphObject * self, PyObject *igraphmodule_Graph_girth(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { - PyObject *sc = Py_False; + int sc = 0; static char *kwlist[] = { "return_shortest_circle", NULL }; igraph_real_t girth; igraph_vector_int_t vids; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &sc)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|p", kwlist, &sc)) { return NULL; } @@ -1787,7 +1792,7 @@ PyObject *igraphmodule_Graph_girth(igraphmodule_GraphObject *self, return NULL; } - if (PyObject_IsTrue(sc)) { + if (sc) { PyObject* o; o = igraphmodule_vector_int_t_to_PyList(&vids); igraph_vector_int_destroy(&vids); @@ -2084,7 +2089,8 @@ PyObject *igraphmodule_Graph_Barabasi(PyTypeObject * type, igraph_bool_t has_outseq = false; igraph_t *start_from = 0; igraph_barabasi_algorithm_t algo = IGRAPH_BARABASI_PSUMTREE; - PyObject *m_obj = 0, *outpref = Py_False, *directed = Py_False; + int outpref = 0, directed = 0; + PyObject *m_obj = 0; PyObject *implementation_o = Py_None; PyObject *start_from_o = Py_None; @@ -2092,7 +2098,7 @@ PyObject *igraphmodule_Graph_Barabasi(PyTypeObject * type, { "n", "m", "outpref", "directed", "power", "zero_appeal", "implementation", "start_from", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|OOOffOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|OppffOO", kwlist, &n, &m_obj, &outpref, &directed, &power, &zero_appeal, &implementation_o, &start_from_o)) @@ -2123,9 +2129,9 @@ PyObject *igraphmodule_Graph_Barabasi(PyTypeObject * type, } if (igraph_barabasi_game(&g, n, power, m, - has_outseq ? &outseq : NULL, PyObject_IsTrue(outpref), + has_outseq ? &outseq : NULL, outpref, zero_appeal, - PyObject_IsTrue(directed), algo, + directed, algo, start_from)) { igraphmodule_handle_igraph_error(); if (has_outseq) { @@ -2156,11 +2162,12 @@ PyObject *igraphmodule_Graph_Bipartite(PyTypeObject * type, igraph_vector_bool_t types; igraph_vector_int_t edges; igraph_bool_t edges_owned = false; - PyObject *types_o, *edges_o, *directed = Py_False; + int directed = 0; + PyObject *types_o, *edges_o; static char *kwlist[] = { "types", "edges", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|p", kwlist, &types_o, &edges_o, &directed)) return NULL; @@ -2172,7 +2179,7 @@ PyObject *igraphmodule_Graph_Bipartite(PyTypeObject * type, return NULL; } - if (igraph_create_bipartite(&g, &types, &edges, PyObject_IsTrue(directed))) { + if (igraph_create_bipartite(&g, &types, &edges, directed)) { igraphmodule_handle_igraph_error(); if (edges_owned) { igraph_vector_int_destroy(&edges); @@ -2207,11 +2214,12 @@ PyObject *igraphmodule_Graph_Chung_Lu(PyTypeObject *type, PyObject *args, PyObje igraph_vector_t outw, inw; igraph_chung_lu_t var = IGRAPH_CHUNG_LU_ORIGINAL; igraph_bool_t has_inw = false; - PyObject *weight_out = NULL, *weight_in = NULL, *loops = Py_True, *variant = NULL; + int loops = 1; + PyObject *weight_out = NULL, *weight_in = NULL, *variant = NULL; static char *kwlist[] = { "out", "in_", "loops", "variant", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OpO", kwlist, &weight_out, &weight_in, &loops, &variant)) return NULL; @@ -2225,7 +2233,7 @@ PyObject *igraphmodule_Graph_Chung_Lu(PyTypeObject *type, PyObject *args, PyObje has_inw=true; } - if (igraph_chung_lu_game(&g, &outw, has_inw ? &inw : NULL, PyObject_IsTrue(loops), var)) { + if (igraph_chung_lu_game(&g, &outw, has_inw ? &inw : NULL, loops, var)) { igraphmodule_handle_igraph_error(); igraph_vector_destroy(&outw); if (has_inw) @@ -2333,12 +2341,12 @@ PyObject *igraphmodule_Graph_Erdos_Renyi(PyTypeObject * type, igraph_t g; Py_ssize_t n, m = -1; double p = -1.0; - PyObject *loops = Py_False, *directed = Py_False, *edge_labeled = Py_False; + int loops = 0, directed = 0, edge_labeled = 0; int retval; static char *kwlist[] = { "n", "p", "m", "directed", "loops", "edge_labeled", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|dnOOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|dnppp", kwlist, &n, &p, &m, &directed, &loops, @@ -2362,16 +2370,16 @@ PyObject *igraphmodule_Graph_Erdos_Renyi(PyTypeObject * type, if (m == -1) { /* GNP model */ retval = igraph_erdos_renyi_game_gnp( - &g, n, p, PyObject_IsTrue(directed), - PyObject_IsTrue(loops) ? IGRAPH_LOOPS_SW : IGRAPH_SIMPLE_SW, - PyObject_IsTrue(edge_labeled) + &g, n, p, directed, + loops ? IGRAPH_LOOPS_SW : IGRAPH_SIMPLE_SW, + edge_labeled ); } else { /* GNM model */ retval = igraph_erdos_renyi_game_gnm( - &g, n, m, PyObject_IsTrue(directed), - PyObject_IsTrue(loops) ? IGRAPH_LOOPS_SW : IGRAPH_SIMPLE_SW, - PyObject_IsTrue(edge_labeled) + &g, n, m, directed, + loops ? IGRAPH_LOOPS_SW : IGRAPH_SIMPLE_SW, + edge_labeled ); } @@ -2397,13 +2405,13 @@ PyObject *igraphmodule_Graph_Establishment(PyTypeObject * type, igraph_t g; Py_ssize_t n, types, k; PyObject *type_dist, *pref_matrix; - PyObject *directed = Py_False; + int directed = 0; igraph_matrix_t pm; igraph_vector_t td; char *kwlist[] = { "n", "k", "type_dist", "pref_matrix", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nnOO|O", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nnOO|p", kwlist, &n, &k, &type_dist, &pref_matrix, &directed)) return NULL; @@ -2438,7 +2446,7 @@ PyObject *igraphmodule_Graph_Establishment(PyTypeObject * type, return NULL; } - if (igraph_establishment_game(&g, n, types, k, &td, &pm, PyObject_IsTrue(directed), 0)) { + if (igraph_establishment_game(&g, n, types, k, &td, &pm, directed, 0)) { igraphmodule_handle_igraph_error(); igraph_vector_destroy(&td); igraph_matrix_destroy(&pm); @@ -2492,18 +2500,18 @@ PyObject *igraphmodule_Graph_Forest_Fire(PyTypeObject * type, igraph_t g; Py_ssize_t n, ambs = 1; double fw_prob, bw_factor=0.0; - PyObject *directed = Py_False; + int directed = 0; static char *kwlist[] = {"n", "fw_prob", "bw_factor", "ambs", "directed", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nd|dnO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nd|dnp", kwlist, &n, &fw_prob, &bw_factor, &ambs, &directed)) return NULL; CHECK_SSIZE_T_RANGE(n, "vertex count"); CHECK_SSIZE_T_RANGE(n, "number of ambassadors"); - if (igraph_forest_fire_game(&g, n, fw_prob, bw_factor, ambs, PyObject_IsTrue(directed))) { + if (igraph_forest_fire_game(&g, n, fw_prob, bw_factor, ambs, directed)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -2525,16 +2533,16 @@ PyObject *igraphmodule_Graph_Full(PyTypeObject * type, igraphmodule_GraphObject *self; igraph_t g; Py_ssize_t n; - PyObject *loops = Py_False, *directed = Py_False; + int loops = 0, directed = 0; char *kwlist[] = { "n", "directed", "loops", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|OO", kwlist, &n, &directed, &loops)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|pp", kwlist, &n, &directed, &loops)) return NULL; CHECK_SSIZE_T_RANGE(n, "vertex count"); - if (igraph_full(&g, n, PyObject_IsTrue(directed), PyObject_IsTrue(loops))) { + if (igraph_full(&g, n, directed, loops)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -2556,11 +2564,12 @@ PyObject *igraphmodule_Graph_Full_Bipartite(PyTypeObject * type, igraph_vector_bool_t vertex_types; igraph_neimode_t mode = IGRAPH_ALL; Py_ssize_t n1, n2; - PyObject *mode_o = Py_None, *directed = Py_False, *vertex_types_o = 0; + int directed = 0; + PyObject *mode_o = Py_None, *vertex_types_o = 0; static char *kwlist[] = { "n1", "n2", "directed", "mode", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|OO", kwlist, &n1, &n2, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|pO", kwlist, &n1, &n2, &directed, &mode_o)) return NULL; @@ -2576,7 +2585,7 @@ PyObject *igraphmodule_Graph_Full_Bipartite(PyTypeObject * type, return NULL; } - if (igraph_full_bipartite(&g, &vertex_types, n1, n2, PyObject_IsTrue(directed), mode)) { + if (igraph_full_bipartite(&g, &vertex_types, n1, n2, directed, mode)) { igraph_vector_bool_destroy(&vertex_types); igraphmodule_handle_igraph_error(); return NULL; @@ -2608,16 +2617,16 @@ PyObject *igraphmodule_Graph_Full_Citation(PyTypeObject *type, igraphmodule_GraphObject *self; igraph_t g; Py_ssize_t n; - PyObject *directed = Py_False; + int directed = 0; char *kwlist[] = { "n", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|O", kwlist, &n, &directed)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|p", kwlist, &n, &directed)) return NULL; CHECK_SSIZE_T_RANGE(n, "vertex count"); - if (igraph_full_citation(&g, n, PyObject_IsTrue(directed))) { + if (igraph_full_citation(&g, n, directed)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -2639,13 +2648,13 @@ PyObject *igraphmodule_Graph_GRG(PyTypeObject * type, igraph_t g; Py_ssize_t n; double r; - PyObject *torus = Py_False; + int torus = 0; PyObject *o_xs, *o_ys; igraph_vector_t xs, ys; static char *kwlist[] = { "n", "radius", "torus", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nd|O", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nd|p", kwlist, &n, &r, &torus)) return NULL; @@ -2660,7 +2669,7 @@ PyObject *igraphmodule_Graph_GRG(PyTypeObject * type, CHECK_SSIZE_T_RANGE(n, "vertex count"); - if (igraph_grg_game(&g, n, r, PyObject_IsTrue(torus), &xs, &ys)) { + if (igraph_grg_game(&g, n, r, torus, &xs, &ys)) { igraphmodule_handle_igraph_error(); igraph_vector_destroy(&xs); igraph_vector_destroy(&ys); @@ -2702,20 +2711,20 @@ PyObject *igraphmodule_Graph_Growing_Random(PyTypeObject * type, PyObject * args, PyObject * kwds) { Py_ssize_t n, m; - PyObject *directed = Py_False, *citation = Py_False; + int directed = 0, citation = 0; igraphmodule_GraphObject *self; igraph_t g; static char *kwlist[] = { "n", "m", "directed", "citation", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|OO", kwlist, &n, &m, &directed, &citation)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|pp", kwlist, &n, &m, &directed, &citation)) { return NULL; } CHECK_SSIZE_T_RANGE(n, "vertex count"); CHECK_SSIZE_T_RANGE_POSITIVE(m, "number of new edges per iteration"); - if (igraph_growing_random_game(&g, n, m, PyObject_IsTrue(directed), PyObject_IsTrue(citation))) { + if (igraph_growing_random_game(&g, n, m, directed, citation)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -2736,19 +2745,19 @@ PyObject *igraphmodule_Graph_Hexagonal_Lattice(PyTypeObject * type, igraph_vector_int_t dimvector; igraph_bool_t directed; igraph_bool_t mutual; - PyObject *o_directed = Py_False, *o_mutual = Py_True; + int o_directed = 0, o_mutual = 1; PyObject *o_dimvector = Py_None; igraphmodule_GraphObject *self; igraph_t g; static char *kwlist[] = { "dim", "directed", "mutual", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pp", kwlist, &o_dimvector, &o_directed, &o_mutual)) return NULL; - directed = PyObject_IsTrue(o_directed); - mutual = PyObject_IsTrue(o_mutual); + directed = o_directed; + mutual = o_mutual; if (igraphmodule_PyObject_to_vector_int_t(o_dimvector, &dimvector)) return NULL; @@ -2777,19 +2786,19 @@ PyObject *igraphmodule_Graph_Hypercube(PyTypeObject * type, { Py_ssize_t n; igraph_bool_t directed; - PyObject *o_directed = Py_False; + int o_directed = 0; igraphmodule_GraphObject *self; igraph_t g; static char *kwlist[] = { "n", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|O", kwlist, &n, &o_directed)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|p", kwlist, &n, &o_directed)) { return NULL; } CHECK_SSIZE_T_RANGE(n, "vertex count"); - directed = PyObject_IsTrue(o_directed); + directed = o_directed; if (igraph_hypercube(&g, n, directed)) { igraphmodule_handle_igraph_error(); @@ -2813,12 +2822,13 @@ PyObject *igraphmodule_Graph_Biadjacency(PyTypeObject * type, igraph_vector_bool_t vertex_types; igraph_t g; PyObject *matrix_o, *vertex_types_o; - PyObject *mode_o = Py_None, *directed = Py_False, *multiple = Py_False; + int directed = 0, multiple = 0; + PyObject *mode_o = Py_None; igraph_neimode_t mode = IGRAPH_OUT; static char *kwlist[] = { "matrix", "directed", "mode", "multiple", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &matrix_o, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pOp", kwlist, &matrix_o, &directed, &mode_o, &multiple)) return NULL; @@ -2835,7 +2845,7 @@ PyObject *igraphmodule_Graph_Biadjacency(PyTypeObject * type, } if (igraph_biadjacency(&g, &vertex_types, &matrix, - PyObject_IsTrue(directed), mode, PyObject_IsTrue(multiple))) { + directed, mode, multiple)) { igraphmodule_handle_igraph_error(); igraph_matrix_destroy(&matrix); igraph_vector_bool_destroy(&vertex_types); @@ -2867,17 +2877,17 @@ PyObject *igraphmodule_Graph_Isoclass(PyTypeObject * type, PyObject * args, PyObject * kwds) { Py_ssize_t n, isoclass; - PyObject *directed = Py_False; + int directed = 0; igraphmodule_GraphObject *self; igraph_t g; static char *kwlist[] = { "n", "cls", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|O", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|p", kwlist, &n, &isoclass, &directed)) return NULL; - if (igraph_isoclass_create(&g, n, isoclass, PyObject_IsTrue(directed))) { + if (igraph_isoclass_create(&g, n, isoclass, directed)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -2926,18 +2936,18 @@ PyObject *igraphmodule_Graph_K_Regular(PyTypeObject * type, igraphmodule_GraphObject *self; igraph_t g; Py_ssize_t n, k; - PyObject *directed_o = Py_False, *multiple_o = Py_False; + int directed_o = 0, multiple_o = 0; static char *kwlist[] = { "n", "k", "directed", "multiple", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|OO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|pp", kwlist, &n, &k, &directed_o, &multiple_o)) return NULL; CHECK_SSIZE_T_RANGE(n, "vertex count"); CHECK_SSIZE_T_RANGE(k, "degree"); - if (igraph_k_regular_game(&g, n, k, PyObject_IsTrue(directed_o), PyObject_IsTrue(multiple_o))) { + if (igraph_k_regular_game(&g, n, k, directed_o, multiple_o)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -2960,19 +2970,20 @@ PyObject *igraphmodule_Graph_Lattice(PyTypeObject * type, Py_ssize_t nei = 1; igraph_bool_t directed; igraph_bool_t mutual; - PyObject *o_directed = Py_False, *o_mutual = Py_True, *o_circular = Py_True; + int o_directed = 0, o_mutual = 1; + PyObject *o_circular = Py_True; PyObject *o_dimvector = Py_None; igraphmodule_GraphObject *self; igraph_t g; static char *kwlist[] = { "dim", "nei", "directed", "mutual", "circular", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nOOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nppO", kwlist, &o_dimvector, &nei, &o_directed, &o_mutual, &o_circular)) return NULL; - directed = PyObject_IsTrue(o_directed); - mutual = PyObject_IsTrue(o_mutual); + directed = o_directed; + mutual = o_mutual; if (igraphmodule_PyObject_to_vector_int_t(o_dimvector, &dimvector)) return NULL; @@ -3178,8 +3189,8 @@ PyObject *igraphmodule_Graph_Preference(PyTypeObject * type, igraph_t g; Py_ssize_t n, types; PyObject *type_dist, *pref_matrix; - PyObject *directed = Py_False; - PyObject *loops = Py_False; + int directed = 0; + int loops = 0; igraph_matrix_t pm; igraph_vector_t td; igraph_vector_int_t type_vec; @@ -3191,7 +3202,7 @@ PyObject *igraphmodule_Graph_Preference(PyTypeObject * type, { "n", "type_dist", "pref_matrix", "attribute", "directed", "loops", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nOO|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nOO|Opp", kwlist, &n, &type_dist, &pref_matrix, &attribute_key, &directed, &loops)) return NULL; @@ -3217,8 +3228,8 @@ NULL }; if (igraph_preference_game(&g, n, types, &td, 0, &pm, store_attribs ? &type_vec : 0, - PyObject_IsTrue(directed), - PyObject_IsTrue(loops))) { + directed, + loops)) { igraphmodule_handle_igraph_error(); igraph_matrix_destroy(&pm); igraph_vector_destroy(&td); @@ -3272,7 +3283,7 @@ PyObject *igraphmodule_Graph_Asymmetric_Preference(PyTypeObject * type, igraph_t g; Py_ssize_t n, in_types, out_types; PyObject *type_dist_matrix, *pref_matrix; - PyObject *loops = Py_False; + int loops = 0; igraph_matrix_t pm; igraph_matrix_t td; igraph_vector_int_t in_type_vec, out_type_vec; @@ -3283,7 +3294,7 @@ PyObject *igraphmodule_Graph_Asymmetric_Preference(PyTypeObject * type, char *kwlist[] = { "n", "type_dist_matrix", "pref_matrix", "attribute", "loops", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nOO|OO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nOO|Op", kwlist, &n, &type_dist_matrix, &pref_matrix, &attribute_key, &loops)) @@ -3322,7 +3333,7 @@ PyObject *igraphmodule_Graph_Asymmetric_Preference(PyTypeObject * type, if (igraph_asymmetric_preference_game(&g, n, in_types, out_types, &td, &pm, store_attribs ? &in_type_vec : 0, store_attribs ? &out_type_vec : 0, - PyObject_IsTrue(loops))) { + loops)) { igraphmodule_handle_igraph_error(); igraph_vector_int_destroy(&in_type_vec); igraph_vector_int_destroy(&out_type_vec); @@ -3417,7 +3428,8 @@ PyObject *igraphmodule_Graph_Random_Bipartite(PyTypeObject * type, Py_ssize_t n1, n2, m = -1; double p = -1.0; igraph_neimode_t neimode = IGRAPH_ALL; - PyObject *directed_o = Py_False, *neimode_o = NULL, *edge_labeled_o = Py_False; + int directed_o = 0, edge_labeled_o = 0; + PyObject *neimode_o = NULL; PyObject *edge_types_o = Py_None; igraph_vector_bool_t vertex_types; igraph_edge_type_sw_t allowed_edge_types = IGRAPH_SIMPLE_SW; @@ -3428,7 +3440,7 @@ PyObject *igraphmodule_Graph_Random_Bipartite(PyTypeObject * type, "n1", "n2", "p", "m", "directed", "neimode", "allowed_edge_types", "edge_labeled", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|dnOOOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|dnpOOp", kwlist, &n1, &n2, &p, &m, &directed_o, &neimode_o, &edge_types_o, &edge_labeled_o)) return NULL; @@ -3461,14 +3473,14 @@ PyObject *igraphmodule_Graph_Random_Bipartite(PyTypeObject * type, if (m == -1) { /* GNP model */ retval = igraph_bipartite_game_gnp( - &g, &vertex_types, n1, n2, p, PyObject_IsTrue(directed_o), neimode, - allowed_edge_types, PyObject_IsTrue(edge_labeled_o) + &g, &vertex_types, n1, n2, p, directed_o, neimode, + allowed_edge_types, edge_labeled_o ); } else { /* GNM model */ retval = igraph_bipartite_game_gnm( - &g, &vertex_types, n1, n2, m, PyObject_IsTrue(directed_o), neimode, - allowed_edge_types, PyObject_IsTrue(edge_labeled_o) + &g, &vertex_types, n1, n2, m, directed_o, neimode, + allowed_edge_types, edge_labeled_o ); } @@ -3505,13 +3517,14 @@ PyObject *igraphmodule_Graph_Recent_Degree(PyTypeObject * type, igraph_int_t m = 0; igraph_vector_int_t outseq; igraph_bool_t has_outseq = false; - PyObject *m_obj, *outpref = Py_False, *directed = Py_False; + int outpref = 0, directed = 0; + PyObject *m_obj; char *kwlist[] = { "n", "m", "window", "outpref", "directed", "power", "zero_appeal", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nOn|OOff", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nOn|ppff", kwlist, &n, &m_obj, &window, &outpref, &directed, &power, &zero_appeal)) return NULL; @@ -3533,9 +3546,9 @@ NULL }; } if (igraph_recent_degree_game(&g, n, power, window, m, has_outseq ? &outseq : NULL, - PyObject_IsTrue(outpref), + outpref, zero_appeal, - PyObject_IsTrue(directed))) { + directed)) { igraphmodule_handle_igraph_error(); if (has_outseq) { igraph_vector_int_destroy(&outseq); @@ -3561,19 +3574,19 @@ PyObject *igraphmodule_Graph_Ring(PyTypeObject * type, PyObject * args, PyObject * kwds) { Py_ssize_t n; - PyObject *directed = Py_False, *mutual = Py_False, *circular = Py_True; + int directed = 0, mutual = 0, circular = 1; igraphmodule_GraphObject *self; igraph_t g; static char *kwlist[] = { "n", "directed", "mutual", "circular", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|OOO", kwlist, &n, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|ppp", kwlist, &n, &directed, &mutual, &circular)) return NULL; CHECK_SSIZE_T_RANGE(n, "vertex count"); - if (igraph_ring(&g, n, PyObject_IsTrue(directed), PyObject_IsTrue(mutual), PyObject_IsTrue(circular))) { + if (igraph_ring(&g, n, directed, mutual, circular)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -3594,14 +3607,14 @@ PyObject *igraphmodule_Graph_SBM(PyTypeObject * type, igraphmodule_GraphObject *self; igraph_t g; PyObject *block_sizes_o, *pref_matrix_o, *edge_types_o = Py_None; - PyObject *directed_o = Py_False; + int directed_o = 0; igraph_edge_type_sw_t allowed_edge_types = IGRAPH_SIMPLE_SW; igraph_matrix_t pref_matrix; igraph_vector_int_t block_sizes; static char *kwlist[] = { "pref_matrix", "block_sizes", "directed", "allowed_edge_types", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|pO", kwlist, &pref_matrix_o, &block_sizes_o, &directed_o, &edge_types_o)) @@ -3619,7 +3632,7 @@ PyObject *igraphmodule_Graph_SBM(PyTypeObject * type, return NULL; } - if (igraph_sbm_game(&g, &pref_matrix, &block_sizes, PyObject_IsTrue(directed_o), allowed_edge_types)) { + if (igraph_sbm_game(&g, &pref_matrix, &block_sizes, directed_o, allowed_edge_types)) { igraphmodule_handle_igraph_error(); igraph_matrix_destroy(&pref_matrix); igraph_vector_int_destroy(&block_sizes); @@ -3759,12 +3772,12 @@ PyObject *igraphmodule_Graph_Static_Power_Law(PyTypeObject *type, float exponent_out = -1.0f, exponent_in = -1.0f, exponent = -1.0f; PyObject *edge_types_o = Py_None; igraph_edge_type_sw_t allowed_edge_types = IGRAPH_SIMPLE_SW; - PyObject *finite_size_correction = Py_True; + int finite_size_correction = 1; static char *kwlist[] = { "n", "m", "exponent_out", "exponent_in", "allowed_edge_types", "finite_size_correction", "exponent", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|ffOOf", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn|ffOpf", kwlist, &n, &m, &exponent_out, &exponent_in, &edge_types_o, &finite_size_correction, &exponent)) @@ -3789,7 +3802,7 @@ PyObject *igraphmodule_Graph_Static_Power_Law(PyTypeObject *type, if (igraph_static_power_law_game(&g, n, m, exponent_out, exponent_in, allowed_edge_types, - PyObject_IsTrue(finite_size_correction))) { + finite_size_correction)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -3860,14 +3873,15 @@ PyObject *igraphmodule_Graph_Tree_Game(PyTypeObject * type, PyObject * args, PyObject * kwds) { Py_ssize_t n; - PyObject *directed = Py_False, *tree_method_o = Py_None; + int directed = 0; + PyObject *tree_method_o = Py_None; igraph_random_tree_t tree_method = IGRAPH_RANDOM_TREE_LERW; igraphmodule_GraphObject *self; igraph_t g; static char *kwlist[] = { "n", "directed", "method", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|OO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "n|pO", kwlist, &n, &directed, &tree_method_o)) return NULL; @@ -3877,7 +3891,7 @@ PyObject *igraphmodule_Graph_Tree_Game(PyTypeObject * type, return NULL; } - if (igraph_tree_game(&g, n, PyObject_IsTrue(directed), tree_method)) { + if (igraph_tree_game(&g, n, directed, tree_method)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -3898,19 +3912,19 @@ PyObject *igraphmodule_Graph_Triangular_Lattice(PyTypeObject * type, igraph_vector_int_t dimvector; igraph_bool_t directed; igraph_bool_t mutual; - PyObject *o_directed = Py_False, *o_mutual = Py_True; + int o_directed = 0, o_mutual = 1; PyObject *o_dimvector = Py_None; igraphmodule_GraphObject *self; igraph_t g; static char *kwlist[] = { "dim", "directed", "mutual", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pp", kwlist, &o_dimvector, &o_directed, &o_mutual)) return NULL; - directed = PyObject_IsTrue(o_directed); - mutual = PyObject_IsTrue(o_mutual); + directed = o_directed; + mutual = o_mutual; if (igraphmodule_PyObject_to_vector_int_t(o_dimvector, &dimvector)) return NULL; @@ -4070,13 +4084,14 @@ PyObject *igraphmodule_Graph_articulation_points(igraphmodule_GraphObject* self, PyObject *igraphmodule_Graph_assortativity_nominal(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "types", "directed", "normalized", "weights", NULL }; - PyObject *types_o = Py_None, *weights_o = Py_None, *directed = Py_True, *normalized = Py_True; + int directed = 1, normalized = 1; + PyObject *types_o = Py_None, *weights_o = Py_None; igraph_real_t res; igraph_error_t ret; igraph_vector_int_t *types = 0; igraph_vector_t *weights = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, &types_o, &directed, &normalized, &weights_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pp", kwlist, &types_o, &directed, &normalized, &weights_o)) return NULL; if (igraphmodule_attrib_to_vector_int_t(types_o, self, &types, ATTRIBUTE_TYPE_VERTEX)) @@ -4087,8 +4102,8 @@ PyObject *igraphmodule_Graph_assortativity_nominal(igraphmodule_GraphObject *sel return NULL; } - ret = igraph_assortativity_nominal(&self->g, weights, types, &res, PyObject_IsTrue(directed), - PyObject_IsTrue(normalized)); + ret = igraph_assortativity_nominal(&self->g, weights, types, &res, directed, + normalized); if (types) { igraph_vector_int_destroy(types); free(types); @@ -4113,13 +4128,14 @@ PyObject *igraphmodule_Graph_assortativity_nominal(igraphmodule_GraphObject *sel PyObject *igraphmodule_Graph_assortativity(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "types1", "types2", "directed", "normalized", "weights", NULL }; - PyObject *types1_o = Py_None, *types2_o = Py_None, *directed = Py_True, *normalized = Py_True; + int directed = 1, normalized = 1; + PyObject *types1_o = Py_None, *types2_o = Py_None; PyObject *weights_o = Py_None; igraph_real_t res; igraph_error_t ret; igraph_vector_t *types1 = 0, *types2 = 0, *weights = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOOO", kwlist, &types1_o, &types2_o, &directed, &normalized, &weights_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OppO", kwlist, &types1_o, &types2_o, &directed, &normalized, &weights_o)) return NULL; if (igraphmodule_attrib_to_vector_t(types1_o, self, &types1, ATTRIBUTE_TYPE_VERTEX)) { @@ -4137,7 +4153,7 @@ PyObject *igraphmodule_Graph_assortativity(igraphmodule_GraphObject *self, PyObj return NULL; } - ret = igraph_assortativity(&self->g, weights, types1, types2, &res, PyObject_IsTrue(directed), PyObject_IsTrue(normalized)); + ret = igraph_assortativity(&self->g, weights, types1, types2, &res, directed, normalized); if (types1) { igraph_vector_destroy(types1); free(types1); } if (types2) { igraph_vector_destroy(types2); free(types2); } @@ -4158,13 +4174,13 @@ PyObject *igraphmodule_Graph_assortativity(igraphmodule_GraphObject *self, PyObj PyObject *igraphmodule_Graph_assortativity_degree(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "directed", NULL }; - PyObject *directed = Py_True; + int directed = 1; igraph_real_t res; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &directed)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|p", kwlist, &directed)) return NULL; - if (igraph_assortativity_degree(&self->g, &res, PyObject_IsTrue(directed))) { + if (igraph_assortativity_degree(&self->g, &res, directed)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -4183,14 +4199,14 @@ PyObject *igraphmodule_Graph_authority_score( PyObject *scale_o = Py_True, *weights_o = Py_None; PyObject *arpack_options_o = igraphmodule_arpack_options_default; igraphmodule_ARPACKOptionsObject *arpack_options; - PyObject *return_eigenvalue = Py_False; + int return_eigenvalue = 0; PyObject *res_o; igraph_real_t value; igraph_vector_t res, *weights = 0; /* scale is deprecated but kept for backward compatibility reasons */ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO!O", kwlist, &weights_o, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO!p", kwlist, &weights_o, &scale_o, igraphmodule_ARPACKOptionsType, &arpack_options_o, &return_eigenvalue)) return NULL; @@ -4215,7 +4231,7 @@ PyObject *igraphmodule_Graph_authority_score( igraph_vector_destroy(&res); if (res_o == NULL) return igraphmodule_handle_igraph_error(); - if (PyObject_IsTrue(return_eigenvalue)) { + if (return_eigenvalue) { PyObject *ev_o = igraphmodule_real_t_to_PyObject(value, IGRAPHMODULE_TYPE_FLOAT); if (ev_o == NULL) { Py_DECREF(res_o); @@ -4239,17 +4255,17 @@ PyObject *igraphmodule_Graph_average_path_length(igraphmodule_GraphObject * { static char *kwlist[] = { "directed", "unconn", "weights", NULL }; PyObject *weights_o = Py_None; - PyObject *directed = Py_True, *unconn = Py_True; + int directed = 1, unconn = 1; igraph_real_t res; igraph_vector_t *weights = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, &directed, &unconn, &weights_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ppO", kwlist, &directed, &unconn, &weights_o)) return NULL; if (igraphmodule_attrib_to_vector_t(weights_o, self, &weights, ATTRIBUTE_TYPE_EDGE)) return NULL; - if (igraph_average_path_length(&self->g, weights, &res, 0, PyObject_IsTrue(directed), PyObject_IsTrue(unconn))) { + if (igraph_average_path_length(&self->g, weights, &res, 0, directed, unconn)) { if (weights) { igraph_vector_destroy(weights); free(weights); } @@ -4274,8 +4290,8 @@ PyObject *igraphmodule_Graph_betweenness(igraphmodule_GraphObject * self, { static char *kwlist[] = { "vertices", "directed", "cutoff", "weights", "sources", "targets", "normalized", NULL }; - PyObject *directed = Py_True; - PyObject *normalized = Py_False; + int directed = 1; + int normalized = 0; PyObject *vobj = Py_None, *list; PyObject *cutoff = Py_None; PyObject *weights_o = Py_None; @@ -4287,7 +4303,7 @@ PyObject *igraphmodule_Graph_betweenness(igraphmodule_GraphObject * self, igraph_vs_t vs, sources, targets; igraph_error_t retval; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OpOOOOp", kwlist, &vobj, &directed, &cutoff, &weights_o, &sources_o, &targets_o, &normalized)) { return NULL; @@ -4330,10 +4346,10 @@ PyObject *igraphmodule_Graph_betweenness(igraphmodule_GraphObject * self, if (cutoff == Py_None) { if (is_subsetted) { retval = igraph_betweenness_subset( - &self->g, weights, &res, sources, targets, vs, PyObject_IsTrue(directed), PyObject_IsTrue(normalized) + &self->g, weights, &res, sources, targets, vs, directed, normalized ); } else { - retval = igraph_betweenness(&self->g, weights, &res, vs, PyObject_IsTrue(directed), PyObject_IsTrue(normalized)); + retval = igraph_betweenness(&self->g, weights, &res, vs, directed, normalized); } if (retval) { @@ -4367,8 +4383,8 @@ PyObject *igraphmodule_Graph_betweenness(igraphmodule_GraphObject * self, return NULL; } - if (igraph_betweenness_cutoff(&self->g, weights, &res, vs, PyObject_IsTrue(directed), - PyObject_IsTrue(normalized), PyFloat_AsDouble(cutoff_num))) { + if (igraph_betweenness_cutoff(&self->g, weights, &res, vs, directed, + normalized, PyFloat_AsDouble(cutoff_num))) { igraph_vs_destroy(&vs); igraph_vs_destroy(&targets); igraph_vs_destroy(&sources); @@ -4457,12 +4473,13 @@ PyObject *igraphmodule_Graph_biconnected_components(igraphmodule_GraphObject *se igraph_vector_int_t points; igraph_bool_t return_articulation_points; igraph_int_t no; - PyObject *result_o, *aps=Py_False; + int aps = 0; + PyObject *result_o; static char* kwlist[] = {"return_articulation_points", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &aps)) return NULL; - return_articulation_points = PyObject_IsTrue(aps); + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|p", kwlist, &aps)) return NULL; + return_articulation_points = aps; if (igraph_vector_int_list_init(&components, 0)) { igraphmodule_handle_igraph_error(); @@ -4504,7 +4521,8 @@ PyObject *igraphmodule_Graph_biconnected_components(igraphmodule_GraphObject *se */ PyObject *igraphmodule_Graph_bipartite_projection(igraphmodule_GraphObject * self, PyObject* args, PyObject* kwds) { - PyObject *types_o = Py_None, *multiplicity_o = Py_True, *mul1 = 0, *mul2 = 0; + int multiplicity_o = 1; + PyObject *types_o = Py_None, *mul1 = 0, *mul2 = 0; igraphmodule_GraphObject *result1 = 0, *result2 = 0; igraph_vector_bool_t* types = 0; igraph_vector_int_t multiplicities[2]; @@ -4514,7 +4532,7 @@ PyObject *igraphmodule_Graph_bipartite_projection(igraphmodule_GraphObject * sel static char* kwlist[] = {"types", "multiplicity", "probe1", "which", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Onn", kwlist, &types_o, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pnn", kwlist, &types_o, &multiplicity_o, &probe1, &which)) return NULL; @@ -4538,7 +4556,7 @@ PyObject *igraphmodule_Graph_bipartite_projection(igraphmodule_GraphObject * sel p_g1 = 0; } - if (PyObject_IsTrue(multiplicity_o)) { + if (multiplicity_o) { if (igraph_vector_int_init(&multiplicities[0], 0)) { if (types) { igraph_vector_bool_destroy(types); free(types); } igraphmodule_handle_igraph_error(); @@ -4587,7 +4605,7 @@ PyObject *igraphmodule_Graph_bipartite_projection(igraphmodule_GraphObject * sel } } - if (PyObject_IsTrue(multiplicity_o)) { + if (multiplicity_o) { if (p_g1) { mul1 = igraphmodule_vector_int_t_to_PyList(&multiplicities[0]); igraph_vector_int_destroy(&multiplicities[0]); @@ -4771,14 +4789,15 @@ PyObject *igraphmodule_Graph_closeness(igraphmodule_GraphObject * self, { static char *kwlist[] = { "vertices", "mode", "cutoff", "weights", "normalized", NULL }; + int normalized_o = 1; PyObject *vobj = Py_None, *list = NULL, *cutoff = Py_None, - *mode_o = Py_None, *weights_o = Py_None, *normalized_o = Py_True; + *mode_o = Py_None, *weights_o = Py_None; igraph_vector_t res, *weights = 0; igraph_neimode_t mode = IGRAPH_ALL; igraph_bool_t return_single = false; igraph_vs_t vs; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOO", kwlist, &vobj, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOp", kwlist, &vobj, &mode_o, &cutoff, &weights_o, &normalized_o)) return NULL; @@ -4802,7 +4821,7 @@ PyObject *igraphmodule_Graph_closeness(igraphmodule_GraphObject * self, if (cutoff == Py_None) { if (igraph_closeness(&self->g, &res, 0, 0, vs, mode, weights, - PyObject_IsTrue(normalized_o))) { + normalized_o)) { igraph_vs_destroy(&vs); igraph_vector_destroy(&res); if (weights) { igraph_vector_destroy(weights); free(weights); } @@ -4817,7 +4836,7 @@ PyObject *igraphmodule_Graph_closeness(igraphmodule_GraphObject * self, return NULL; } if (igraph_closeness_cutoff(&self->g, &res, NULL, NULL, vs, mode, weights, - PyObject_IsTrue(normalized_o), PyFloat_AsDouble(cutoff_num))) { + normalized_o, PyFloat_AsDouble(cutoff_num))) { igraph_vs_destroy(&vs); igraph_vector_destroy(&res); if (weights) { igraph_vector_destroy(weights); free(weights); } @@ -4851,14 +4870,15 @@ PyObject *igraphmodule_Graph_harmonic_centrality(igraphmodule_GraphObject * self { static char *kwlist[] = { "vertices", "mode", "cutoff", "weights", "normalized", NULL }; + int normalized_o = 1; PyObject *vobj = Py_None, *list = NULL, *cutoff = Py_None, - *mode_o = Py_None, *weights_o = Py_None, *normalized_o = Py_True; + *mode_o = Py_None, *weights_o = Py_None; igraph_vector_t res, *weights = 0; igraph_neimode_t mode = IGRAPH_ALL; igraph_bool_t return_single = false; igraph_vs_t vs; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOO", kwlist, &vobj, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOp", kwlist, &vobj, &mode_o, &cutoff, &weights_o, &normalized_o)) return NULL; @@ -4882,7 +4902,7 @@ PyObject *igraphmodule_Graph_harmonic_centrality(igraphmodule_GraphObject * self if (cutoff == Py_None) { if (igraph_harmonic_centrality(&self->g, &res, vs, mode, weights, - PyObject_IsTrue(normalized_o))) { + normalized_o)) { igraph_vs_destroy(&vs); igraph_vector_destroy(&res); if (weights) { igraph_vector_destroy(weights); free(weights); } @@ -4897,7 +4917,7 @@ PyObject *igraphmodule_Graph_harmonic_centrality(igraphmodule_GraphObject * self return NULL; } if (igraph_harmonic_centrality_cutoff(&self->g, &res, vs, mode, weights, - PyObject_IsTrue(normalized_o), PyFloat_AsDouble(cutoff_num))) { + normalized_o, PyFloat_AsDouble(cutoff_num))) { igraph_vs_destroy(&vs); igraph_vector_destroy(&res); if (weights) { igraph_vector_destroy(weights); free(weights); } @@ -5293,7 +5313,8 @@ PyObject *igraphmodule_Graph_edge_betweenness(igraphmodule_GraphObject * self, { static char *kwlist[] = { "directed", "cutoff", "weights", "sources", "targets", "normalized", NULL }; igraph_vector_t res, *weights = 0; - PyObject *list, *directed = Py_True, *cutoff = Py_None, *normalized = Py_False; + int directed = 1, normalized = 0; + PyObject *list, *cutoff = Py_None; PyObject *weights_o = Py_None; PyObject *sources_o = Py_None; PyObject *targets_o = Py_None; @@ -5302,7 +5323,7 @@ PyObject *igraphmodule_Graph_edge_betweenness(igraphmodule_GraphObject * self, igraph_error_t retval; igraph_bool_t is_subsetted = false; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pOOOOp", kwlist, &directed, &cutoff, &weights_o, &sources_o, &targets_o, &normalized)) return NULL; @@ -5337,12 +5358,12 @@ PyObject *igraphmodule_Graph_edge_betweenness(igraphmodule_GraphObject * self, retval = igraph_edge_betweenness_subset( &self->g, weights, &res, sources, targets, igraph_ess_all(IGRAPH_EDGEORDER_ID), - PyObject_IsTrue(directed), PyObject_IsTrue(normalized) + directed, normalized ); } else { retval = igraph_edge_betweenness( &self->g, weights, &res, igraph_ess_all(IGRAPH_EDGEORDER_ID), - PyObject_IsTrue(directed), PyObject_IsTrue(normalized) + directed, normalized ); } if (retval) { @@ -5374,7 +5395,7 @@ PyObject *igraphmodule_Graph_edge_betweenness(igraphmodule_GraphObject * self, if (igraph_edge_betweenness_cutoff( &self->g, weights, &res, igraph_ess_all(IGRAPH_EDGEORDER_ID), - PyObject_IsTrue(directed), PyObject_IsTrue(normalized), PyFloat_AsDouble(cutoff_num) + directed, normalized, PyFloat_AsDouble(cutoff_num) )) { igraph_vector_destroy(&res); igraph_vs_destroy(&targets); @@ -5409,11 +5430,12 @@ PyObject *igraphmodule_Graph_edge_betweenness(igraphmodule_GraphObject * self, PyObject *igraphmodule_Graph_edge_connectivity(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "source", "target", "checks", NULL }; - PyObject *checks = Py_True, *source_o = Py_None, *target_o = Py_None; + int checks = 1; + PyObject *source_o = Py_None, *target_o = Py_None; igraph_int_t source = -1, target = -1; igraph_int_t res; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, &source_o, &target_o, &checks)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOp", kwlist, &source_o, &target_o, &checks)) return NULL; if (igraphmodule_PyObject_to_optional_vid(source_o, &source, &self->g)) { @@ -5425,7 +5447,7 @@ PyObject *igraphmodule_Graph_edge_connectivity(igraphmodule_GraphObject *self, } if (source < 0 && target < 0) { - if (igraph_edge_connectivity(&self->g, &res, PyObject_IsTrue(checks))) { + if (igraph_edge_connectivity(&self->g, &res, checks)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -5451,19 +5473,19 @@ PyObject *igraphmodule_Graph_eigenvector_centrality( static char *kwlist[] = { "directed", "scale", "weights", "arpack_options", "return_eigenvalue", NULL }; - PyObject *directed_o = Py_True; + int directed_o = 1; PyObject *scale_o = Py_True; PyObject *weights_o = Py_None; PyObject *arpack_options_o = igraphmodule_arpack_options_default; igraphmodule_ARPACKOptionsObject *arpack_options; - PyObject *return_eigenvalue = Py_False; + int return_eigenvalue = 0; PyObject *res_o; igraph_real_t value; igraph_vector_t *weights=0, res; /* scale is deprecated but kept for backward compatibility reasons */ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO!O", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pOOO!p", kwlist, &directed_o, &scale_o, &weights_o, igraphmodule_ARPACKOptionsType, &arpack_options, &return_eigenvalue)) @@ -5479,7 +5501,7 @@ PyObject *igraphmodule_Graph_eigenvector_centrality( arpack_options = (igraphmodule_ARPACKOptionsObject*)arpack_options_o; if (igraph_eigenvector_centrality(&self->g, &res, &value, - PyObject_IsTrue(directed_o), + directed_o, weights, igraphmodule_ARPACKOptions_get(arpack_options))) { igraphmodule_handle_igraph_error(); if (weights) { igraph_vector_destroy(weights); free(weights); } @@ -5493,7 +5515,7 @@ PyObject *igraphmodule_Graph_eigenvector_centrality( igraph_vector_destroy(&res); if (res_o == NULL) return igraphmodule_handle_igraph_error(); - if (PyObject_IsTrue(return_eigenvalue)) { + if (return_eigenvalue) { PyObject *ev_o = igraphmodule_real_t_to_PyObject(value, IGRAPHMODULE_TYPE_FLOAT); if (ev_o == NULL) { Py_DECREF(res_o); @@ -6115,14 +6137,14 @@ PyObject *igraphmodule_Graph_hub_score( PyObject *scale_o = Py_True, *weights_o = Py_None; PyObject *arpack_options_o = igraphmodule_arpack_options_default; igraphmodule_ARPACKOptionsObject *arpack_options; - PyObject *return_eigenvalue = Py_False; + int return_eigenvalue = 0; PyObject *res_o; igraph_real_t value; igraph_vector_t res, *weights = 0; /* scale is deprecated but kept for backward compatibility reasons */ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO!O", kwlist, &weights_o, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO!p", kwlist, &weights_o, &scale_o, igraphmodule_ARPACKOptionsType, &arpack_options, &return_eigenvalue)) return NULL; @@ -6147,7 +6169,7 @@ PyObject *igraphmodule_Graph_hub_score( igraph_vector_destroy(&res); if (res_o == NULL) return igraphmodule_handle_igraph_error(); - if (PyObject_IsTrue(return_eigenvalue)) { + if (return_eigenvalue) { PyObject *ev_o = igraphmodule_real_t_to_PyObject(value, IGRAPHMODULE_TYPE_FLOAT); if (ev_o == NULL) { Py_DECREF(res_o); @@ -6413,7 +6435,7 @@ PyObject *igraphmodule_Graph_personalized_pagerank(igraphmodule_GraphObject *sel static char *kwlist[] = { "vertices", "directed", "damping", "reset", "reset_vertices", "weights", "arpack_options", "implementation", NULL }; - PyObject *directed = Py_True; + int directed = 1; PyObject *vobj = Py_None, *wobj = Py_None, *robj = Py_None, *rvsobj = Py_None; PyObject *list; PyObject *arpack_options_o = igraphmodule_arpack_options_default; @@ -6429,7 +6451,7 @@ PyObject *igraphmodule_Graph_personalized_pagerank(igraphmodule_GraphObject *sel void *opts; igraph_error_t retval; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOdOOOO!O", kwlist, &vobj, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OpdOOOO!O", kwlist, &vobj, &directed, &damping, &robj, &rvsobj, &wobj, igraphmodule_ARPACKOptionsType, @@ -6491,10 +6513,10 @@ PyObject *igraphmodule_Graph_personalized_pagerank(igraphmodule_GraphObject *sel if (rvsobj != Py_None) retval = igraph_personalized_pagerank_vs(&self->g, &weights, &res, 0, reset_vs, - damping, PyObject_IsTrue(directed), vs, algo, opts); + damping, directed, vs, algo, opts); else retval = igraph_personalized_pagerank(&self->g, &weights, &res, 0, reset, - damping, PyObject_IsTrue(directed), vs, algo, opts); + damping, directed, vs, algo, opts); if (retval) { igraphmodule_handle_igraph_error(); @@ -6527,17 +6549,18 @@ PyObject *igraphmodule_Graph_personalized_pagerank(igraphmodule_GraphObject *sel PyObject *igraphmodule_Graph_path_length_hist(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "directed", NULL }; - PyObject *directed = Py_True, *result_o; + int directed = 1; + PyObject *result_o; igraph_real_t unconn; igraph_vector_t res; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &directed)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|p", kwlist, &directed)) return NULL; if (igraph_vector_init(&res, 0)) return igraphmodule_handle_igraph_error(); - if (igraph_path_length_hist(&self->g, &res, &unconn, PyObject_IsTrue(directed))) { + if (igraph_path_length_hist(&self->g, &res, &unconn, directed)) { igraph_vector_destroy(&res); return igraphmodule_handle_igraph_error(); } @@ -6751,10 +6774,11 @@ PyObject *igraphmodule_Graph_similarity_jaccard(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { static char *kwlist[] = { "vertices", "pairs", "mode", "loops", NULL }; PyObject *vertices_o = Py_None, *pairs_o = Py_None; - PyObject *list = NULL, *loops = Py_True, *mode_o = Py_None; + int loops = 1; + PyObject *list = NULL, *mode_o = Py_None; igraph_neimode_t mode = IGRAPH_ALL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwlist, &vertices_o, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOp", kwlist, &vertices_o, &pairs_o, &mode_o, &loops)) return NULL; @@ -6789,7 +6813,7 @@ PyObject *igraphmodule_Graph_similarity_jaccard(igraphmodule_GraphObject * self, return igraphmodule_handle_igraph_error(); } - if (igraph_similarity_jaccard(&self->g, &res, vs_from, vs_to, mode, PyObject_IsTrue(loops))) { + if (igraph_similarity_jaccard(&self->g, &res, vs_from, vs_to, mode, loops)) { igraph_matrix_destroy(&res); igraph_vs_destroy(&vs_from); igraph_vs_destroy(&vs_to); @@ -6818,7 +6842,7 @@ PyObject *igraphmodule_Graph_similarity_jaccard(igraphmodule_GraphObject * self, } if (igraph_similarity_jaccard_pairs(&self->g, &res, &edges, mode, - PyObject_IsTrue(loops))) { + loops)) { igraph_vector_destroy(&res); if (edges_owned) { igraph_vector_int_destroy(&edges); @@ -6847,10 +6871,11 @@ PyObject *igraphmodule_Graph_similarity_dice(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { static char *kwlist[] = { "vertices", "pairs", "mode", "loops", NULL }; PyObject *vertices_o = Py_None, *pairs_o = Py_None; - PyObject *list = NULL, *loops = Py_True, *mode_o = Py_None; + int loops = 1; + PyObject *list = NULL, *mode_o = Py_None; igraph_neimode_t mode = IGRAPH_ALL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwlist, &vertices_o, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOp", kwlist, &vertices_o, &pairs_o, &mode_o, &loops)) return NULL; @@ -6885,7 +6910,7 @@ PyObject *igraphmodule_Graph_similarity_dice(igraphmodule_GraphObject * self, return igraphmodule_handle_igraph_error(); } - if (igraph_similarity_dice(&self->g, &res, vs_from, vs_to, mode, PyObject_IsTrue(loops))) { + if (igraph_similarity_dice(&self->g, &res, vs_from, vs_to, mode, loops)) { igraph_matrix_destroy(&res); igraph_vs_destroy(&vs_from); igraph_vs_destroy(&vs_to); @@ -6916,7 +6941,7 @@ PyObject *igraphmodule_Graph_similarity_dice(igraphmodule_GraphObject * self, } if (igraph_similarity_dice_pairs(&self->g, &res, &edges, mode, - PyObject_IsTrue(loops))) { + loops)) { igraph_vector_destroy(&res); if (edges_owned) { igraph_vector_int_destroy(&edges); @@ -7036,18 +7061,19 @@ PyObject *igraphmodule_Graph_simplify(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { static char *kwlist[] = { "multiple", "loops", "combine_edges", NULL }; - PyObject *multiple = Py_True, *loops = Py_True, *comb_o = Py_None; + int multiple = 1, loops = 1; + PyObject *comb_o = Py_None; igraph_attribute_combination_t comb; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ppO", kwlist, &multiple, &loops, &comb_o)) return NULL; if (igraphmodule_PyObject_to_attribute_combination_t(comb_o, &comb)) return NULL; - if (igraph_simplify(&self->g, PyObject_IsTrue(multiple), - PyObject_IsTrue(loops), &comb)) { + if (igraph_simplify(&self->g, multiple, + loops, &comb)) { igraph_attribute_combination_destroy(&comb); igraphmodule_handle_igraph_error(); return NULL; @@ -7144,15 +7170,16 @@ PyObject *igraphmodule_Graph_subgraph_edges(igraphmodule_GraphObject * self, igraph_es_t es; igraph_t sg; igraphmodule_GraphObject *result_o; - PyObject *list, *delete_vertices = Py_True; + int delete_vertices = 1; + PyObject *list; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, &list, &delete_vertices)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|p", kwlist, &list, &delete_vertices)) return NULL; if (igraphmodule_PyObject_to_es_t(list, &es, &self->g, 0)) return NULL; - if (igraph_subgraph_from_edges(&self->g, &sg, es, PyObject_IsTrue(delete_vertices))) { + if (igraph_subgraph_from_edges(&self->g, &sg, es, delete_vertices)) { igraphmodule_handle_igraph_error(); igraph_es_destroy(&es); return NULL; @@ -7302,27 +7329,27 @@ PyObject *igraphmodule_Graph_topological_sorting(igraphmodule_GraphObject * { static char *kwlist[] = { "mode", "warnings", NULL }; PyObject *list, *mode_o=Py_None; - PyObject *warnings_o=Py_True; + int warnings_o = 1; igraph_neimode_t mode = IGRAPH_OUT; igraph_vector_int_t res; igraph_warning_handler_t* old_handler = 0; igraph_error_t retval; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &mode_o, &warnings_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Op", kwlist, &mode_o, &warnings_o)) return NULL; if (igraphmodule_PyObject_to_neimode_t(mode_o, &mode)) return NULL; if (igraph_vector_int_init(&res, 0)) return igraphmodule_handle_igraph_error(); - if (!PyObject_IsTrue(warnings_o)) { + if (!warnings_o) { /* Turn off the warnings temporarily */ old_handler = igraph_set_warning_handler(igraph_warning_handler_ignore); } retval = igraph_topological_sorting(&self->g, &res, mode); - if (!PyObject_IsTrue(warnings_o)) { + if (!warnings_o) { /* Restore the warning handler */ igraph_set_warning_handler(old_handler); } @@ -7347,11 +7374,12 @@ PyObject *igraphmodule_Graph_topological_sorting(igraphmodule_GraphObject * PyObject *igraphmodule_Graph_vertex_connectivity(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "source", "target", "checks", "neighbors", NULL }; - PyObject *checks = Py_True, *neis = Py_None, *source_o = Py_None, *target_o = Py_None; + int checks = 1; + PyObject *neis = Py_None, *source_o = Py_None, *target_o = Py_None; igraph_int_t source = -1, target = -1, res; igraph_vconn_nei_t neighbors = IGRAPH_VCONN_NEI_ERROR; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOpO", kwlist, &source_o, &target_o, &checks, &neis)) return NULL; @@ -7364,7 +7392,7 @@ PyObject *igraphmodule_Graph_vertex_connectivity(igraphmodule_GraphObject *self, } if (source < 0 && target < 0) { - if (igraph_vertex_connectivity(&self->g, &res, PyObject_IsTrue(checks))) { + if (igraph_vertex_connectivity(&self->g, &res, checks)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -7395,15 +7423,16 @@ PyObject *igraphmodule_Graph_vertex_connectivity(igraphmodule_GraphObject *self, */ PyObject *igraphmodule_Graph_is_bipartite(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { - PyObject *types_o, *return_types_o = Py_False; + int return_types_o = 0; + PyObject *types_o; igraph_vector_bool_t types; igraph_bool_t return_types = false, res; static char *kwlist[] = { "return_types", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &return_types_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|p", kwlist, &return_types_o)) return NULL; - return_types = PyObject_IsTrue(return_types_o); + return_types = return_types_o; if (return_types) { if (igraph_vector_bool_init(&types, igraph_vcount(&self->g))) { @@ -7830,8 +7859,8 @@ PyObject *igraphmodule_Graph_minimum_cycle_basis( igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds ) { PyObject *cutoff_o = Py_None; - PyObject *complete_o = Py_True; - PyObject *use_cycle_order_o = Py_True; + int complete_o = 1; + int use_cycle_order_o = 1; PyObject *weights_o = Py_None; PyObject *result_o; igraph_int_t cutoff = -1; @@ -7840,7 +7869,7 @@ PyObject *igraphmodule_Graph_minimum_cycle_basis( static char *kwlist[] = { "cutoff", "complete", "use_cycle_order", "weights", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO", kwlist, &cutoff_o, &complete_o, &use_cycle_order_o, &weights_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OppO", kwlist, &cutoff_o, &complete_o, &use_cycle_order_o, &weights_o)) return NULL; if (cutoff_o != Py_None && igraphmodule_PyObject_to_integer_t(cutoff_o, &cutoff)) @@ -7859,8 +7888,8 @@ PyObject *igraphmodule_Graph_minimum_cycle_basis( } if (igraph_minimum_cycle_basis( - &self->g, weights, &result, cutoff, PyObject_IsTrue(complete_o), - PyObject_IsTrue(use_cycle_order_o) + &self->g, weights, &result, cutoff, complete_o, + use_cycle_order_o )) { igraph_vector_int_list_destroy(&result); if (weights) { @@ -9555,12 +9584,13 @@ PyObject *igraphmodule_Graph_Read_DIMACS(PyTypeObject * type, igraph_int_t source = 0, target = 0; igraph_vector_t capacity; igraph_t g; - PyObject *fname = NULL, *directed = Py_False, *capacity_obj; + int directed = 0; + PyObject *fname = NULL, *capacity_obj; static char *kwlist[] = { "f", "directed", NULL }; if (!PyArg_ParseTupleAndKeywords - (args, kwds, "O|O", kwlist, &fname, &directed)) + (args, kwds, "O|p", kwlist, &fname, &directed)) return NULL; if (igraphmodule_filehandle_init(&fobj, fname, "r")) @@ -9574,7 +9604,7 @@ PyObject *igraphmodule_Graph_Read_DIMACS(PyTypeObject * type, if (igraph_read_graph_dimacs_flow( &g, igraphmodule_filehandle_get(&fobj), 0, 0, &source, &target, - &capacity, PyObject_IsTrue(directed) + &capacity, directed )) { igraphmodule_handle_igraph_error(); igraph_vector_destroy(&capacity); @@ -9610,19 +9640,20 @@ PyObject *igraphmodule_Graph_Read_DL(PyTypeObject * type, igraphmodule_GraphObject *self; igraph_t g; igraphmodule_filehandle_t fobj; - PyObject *fname = NULL, *directed = Py_True; + int directed = 1; + PyObject *fname = NULL; static char *kwlist[] = { "f", "directed", NULL }; if (!PyArg_ParseTupleAndKeywords - (args, kwds, "O|O", kwlist, &fname, &directed)) + (args, kwds, "O|p", kwlist, &fname, &directed)) return NULL; if (igraphmodule_filehandle_init(&fobj, fname, "r")) return NULL; if (igraph_read_graph_dl(&g, igraphmodule_filehandle_get(&fobj), - PyObject_IsTrue(directed))) { + directed)) { igraphmodule_handle_igraph_error(); igraphmodule_filehandle_destroy(&fobj); return NULL; @@ -9643,13 +9674,14 @@ PyObject *igraphmodule_Graph_Read_Edgelist(PyTypeObject * type, PyObject * args, PyObject * kwds) { igraphmodule_GraphObject *self; - PyObject *directed = Py_True, *fname = NULL; + int directed = 1; + PyObject *fname = NULL; igraphmodule_filehandle_t fobj; igraph_t g; static char *kwlist[] = { "f", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|p", kwlist, &fname, &directed)) return NULL; @@ -9657,7 +9689,7 @@ PyObject *igraphmodule_Graph_Read_Edgelist(PyTypeObject * type, return NULL; if (igraph_read_graph_edgelist(&g, igraphmodule_filehandle_get(&fobj), - 0, PyObject_IsTrue(directed))) { + 0, directed)) { igraphmodule_handle_igraph_error(); igraphmodule_filehandle_destroy(&fobj); return NULL; @@ -9678,7 +9710,8 @@ PyObject *igraphmodule_Graph_Read_Ncol(PyTypeObject * type, PyObject * args, PyObject * kwds) { igraphmodule_GraphObject *self; - PyObject *names = Py_True, *weights = Py_None, *directed = Py_True; + int names = 1, directed = 1; + PyObject *weights = Py_None; PyObject *fname = NULL; igraphmodule_filehandle_t fobj; igraph_add_weights_t add_weights = IGRAPH_ADD_WEIGHTS_IF_PRESENT; @@ -9686,7 +9719,7 @@ PyObject *igraphmodule_Graph_Read_Ncol(PyTypeObject * type, PyObject * args, static char *kwlist[] = { "f", "names", "weights", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pOp", kwlist, &fname, &names, &weights, &directed)) return NULL; @@ -9697,8 +9730,8 @@ PyObject *igraphmodule_Graph_Read_Ncol(PyTypeObject * type, PyObject * args, return NULL; if (igraph_read_graph_ncol(&g, igraphmodule_filehandle_get(&fobj), 0, - PyObject_IsTrue(names), add_weights, - PyObject_IsTrue(directed))) { + names, add_weights, + directed)) { igraphmodule_handle_igraph_error(); igraphmodule_filehandle_destroy(&fobj); return NULL; @@ -9719,7 +9752,8 @@ PyObject *igraphmodule_Graph_Read_Lgl(PyTypeObject * type, PyObject * args, PyObject * kwds) { igraphmodule_GraphObject *self; - PyObject *names = Py_True, *weights = Py_None, *directed = Py_True; + int names = 1, directed = 1; + PyObject *weights = Py_None; PyObject *fname = NULL; igraphmodule_filehandle_t fobj; igraph_add_weights_t add_weights = IGRAPH_ADD_WEIGHTS_IF_PRESENT; @@ -9727,7 +9761,7 @@ PyObject *igraphmodule_Graph_Read_Lgl(PyTypeObject * type, PyObject * args, static char *kwlist[] = { "f", "names", "weights", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pOp", kwlist, &fname, &names, &weights, &directed)) return NULL; @@ -9744,8 +9778,8 @@ PyObject *igraphmodule_Graph_Read_Lgl(PyTypeObject * type, PyObject * args, return NULL; if (igraph_read_graph_lgl(&g, igraphmodule_filehandle_get(&fobj), - PyObject_IsTrue(names), add_weights, - PyObject_IsTrue(directed))) { + names, add_weights, + directed)) { igraphmodule_handle_igraph_error(); igraphmodule_filehandle_destroy(&fobj); return NULL; @@ -9832,20 +9866,21 @@ PyObject *igraphmodule_Graph_Read_GraphDB(PyTypeObject * type, PyObject * args, PyObject * kwds) { igraphmodule_GraphObject *self; - PyObject *fname = NULL, *directed_o = Py_False; + int directed_o = 0; + PyObject *fname = NULL; igraph_t g; igraphmodule_filehandle_t fobj; static char *kwlist[] = { "f", "directed", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, &fname, &directed_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|p", kwlist, &fname, &directed_o)) return NULL; if (igraphmodule_filehandle_init(&fobj, fname, "r")) return NULL; if (igraph_read_graph_graphdb(&g, igraphmodule_filehandle_get(&fobj), - PyObject_IsTrue(directed_o))) { + directed_o)) { igraphmodule_handle_igraph_error(); igraphmodule_filehandle_destroy(&fobj); return NULL; @@ -10127,12 +10162,12 @@ PyObject *igraphmodule_Graph_write_lgl(igraphmodule_GraphObject * self, PyObject *fname = NULL; char *names = "name"; char *weights = "weight"; - PyObject *isolates = Py_True; + int isolates = 1; igraphmodule_filehandle_t fobj; static char *kwlist[] = { "f", "names", "weights", "isolates", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zzO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zzp", kwlist, &fname, &names, &weights, &isolates)) return NULL; @@ -10140,7 +10175,7 @@ PyObject *igraphmodule_Graph_write_lgl(igraphmodule_GraphObject * self, return NULL; if (igraph_write_graph_lgl(&self->g, igraphmodule_filehandle_get(&fobj), - names, weights, PyObject_IsTrue(isolates))) { + names, weights, isolates)) { igraphmodule_handle_igraph_error(); igraphmodule_filehandle_destroy(&fobj); return NULL; @@ -10185,18 +10220,19 @@ PyObject *igraphmodule_Graph_write_pajek(igraphmodule_GraphObject * self, PyObject *igraphmodule_Graph_write_graphml(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { - PyObject *fname = NULL, *prefixattr_o = Py_True; + int prefixattr_o = 1; + PyObject *fname = NULL; static char *kwlist[] = { "f", "prefixattr", NULL }; igraphmodule_filehandle_t fobj; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, &fname, &prefixattr_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|p", kwlist, &fname, &prefixattr_o)) return NULL; if (igraphmodule_filehandle_init(&fobj, fname, "w")) return NULL; if (igraph_write_graph_graphml( - &self->g, igraphmodule_filehandle_get(&fobj), PyObject_IsTrue(prefixattr_o) + &self->g, igraphmodule_filehandle_get(&fobj), prefixattr_o )) { igraphmodule_handle_igraph_error(); igraphmodule_filehandle_destroy(&fobj); @@ -10458,7 +10494,8 @@ PyObject *igraphmodule_Graph_isomorphic_bliss(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { igraph_bool_t res = false; - PyObject *o=Py_None, *return1=Py_False, *return2=Py_False; + int return1 = 0, return2 = 0; + PyObject *o=Py_None; PyObject *sho1=Py_None, *sho2=Py_None; PyObject *color1_o=Py_None, *color2_o=Py_None; igraphmodule_GraphObject *other; @@ -10471,7 +10508,7 @@ PyObject *igraphmodule_Graph_isomorphic_bliss(igraphmodule_GraphObject * self, "return_mapping_21", "sh1", "sh2", "color1", "color2", NULL }; /* TODO: convert igraph_bliss_info_t when needed */ if (!PyArg_ParseTupleAndKeywords - (args, kwds, "|O!OOOOOO", kwlist, igraphmodule_GraphType, &o, + (args, kwds, "|O!ppOOOO", kwlist, igraphmodule_GraphType, &o, &return1, &return2, &sho1, &sho2, &color1_o, &color2_o)) return NULL; if (igraphmodule_PyObject_to_bliss_sh_t(sho1, &sh1)) return NULL; @@ -10490,11 +10527,11 @@ PyObject *igraphmodule_Graph_isomorphic_bliss(igraphmodule_GraphObject * self, if (o == Py_None) other = self; else other = (igraphmodule_GraphObject *) o; - if (PyObject_IsTrue(return1)) { + if (return1) { igraph_vector_int_init(&mapping_12, 0); map12 = &mapping_12; } - if (PyObject_IsTrue(return2)) { + if (return2) { igraph_vector_int_init(&mapping_21, 0); map21 = &mapping_21; } @@ -10649,7 +10686,8 @@ PyObject *igraphmodule_Graph_isomorphic_vf2(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { igraph_bool_t res = false; - PyObject *o=Py_None, *return1=Py_False, *return2=Py_False; + int return1 = 0, return2 = 0; + PyObject *o=Py_None; PyObject *color1_o=Py_None, *color2_o=Py_None; PyObject *edge_color1_o=Py_None, *edge_color2_o=Py_None; PyObject *callback_fn=Py_None; @@ -10668,7 +10706,7 @@ PyObject *igraphmodule_Graph_isomorphic_vf2(igraphmodule_GraphObject * self, }; if (!PyArg_ParseTupleAndKeywords - (args, kwds, "|O!OOOOOOOOO", kwlist, igraphmodule_GraphType, &o, + (args, kwds, "|O!OOOOppOOO", kwlist, igraphmodule_GraphType, &o, &color1_o, &color2_o, &edge_color1_o, &edge_color2_o, &return1, &return2, &callback_fn, &node_compat_fn, &edge_compat_fn)) return NULL; @@ -10714,11 +10752,11 @@ PyObject *igraphmodule_Graph_isomorphic_vf2(igraphmodule_GraphObject * self, return NULL; } - if (PyObject_IsTrue(return1)) { + if (return1) { igraph_vector_int_init(&mapping_12, 0); map12 = &mapping_12; } - if (PyObject_IsTrue(return2)) { + if (return2) { igraph_vector_int_init(&mapping_21, 0); map21 = &mapping_21; } @@ -10986,7 +11024,8 @@ PyObject *igraphmodule_Graph_subisomorphic_vf2(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { igraph_bool_t res = false; - PyObject *o, *return1=Py_False, *return2=Py_False; + int return1 = 0, return2 = 0; + PyObject *o; PyObject *color1_o=Py_None, *color2_o=Py_None; PyObject *edge_color1_o=Py_None, *edge_color2_o=Py_None; PyObject *callback_fn=Py_None; @@ -11004,7 +11043,7 @@ PyObject *igraphmodule_Graph_subisomorphic_vf2(igraphmodule_GraphObject * self, NULL }; if (!PyArg_ParseTupleAndKeywords - (args, kwds, "O!|OOOOOOOOO", kwlist, igraphmodule_GraphType, &o, + (args, kwds, "O!|OOOOppOOO", kwlist, igraphmodule_GraphType, &o, &color1_o, &color2_o, &edge_color1_o, &edge_color2_o, &return1, &return2, &callback_fn, &node_compat_fn, &edge_compat_fn)) return NULL; @@ -11047,11 +11086,11 @@ PyObject *igraphmodule_Graph_subisomorphic_vf2(igraphmodule_GraphObject * self, return NULL; } - if (PyObject_IsTrue(return1)) { + if (return1) { igraph_vector_int_init(&mapping_12, 0); map12 = &mapping_12; } - if (PyObject_IsTrue(return2)) { + if (return2) { igraph_vector_int_init(&mapping_21, 0); map21 = &mapping_21; } @@ -11314,7 +11353,8 @@ PyObject *igraphmodule_Graph_subisomorphic_lad(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { igraph_bool_t res = false; - PyObject *o, *return_mapping=Py_False, *domains_o=Py_None, *induced=Py_False; + int return_mapping = 0, induced = 0; + PyObject *o, *domains_o=Py_None; igraphmodule_GraphObject *other; igraph_vector_int_list_t domains; igraph_vector_int_list_t* p_domains = 0; @@ -11324,7 +11364,7 @@ PyObject *igraphmodule_Graph_subisomorphic_lad(igraphmodule_GraphObject * self, "pattern", "domains", "induced", "return_mapping", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Opp", kwlist, igraphmodule_GraphType, &o, &domains_o, &induced, &return_mapping)) return NULL; @@ -11338,7 +11378,7 @@ PyObject *igraphmodule_Graph_subisomorphic_lad(igraphmodule_GraphObject * self, p_domains = &domains; } - if (PyObject_IsTrue(return_mapping)) { + if (return_mapping) { if (igraph_vector_int_init(&mapping, 0)) { if (p_domains) { igraph_vector_int_list_destroy(p_domains); @@ -11350,7 +11390,7 @@ PyObject *igraphmodule_Graph_subisomorphic_lad(igraphmodule_GraphObject * self, } if (igraph_subisomorphic_lad(&other->g, &self->g, p_domains, &res, - map, 0, PyObject_IsTrue(induced))) { + map, 0, induced)) { if (p_domains) { igraph_vector_int_list_destroy(p_domains); } @@ -11383,7 +11423,8 @@ PyObject *igraphmodule_Graph_subisomorphic_lad(igraphmodule_GraphObject * self, PyObject *igraphmodule_Graph_get_subisomorphisms_lad( igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { - PyObject *o, *domains_o=Py_None, *induced=Py_False, *result_o; + int induced = 0; + PyObject *o, *domains_o=Py_None, *result_o; igraphmodule_GraphObject *other; igraph_vector_int_list_t domains; igraph_vector_int_list_t* p_domains = 0; @@ -11391,7 +11432,7 @@ PyObject *igraphmodule_Graph_get_subisomorphisms_lad( static char *kwlist[] = { "pattern", "domains", "induced", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|OO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Op", kwlist, igraphmodule_GraphType, &o, &domains_o, &induced)) return NULL; @@ -11412,7 +11453,7 @@ PyObject *igraphmodule_Graph_get_subisomorphisms_lad( } if (igraph_subisomorphic_lad( - &other->g, &self->g, p_domains, 0, 0, &mappings, PyObject_IsTrue(induced) + &other->g, &self->g, p_domains, 0, 0, &mappings, induced )) { igraphmodule_handle_igraph_error(); igraph_vector_int_list_destroy(&mappings); @@ -11613,12 +11654,12 @@ PyObject *igraphmodule_Graph_complementer(igraphmodule_GraphObject * self, { static char *kwlist[] = { "loops", NULL }; igraphmodule_GraphObject *result_o; - PyObject *o = Py_True; + int o = 1; igraph_t g; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|p", kwlist, &o)) return NULL; - if (igraph_complementer(&g, &self->g, PyObject_IsTrue(o))) { + if (igraph_complementer(&g, &self->g, o)) { igraphmodule_handle_igraph_error(); return NULL; } @@ -11793,14 +11834,15 @@ PyObject *igraphmodule_Graph_bfsiter(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { char *kwlist[] = { "vid", "mode", "advanced", NULL }; - PyObject *root, *adv = Py_False, *mode_o = Py_None; + int adv = 0; + PyObject *root, *mode_o = Py_None; igraph_neimode_t mode = IGRAPH_OUT; if (!PyArg_ParseTupleAndKeywords - (args, kwds, "O|OO", kwlist, &root, &mode_o, &adv)) + (args, kwds, "O|Op", kwlist, &root, &mode_o, &adv)) return NULL; if (igraphmodule_PyObject_to_neimode_t(mode_o, &mode)) return NULL; - return igraphmodule_BFSIter_new(self, root, mode, PyObject_IsTrue(adv)); + return igraphmodule_BFSIter_new(self, root, mode, adv); } /** \ingroup python_interface_graph @@ -11876,14 +11918,15 @@ PyObject *igraphmodule_Graph_dfsiter(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { char *kwlist[] = { "vid", "mode", "advanced", NULL }; - PyObject *root, *adv = Py_False, *mode_o = Py_None; + int adv = 0; + PyObject *root, *mode_o = Py_None; igraph_neimode_t mode = IGRAPH_OUT; if (!PyArg_ParseTupleAndKeywords - (args, kwds, "O|OO", kwlist, &root, &mode_o, &adv)) + (args, kwds, "O|Op", kwlist, &root, &mode_o, &adv)) return NULL; if (igraphmodule_PyObject_to_neimode_t(mode_o, &mode)) return NULL; - return igraphmodule_DFSIter_new(self, root, mode, PyObject_IsTrue(adv)); + return igraphmodule_DFSIter_new(self, root, mode, adv); } /********************************************************************** @@ -13098,9 +13141,9 @@ PyObject *igraphmodule_Graph_modularity(igraphmodule_GraphObject *self, double resolution = 1; igraph_real_t modularity; PyObject *mvec, *wvec=Py_None; - PyObject *directed = Py_True; + int directed = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OdO", kwlist, &mvec, &wvec, &resolution, &directed)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Odp", kwlist, &mvec, &wvec, &resolution, &directed)) return NULL; if (igraphmodule_PyObject_to_vector_int_t(mvec, &membership)) @@ -13111,7 +13154,7 @@ PyObject *igraphmodule_Graph_modularity(igraphmodule_GraphObject *self, return NULL; } - if (igraph_modularity(&self->g, &membership, weights, resolution, PyObject_IsTrue(directed), &modularity)) { + if (igraph_modularity(&self->g, &membership, weights, resolution, directed, &modularity)) { igraph_vector_int_destroy(&membership); if (weights) { igraph_vector_destroy(weights); free(weights); @@ -13137,10 +13180,10 @@ PyObject *igraphmodule_Graph_modularity_matrix(igraphmodule_GraphObject *self, double resolution = 1; igraph_matrix_t result; PyObject *wvec = Py_None; - PyObject *directed = Py_True; + int directed = 1; PyObject *result_o; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OdO", kwlist, &wvec, &resolution, &directed)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Odp", kwlist, &wvec, &resolution, &directed)) return NULL; if (igraphmodule_attrib_to_vector_t(wvec, self, &weights, ATTRIBUTE_TYPE_EDGE)) @@ -13153,7 +13196,7 @@ PyObject *igraphmodule_Graph_modularity_matrix(igraphmodule_GraphObject *self, return NULL; } - if (igraph_modularity_matrix(&self->g, weights, resolution, &result, PyObject_IsTrue(directed))) { + if (igraph_modularity_matrix(&self->g, weights, resolution, &result, directed)) { igraph_matrix_destroy(&result); if (weights) { igraph_vector_destroy(weights); free(weights); @@ -13177,14 +13220,14 @@ PyObject *igraphmodule_Graph_modularity_matrix(igraphmodule_GraphObject *self, */ PyObject *igraphmodule_Graph_community_edge_betweenness(igraphmodule_GraphObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "directed", "weights", NULL }; - PyObject *directed = Py_True; + int directed = 1; PyObject *weights_o = Py_None; PyObject *res, *qs, *ms; igraph_matrix_int_t merges; igraph_vector_t q; igraph_vector_t *weights = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &directed, &weights_o)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|pO", kwlist, &directed, &weights_o)) { return NULL; } @@ -13214,7 +13257,7 @@ PyObject *igraphmodule_Graph_community_edge_betweenness(igraphmodule_GraphObject /* bridges = */ NULL, /* modularity = */ &q, /* membership = */ NULL, - PyObject_IsTrue(directed), + directed, weights, /* lengths = */ NULL)) { @@ -13524,7 +13567,7 @@ PyObject *igraphmodule_Graph_community_multilevel(igraphmodule_GraphObject *self PyObject *args, PyObject *kwds) { static char *kwlist[] = { "weights", "return_levels", "resolution", NULL }; - PyObject *return_levels = Py_False; + int return_levels = 0; PyObject *mss, *qs, *res, *weights = Py_None; igraph_matrix_int_t memberships; igraph_vector_int_t membership; @@ -13532,7 +13575,7 @@ PyObject *igraphmodule_Graph_community_multilevel(igraphmodule_GraphObject *self double resolution = 1; igraph_vector_t *ws; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOd", kwlist, &weights, &return_levels, &resolution)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Opd", kwlist, &weights, &return_levels, &resolution)) { return NULL; } @@ -13562,7 +13605,7 @@ PyObject *igraphmodule_Graph_community_multilevel(igraphmodule_GraphObject *self return NULL; } - if (PyObject_IsTrue(return_levels)) { + if (return_levels) { mss=igraphmodule_matrix_int_t_to_PyList(&memberships); if (!mss) { res = mss; @@ -13639,7 +13682,7 @@ PyObject *igraphmodule_Graph_community_spinglass(igraphmodule_GraphObject *self, "start_temp", "stop_temp", "cool_fact", "update_rule", "gamma", "implementation", "lambda_", NULL}; PyObject *weights_o = Py_None; - PyObject *parupdate_o = Py_False; + int parupdate_o = 0; PyObject *update_rule_o = Py_None; PyObject *impl_o = Py_None; PyObject *res; @@ -13655,7 +13698,7 @@ PyObject *igraphmodule_Graph_community_spinglass(igraphmodule_GraphObject *self, igraph_vector_t *weights = 0; igraph_vector_int_t membership; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OnOdddOdOd", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OnpdddOdOd", kwlist, &weights_o, &spins, &parupdate_o, &start_temp, &stop_temp, &cool_fact, &update_rule_o, &gamma, &impl_o, &lambda)) return NULL; @@ -13683,7 +13726,7 @@ PyObject *igraphmodule_Graph_community_spinglass(igraphmodule_GraphObject *self, if (igraph_community_spinglass(&self->g, weights, 0, 0, &membership, 0, spins, - PyObject_IsTrue(parupdate_o), + parupdate_o, start_temp, stop_temp, cool_fact, update_rule, gamma, impl, lambda)) { igraphmodule_handle_igraph_error(); @@ -13774,7 +13817,7 @@ PyObject *igraphmodule_Graph_community_leiden(igraphmodule_GraphObject *self, PyObject *node_weights_o = Py_None; PyObject *node_in_weights_o = Py_None; PyObject *initial_membership_o = Py_None; - PyObject *normalize_resolution = Py_False; + int normalize_resolution = 0; PyObject *res = Py_None; int error = 0; @@ -13787,7 +13830,7 @@ PyObject *igraphmodule_Graph_community_leiden(igraphmodule_GraphObject *self, igraph_int_t nb_clusters = 0; igraph_real_t quality = 0.0; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOdOdOn", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOdpdOn", kwlist, &edge_weights_o, &node_weights_o, &node_in_weights_o, &resolution, &normalize_resolution, &beta, &initial_membership_o, &n_iterations)) return NULL; @@ -13837,7 +13880,7 @@ PyObject *igraphmodule_Graph_community_leiden(igraphmodule_GraphObject *self, } } - if (!error && PyObject_IsTrue(normalize_resolution)) + if (!error && normalize_resolution) { /* If we need to normalize the resolution parameter, * we will need to have node weights. */ @@ -14202,7 +14245,8 @@ PyObject *igraphmodule_Graph_random_walk(igraphmodule_GraphObject * self, PyObject *igraphmodule_Graph_Nearest_Neighbor_Graph(PyTypeObject *type, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"points", "k", "r", "metric", "directed", NULL}; - PyObject *points_o = Py_None, *metric_o = Py_None, *directed_o = Py_False; + int directed_o = 0; + PyObject *points_o = Py_None, *metric_o = Py_None; double r = -1; Py_ssize_t k = 1; igraph_matrix_t points; @@ -14210,7 +14254,7 @@ PyObject *igraphmodule_Graph_Nearest_Neighbor_Graph(PyTypeObject *type, igraph_t graph; igraph_metric_t metric = IGRAPH_METRIC_EUCLIDEAN; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ndOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ndOp", kwlist, &points_o, &k, &r, &metric_o, &directed_o)) { return NULL; } @@ -14223,7 +14267,7 @@ PyObject *igraphmodule_Graph_Nearest_Neighbor_Graph(PyTypeObject *type, return NULL; } - if (igraph_nearest_neighbor_graph(&graph, &points, metric, k, r, PyObject_IsTrue(directed_o))) { + if (igraph_nearest_neighbor_graph(&graph, &points, metric, k, r, directed_o)) { igraph_matrix_destroy(&points); return igraphmodule_handle_igraph_error(); } diff --git a/src/_igraph/igraphmodule.c b/src/_igraph/igraphmodule.c index 948ff0c83..ffb7ca715 100644 --- a/src/_igraph/igraphmodule.c +++ b/src/_igraph/igraphmodule.c @@ -261,13 +261,14 @@ PyObject* igraphmodule_align_layout(PyObject* self, PyObject* args, PyObject* kw PyObject* igraphmodule_convex_hull(PyObject* self, PyObject* args, PyObject* kwds) { static char* kwlist[] = {"vs", "coords", NULL}; - PyObject *vs, *o, *o1 = 0, *o2 = 0, *o1_float, *o2_float, *coords = Py_False; + int coords = 0; + PyObject *vs, *o, *o1 = 0, *o2 = 0, *o1_float, *o2_float; igraph_matrix_t mtrx; igraph_vector_int_t result; igraph_matrix_t resmat; Py_ssize_t no_of_nodes, i; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|O", kwlist, &PyList_Type, &vs, &coords)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|p", kwlist, &PyList_Type, &vs, &coords)) return NULL; no_of_nodes = PyList_Size(vs); @@ -339,7 +340,7 @@ PyObject* igraphmodule_convex_hull(PyObject* self, PyObject* args, PyObject* kwd Py_DECREF(o2_float); } - if (!PyObject_IsTrue(coords)) { + if (!coords) { if (igraph_vector_int_init(&result, 0)) { igraphmodule_handle_igraph_error(); igraph_matrix_destroy(&mtrx); @@ -378,12 +379,13 @@ PyObject* igraphmodule_convex_hull(PyObject* self, PyObject* args, PyObject* kwd PyObject* igraphmodule_community_to_membership(PyObject *self, PyObject *args, PyObject *kwds) { static char* kwlist[] = { "merges", "nodes", "steps", "return_csize", NULL }; - PyObject *merges_o, *return_csize = Py_False, *result_o; + int return_csize = 0; + PyObject *merges_o, *result_o; igraph_matrix_int_t merges; igraph_vector_int_t result, csize, *csize_p = 0; Py_ssize_t nodes, steps; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "Onn|O", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "Onn|p", kwlist, &merges_o, &nodes, &steps, &return_csize)) return NULL; if (igraphmodule_PyObject_to_matrix_int_t_with_minimum_column_count(merges_o, &merges, 2, "merges")) { @@ -399,7 +401,7 @@ PyObject* igraphmodule_community_to_membership(PyObject *self, return NULL; } - if (PyObject_IsTrue(return_csize)) { + if (return_csize) { igraph_vector_int_init(&csize, 0); csize_p = &csize; } @@ -566,7 +568,7 @@ static PyObject* igraphmodule_i_is_graphical_or_bigraphical( static char* kwlist_graphical[] = { "out_deg", "in_deg", "loops", "multiple", NULL }; static char* kwlist_bigraphical[] = { "degrees1", "degrees2", "multiple", NULL }; PyObject *out_deg_o = 0, *in_deg_o = 0; - PyObject *loops = Py_False, *multiple = Py_False; + int loops = 0, multiple = 0; igraph_vector_int_t out_deg, in_deg; igraph_bool_t is_directed, result; int allowed_edge_types; @@ -574,14 +576,14 @@ static PyObject* igraphmodule_i_is_graphical_or_bigraphical( if (is_bigraphical) { if (!PyArg_ParseTupleAndKeywords( - args, kwds, "OO|O", kwlist_bigraphical, + args, kwds, "OO|p", kwlist_bigraphical, &out_deg_o, &in_deg_o, &multiple )) { return NULL; } } else { if (!PyArg_ParseTupleAndKeywords( - args, kwds, "O|OOO", kwlist_graphical, + args, kwds, "O|Opp", kwlist_graphical, &out_deg_o, &in_deg_o, &loops, &multiple )) { return NULL; @@ -599,10 +601,10 @@ static PyObject* igraphmodule_i_is_graphical_or_bigraphical( } allowed_edge_types = IGRAPH_SIMPLE_SW; - if (PyObject_IsTrue(loops)) { + if (loops) { allowed_edge_types |= IGRAPH_LOOPS_SW; } - if (PyObject_IsTrue(multiple)) { + if (multiple) { allowed_edge_types |= IGRAPH_MULTI_SW; } @@ -633,20 +635,21 @@ static PyObject* igraphmodule_i_is_graphical_or_bigraphical( PyObject* igraphmodule_power_law_fit(PyObject *self, PyObject *args, PyObject *kwds) { static char* kwlist[] = { "data", "xmin", "force_continuous", "p_precision", NULL }; - PyObject *data_o, *force_continuous_o = Py_False; + int force_continuous_o = 0; + PyObject *data_o; igraph_vector_t data; igraph_plfit_result_t result; double xmin = -1, p_precision = 0.01; igraph_real_t p; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|dOd", kwlist, &data_o, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|dpd", kwlist, &data_o, &xmin, &force_continuous_o, &p_precision)) return NULL; if (igraphmodule_PyObject_float_to_vector_t(data_o, &data)) return NULL; - if (igraph_power_law_fit(&data, &result, xmin, PyObject_IsTrue(force_continuous_o))) { + if (igraph_power_law_fit(&data, &result, xmin, force_continuous_o)) { igraphmodule_handle_igraph_error(); igraph_vector_destroy(&data); return NULL; diff --git a/tests/test_bool_arguments.py b/tests/test_bool_arguments.py new file mode 100644 index 000000000..3aa563023 --- /dev/null +++ b/tests/test_bool_arguments.py @@ -0,0 +1,62 @@ +import unittest + +from igraph import Graph, is_graphical + +from .utils import BadBool + +try: + import numpy as np +except ImportError: + np = None + + +class BooleanArgumentTests(unittest.TestCase): + """Tests for the conversion of boolean arguments in the C extension.""" + + def setUp(self): + self.g = Graph.Ring(5) + + def testTruthyValuesAreAccepted(self): + # Boolean arguments accept any object, using Python's truth testing + g = self.g + self.assertEqual(g.degree(loops=0), g.degree(loops=False)) + self.assertTrue(Graph.Ring(5, directed=1).is_directed()) + self.assertTrue(Graph.Ring(5, directed="yes").is_directed()) + self.assertFalse(Graph.Ring(5, directed=[]).is_directed()) + self.assertFalse(Graph.Ring(5, directed=None).is_directed()) + + def testConversionErrorsArePropagated(self): + # Errors raised while converting an argument to a boolean must be + # propagated as they are + g = self.g + bad = BadBool() + self.assertRaises(ValueError, g.degree, loops=bad) + self.assertRaises(ValueError, g.copy().simplify, multiple=bad) + self.assertRaises(ValueError, g.betweenness, directed=bad) + self.assertRaises(ValueError, g.closeness, normalized=bad) + self.assertRaises(ValueError, Graph.Ring, 5, directed=bad) + self.assertRaises(ValueError, Graph.Erdos_Renyi, 5, 0.5, loops=bad) + self.assertRaises(ValueError, is_graphical, [1, 1], loops=bad) + + @unittest.skipIf(np is None, "test case depends on NumPy") + def testNumPyArrayArgument(self): + # Multi-element NumPy arrays do not have a truth value + flags = np.array([True, False]) + self.assertRaises(ValueError, self.g.degree, loops=flags) + self.assertRaises(ValueError, self.g.copy().simplify, multiple=flags) + # Single-element arrays and NumPy scalars do + self.assertEqual(self.g.degree(loops=np.bool_(True)), [2] * 5) + self.assertEqual(self.g.degree(loops=np.array([True])), [2] * 5) + + +def suite(): + return unittest.defaultTestLoader.loadTestsFromTestCase(BooleanArgumentTests) + + +def test(): + runner = unittest.TextTestRunner() + runner.run(suite()) + + +if __name__ == "__main__": + test() diff --git a/tests/utils.py b/tests/utils.py index 19b177181..bc1d5ee1c 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -52,3 +52,14 @@ def temporary_file(content=None, mode=None, binary=False): is_pypy = platform.python_implementation() == "PyPy" + + +class BadBool: + """Object whose conversion to a boolean raises an exception. + + Used to test that errors raised while converting a Python object to a + boolean are propagated correctly from the C extension. + """ + + def __bool__(self): + raise ValueError("Cannot convert to boolean.") From 618d57c494003f392fda0a6f66c613a0191e62ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sat, 28 Nov 2026 15:55:45 +0000 Subject: [PATCH 2/7] fix: parse the edgemaps argument of _union() and _intersection() with "p" The optional edgemaps argument was parsed with "O" into a variable that had no default value, so it was read uninitialized when the argument was omitted, and errors from PyObject_IsTrue() were ignored. Parse it with "p" directly into the existing with_edgemaps flag, which defaults to 0. Assisted-by: Claude Opus 5.5 --- src/_igraph/operators.c | 18 ++++++------------ tests/test_bool_arguments.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/_igraph/operators.c b/src/_igraph/operators.c index 19199633a..1a2cb034a 100644 --- a/src/_igraph/operators.c +++ b/src/_igraph/operators.c @@ -93,7 +93,7 @@ PyObject *igraphmodule__union(PyObject *self, PyObject *args, PyObject *kwds) { static char* kwlist[] = { "graphs", "edgemaps", NULL }; - PyObject *it, *em_list = 0, *graphs, *with_edgemaps_o; + PyObject *it, *em_list = 0, *graphs; int with_edgemaps = 0; Py_ssize_t i, j, no_of_graphs; igraph_vector_ptr_t gs; @@ -102,13 +102,10 @@ PyObject *igraphmodule__union(PyObject *self, PyTypeObject *result_type; igraph_t g; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, - &graphs, &with_edgemaps_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|p", kwlist, + &graphs, &with_edgemaps)) return NULL; - if (PyObject_IsTrue(with_edgemaps_o)) - with_edgemaps = 1; - /* Needs to be an iterable */ it = PyObject_GetIter(graphs); if (!it) { @@ -218,7 +215,7 @@ PyObject *igraphmodule__intersection(PyObject *self, PyObject *args, PyObject *kwds) { static char* kwlist[] = { "graphs", "edgemaps", NULL }; - PyObject *it, *em_list = 0, *graphs, *with_edgemaps_o; + PyObject *it, *em_list = 0, *graphs; int with_edgemaps = 0; Py_ssize_t i, j, no_of_graphs; igraph_vector_ptr_t gs; @@ -227,13 +224,10 @@ PyObject *igraphmodule__intersection(PyObject *self, PyTypeObject *result_type; igraph_t g; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, - &graphs, &with_edgemaps_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|p", kwlist, + &graphs, &with_edgemaps)) return NULL; - if (PyObject_IsTrue(with_edgemaps_o)) - with_edgemaps = 1; - /* Needs to be an iterable */ it = PyObject_GetIter(graphs); if (!it) { diff --git a/tests/test_bool_arguments.py b/tests/test_bool_arguments.py index 3aa563023..9190876b4 100644 --- a/tests/test_bool_arguments.py +++ b/tests/test_bool_arguments.py @@ -1,6 +1,7 @@ import unittest from igraph import Graph, is_graphical +from igraph._igraph import _intersection, _union from .utils import BadBool @@ -38,6 +39,16 @@ def testConversionErrorsArePropagated(self): self.assertRaises(ValueError, Graph.Erdos_Renyi, 5, 0.5, loops=bad) self.assertRaises(ValueError, is_graphical, [1, 1], loops=bad) + def testUnionIntersectionEdgemaps(self): + g = self.g + self.assertEqual(_union([g, g], False).ecount(), 5) + self.assertEqual(_intersection([g, g], False).ecount(), 5) + # The edgemaps argument is optional + self.assertEqual(_union([g, g]).ecount(), 5) + self.assertEqual(_intersection([g, g]).ecount(), 5) + self.assertRaises(ValueError, _union, [g, g], BadBool()) + self.assertRaises(ValueError, _intersection, [g, g], BadBool()) + @unittest.skipIf(np is None, "test case depends on NumPy") def testNumPyArrayArgument(self): # Multi-element NumPy arrays do not have a truth value From 2cffb7d3012b9149cb2efa29036557f3c59d513b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sat, 28 Nov 2026 15:55:50 +0000 Subject: [PATCH 3/7] chore: update changelog Assisted-by: Claude Opus 5.5 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50a259f12..b2b0059ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ - Dropped support for Python 3.9 and 3.10 as they have reached their end of life. The minimum supported Python version is now 3.11. +### Fixed + +- Errors raised while converting a boolean argument to a boolean (e.g. when + passing a NumPy array with more than one element) are now propagated + correctly instead of being treated as `True` and left pending. + ## 1.0.1 - 2025-12-26 ### Changed From 779bc7660491dc1ebfdb00f8209138a04241dab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sat, 28 Nov 2026 18:19:59 +0000 Subject: [PATCH 4/7] fix: do not add loop edges in complementer() by default Graph.complementer() is documented with loops=False, which is also the default in the C core's functions.yaml and in R/igraph, but the C glue code defaulted to true. Use false, as documented. This changes the result of complementer() called without arguments: loop edges are now only added when loops=True is passed. Fixes #883. Assisted-by: Claude Opus 5.5 --- CHANGELOG.md | 4 ++++ src/_igraph/graphobject.c | 2 +- tests/test_operators.py | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2b0059ec..d16fd08cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ passing a NumPy array with more than one element) are now propagated correctly instead of being treated as `True` and left pending. +- `Graph.complementer()` incorrectly added loop edges by default, contrary to + its documentation. The default of `loops` is now `False`, as documented + (#883). + ## 1.0.1 - 2025-12-26 ### Changed diff --git a/src/_igraph/graphobject.c b/src/_igraph/graphobject.c index c3a9273f5..cc0f34a78 100644 --- a/src/_igraph/graphobject.c +++ b/src/_igraph/graphobject.c @@ -11654,7 +11654,7 @@ PyObject *igraphmodule_Graph_complementer(igraphmodule_GraphObject * self, { static char *kwlist[] = { "loops", NULL }; igraphmodule_GraphObject *result_o; - int o = 1; + int o = 0; igraph_t g; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|p", kwlist, &o)) diff --git a/tests/test_operators.py b/tests/test_operators.py index 523181311..bd540e02e 100644 --- a/tests/test_operators.py +++ b/tests/test_operators.py @@ -12,6 +12,9 @@ class OperatorTests(unittest.TestCase): def testComplementer(self): g = Graph.Full(3) g2 = g.complementer() + self.assertTrue(g2.vcount() == 3 and g2.ecount() == 0) + + g2 = g.complementer(loops=True) self.assertTrue(g2.vcount() == 3 and g2.ecount() == 3) self.assertTrue(sorted(g2.get_edgelist()) == [(0, 0), (1, 1), (2, 2)]) From fb43b2287dff8914624177828289de1b07515651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sat, 28 Nov 2026 18:25:08 +0000 Subject: [PATCH 5/7] docs: improve documentation of biconnected_components() The GraphBase docstring signature stated return_articulation_points=True, but the default in the C glue code is false. Graph.biconnected_components(), the public wrapper, already documents and uses false. Since the definition of biconnectedness varies between textbooks, also document igraph's conventions in both docstrings, following the C documentation: edge directions are ignored, two vertices connected by an edge are considered biconnected, single-vertex components are not, so isolated vertices belong to no component and a single component does not imply that the graph is biconnected. Define articulation points and refer to is_biconnected() and articulation_points(). Assisted-by: Claude Opus 5.5 --- src/_igraph/graphobject.c | 20 +++++++++++++++----- src/igraph/clustering.py | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/_igraph/graphobject.c b/src/_igraph/graphobject.c index cc0f34a78..e56be0720 100644 --- a/src/_igraph/graphobject.c +++ b/src/_igraph/graphobject.c @@ -15622,15 +15622,25 @@ struct PyMethodDef igraphmodule_Graph_methods[] = { /* interface to biconnected_components */ {"biconnected_components", (PyCFunction) igraphmodule_Graph_biconnected_components, METH_VARARGS | METH_KEYWORDS, - "biconnected_components(return_articulation_points=True)\n--\n\n" + "biconnected_components(return_articulation_points=False)\n--\n\n" "Calculates the biconnected components of the graph.\n\n" - "Components containing a single vertex only are not considered as being\n" - "biconnected.\n\n" + "A graph is biconnected if the removal of any single vertex (and its\n" + "incident edges) does not disconnect it. A biconnected component is a\n" + "maximal biconnected subgraph. Edge directions are ignored.\n\n" + "Every edge belongs to exactly one biconnected component, but a vertex\n" + "may belong to several. igraph considers two vertices connected by an\n" + "edge as biconnected, while some authors do not. Components consisting\n" + "of a single vertex are not considered biconnected, so isolated vertices\n" + "are not part of any component. Therefore, finding a single biconnected\n" + "component does not imply that the graph is biconnected; use\n" + "L{is_biconnected()} to check this.\n\n" "@param return_articulation_points: whether to return the articulation\n" - " points as well\n" + " points as well. A vertex is an articulation point if its removal\n" + " increases the number of connected components of the graph.\n" "@return: a list of lists containing edge indices making up spanning trees\n" " of the biconnected components (one spanning tree for each component)\n" - " and optionally the list of articulation points" + " and optionally the list of articulation points\n" + "@see: L{is_biconnected()}, L{articulation_points()}" }, /* interface to igraph_bipartite_projection */ diff --git a/src/igraph/clustering.py b/src/igraph/clustering.py index 36df961ad..05829a523 100644 --- a/src/igraph/clustering.py +++ b/src/igraph/clustering.py @@ -1564,10 +1564,24 @@ def _biconnected_components(graph, return_articulation_points=False): """\ Calculates the biconnected components of the graph. + A graph is biconnected if the removal of any single vertex (and its + incident edges) does not disconnect it. A biconnected component is a + maximal biconnected subgraph. Edge directions are ignored. + + Every edge belongs to exactly one biconnected component, but a vertex + may belong to several. igraph considers two vertices connected by an + edge as biconnected, while some authors do not. Components consisting + of a single vertex are not considered biconnected, so isolated vertices + are not part of any component. Therefore, finding a single biconnected + component does not imply that the graph is biconnected; use + L{is_biconnected()} to check this. + @param return_articulation_points: whether to return the articulation - points as well + points as well. A vertex is an articulation point if its removal + increases the number of connected components of the graph. @return: a L{VertexCover} object describing the biconnected components, and optionally the list of articulation points as well + @see: L{is_biconnected()}, L{articulation_points()} """ if return_articulation_points: trees, aps = GraphBase.biconnected_components(graph, True) From 10dc6921dcb2bcac334a899745932cce84168978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sat, 28 Nov 2026 18:39:22 +0000 Subject: [PATCH 6/7] refactor: remove the warnings argument of topological_sorting() The undocumented warnings argument of Graph.topological_sorting() temporarily disabled igraph warnings around the C call. Since igraph 0.9, igraph_topological_sorting() raises an error instead of a warning for graphs with cycles, so the argument has had no effect. A search of public code on GitHub found no callers passing it. Remove the argument and correct the docstring, which still claimed that a partial sorting is returned with a warning for graphs that are not acyclic. Following the C documentation, state that self-loops are allowed and that undirected graphs and graphs with other cycles raise an error, and refer to is_dag() and feedback_arc_set(). Assisted-by: Claude Opus 5.5 --- CHANGELOG.md | 6 ++++++ src/_igraph/graphobject.c | 29 ++++++++--------------------- tests/test_structural.py | 3 +++ 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d16fd08cc..0fcf926f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ - Dropped support for Python 3.9 and 3.10 as they have reached their end of life. The minimum supported Python version is now 3.11. +### Removed + +- Removed the undocumented `warnings` argument of `Graph.topological_sorting()`. + It had no effect since igraph 0.9, where topological sorting started + raising an error instead of a warning for graphs with cycles. + ### Fixed - Errors raised while converting a boolean argument to a boolean (e.g. when diff --git a/src/_igraph/graphobject.c b/src/_igraph/graphobject.c index e56be0720..5982009fa 100644 --- a/src/_igraph/graphobject.c +++ b/src/_igraph/graphobject.c @@ -7327,34 +7327,19 @@ PyObject *igraphmodule_Graph_topological_sorting(igraphmodule_GraphObject * self, PyObject * args, PyObject * kwds) { - static char *kwlist[] = { "mode", "warnings", NULL }; + static char *kwlist[] = { "mode", NULL }; PyObject *list, *mode_o=Py_None; - int warnings_o = 1; igraph_neimode_t mode = IGRAPH_OUT; igraph_vector_int_t res; - igraph_warning_handler_t* old_handler = 0; - igraph_error_t retval; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Op", kwlist, &mode_o, &warnings_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &mode_o)) return NULL; if (igraphmodule_PyObject_to_neimode_t(mode_o, &mode)) return NULL; if (igraph_vector_int_init(&res, 0)) return igraphmodule_handle_igraph_error(); - if (!warnings_o) { - /* Turn off the warnings temporarily */ - old_handler = igraph_set_warning_handler(igraph_warning_handler_ignore); - } - - retval = igraph_topological_sorting(&self->g, &res, mode); - - if (!warnings_o) { - /* Restore the warning handler */ - igraph_set_warning_handler(old_handler); - } - - if (retval) { + if (igraph_topological_sorting(&self->g, &res, mode)) { igraphmodule_handle_igraph_error(); igraph_vector_int_destroy(&res); return NULL; @@ -16722,12 +16707,14 @@ struct PyMethodDef igraphmodule_Graph_methods[] = { METH_VARARGS | METH_KEYWORDS, "topological_sorting(mode=\"out\")\n--\n\n" "Calculates a possible topological sorting of the graph.\n\n" - "Returns a partial sorting and issues a warning if the graph is not\n" - "a directed acyclic graph.\n\n" + "Self-loops are allowed. Raises an error if the graph is undirected, or\n" + "if it contains any cycles that are not self-loops.\n\n" "@param mode: if C{\"out\"}, vertices are returned according to the\n" " forward topological order -- all vertices come before their\n" " successors. If C{\"in\"}, all vertices come before their ancestors.\n" - "@return: a possible topological ordering as a list"}, + "@return: a possible topological ordering as a list\n" + "@see: L{is_dag()} to only check whether the graph is a DAG, and\n" + " L{feedback_arc_set()} to find edges whose removal makes it acyclic."}, /* interface to to_prufer */ {"to_prufer", diff --git a/tests/test_structural.py b/tests/test_structural.py index c0f1bdb59..45d2ec181 100644 --- a/tests/test_structural.py +++ b/tests/test_structural.py @@ -666,6 +666,9 @@ def testTopologicalSorting(self): g = Graph(5, [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)], directed=True) self.assertTrue(g.topological_sorting() == [0, 4, 1, 2, 3]) self.assertTrue(g.topological_sorting(IN) == [3, 4, 2, 1, 0]) + # Self-loops are allowed + g2 = Graph([(0, 1), (1, 1), (1, 2)], directed=True) + self.assertEqual(g2.topological_sorting(), [0, 1, 2]) g.to_undirected() self.assertRaises(InternalError, g.topological_sorting) From 1bd129311ab78d5777d068e1317471e80f65c25e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sat, 28 Nov 2026 21:00:16 +0000 Subject: [PATCH 7/7] fix: parse the weights argument of assortativity_nominal() The keyword list of Graph.assortativity_nominal() has four entries, but the format string had only three conversions, so the weights argument was never parsed and every call passing it raised a SystemError. Add the missing conversion. Note that the C core does not implement weighted nominal assortativity yet, so non-null weights now raise a NotImplementedError from the C core instead. Assisted-by: Claude Opus 5.5 --- CHANGELOG.md | 3 +++ src/_igraph/graphobject.c | 2 +- tests/test_structural.py | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fcf926f4..2498d0892 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,9 @@ its documentation. The default of `loops` is now `False`, as documented (#883). +- `Graph.assortativity_nominal()` raised a `SystemError` whenever the `weights` + argument was given, because the argument was not parsed. + ## 1.0.1 - 2025-12-26 ### Changed diff --git a/src/_igraph/graphobject.c b/src/_igraph/graphobject.c index 5982009fa..01985a999 100644 --- a/src/_igraph/graphobject.c +++ b/src/_igraph/graphobject.c @@ -4091,7 +4091,7 @@ PyObject *igraphmodule_Graph_assortativity_nominal(igraphmodule_GraphObject *sel igraph_vector_int_t *types = 0; igraph_vector_t *weights = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|pp", kwlist, &types_o, &directed, &normalized, &weights_o)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ppO", kwlist, &types_o, &directed, &normalized, &weights_o)) return NULL; if (igraphmodule_attrib_to_vector_int_t(types_o, self, &types, ATTRIBUTE_TYPE_VERTEX)) diff --git a/tests/test_structural.py b/tests/test_structural.py index 45d2ec181..85912eaf4 100644 --- a/tests/test_structural.py +++ b/tests/test_structural.py @@ -662,6 +662,15 @@ def testConstraint(self): g = Graph(4, [(0, 1), (0, 2), (1, 2), (0, 3), (1, 3)]) self.assertTrue(isinstance(g.constraint(), list)) # TODO check more + def testAssortativityNominal(self): + g = Graph.Ring(4) + types = [0, 1, 0, 1] + self.assertEqual(g.assortativity_nominal(types), -1.0) + # The weights argument must be accepted, both as a keyword and as a + # positional argument + self.assertEqual(g.assortativity_nominal(types, weights=None), -1.0) + self.assertEqual(g.assortativity_nominal(types, True, True, None), -1.0) + def testTopologicalSorting(self): g = Graph(5, [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)], directed=True) self.assertTrue(g.topological_sorting() == [0, 4, 1, 2, 3])