Repository navigation
Handle errors in tri-state Python API boolean function by using the "p" parsing format specifier - #901
Merged
Conversation
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
… "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
Assisted-by: Claude Opus 5.5
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 igraph#883. Assisted-by: Claude Opus 5.5
szhorvat
force-pushed
the
fix/bool-arguments
branch
2 times, most recently
from
November 28, 2026 18:42
d022f44 to
21f74e7
Compare
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
szhorvat
force-pushed
the
fix/bool-arguments
branch
from
November 28, 2026 18:48
21f74e7 to
1981646
Compare
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
szhorvat
force-pushed
the
fix/bool-arguments
branch
from
November 28, 2026 19:04
1981646 to
10dc692
Compare
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
ntamas
reviewed
Nov 29, 2026
ntamas
left a comment
Member
There was a problem hiding this comment.
LGTM apart from that single comment I posted.
| static char *kwlist[] = { "loops", NULL }; | ||
| igraphmodule_GraphObject *result_o; | ||
| PyObject *o = Py_True; | ||
| int o = 0; |
Member
There was a problem hiding this comment.
This changes the default from true to false, FYI. Is this what you wanted?
Member
There was a problem hiding this comment.
Ah I just read the PR description now. This is okay then.
ntamas
approved these changes
Nov 29, 2026
Member
|
Copilot has a point in its review but it's outside the scope of this PR. Will you take care of this or shall I fix it later? |
Member
Author
It was fixed in the same PR in this commit: 1bd1293 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Boolean function in the Python API typically don't just return
1/0for true/false, but also-1for an error. This means that usage such asif (PyObject_IsTrue()) ...isn't proper.This PR fixes some, but not all of these cases (the rest will come in a new PR). It contains fixes for those cases where the
"p"parsing format specifier can handle this automatically. This is a very large number of mostly identical changes. Slightly different cases are in a second commit.At the same time, I did a review of whether the touched parameters' default value in the C code matches the default value from the docs / Python signature. Three corrections came out of this review:
complementer()claimsloops=Falsein the docs and signature but usesloops=Truein reality. This was reported in Mistake in complementer signature #883. This behaviour and mismatch has been present in python-igraph for forever, which makes changing it now a difficult decision. What do you think @ntamas ? I think the right thing to do is to fix the behaviour and go with what the docs say, i.e.loops=False. This is done in this commit. This is the behaviour one would normally expect and what all other interfaces use by default.topological_sorting()has an undocumentedwarningparameter that did nothing. This is leftover from when C/igraph 0.8 had such a warning parameter, which got dropped in C/igraph 0.9. It is now removed without deprecation.GraphBase.biconnected_componentsstated the wrong default in the docs forreturn_articulation_points, this is now changed to match the actual behaviour. Should not be controversial.