Raise RuntimeError instead of PanicException when pruning on finish_vertex - #1670
Open
JuanPBedoya (juan52878911) wants to merge 1 commit into
Open
Conversation
…ertex Raising PruneSearch from the finish_vertex event of bfs_search, dfs_search or dijkstra_search reached rustworkx-core, which panics because there is nothing left to prune once a vertex is finished. The resulting PanicException derives from BaseException, so it escaped a plain 'except Exception', and it also printed a panic message to stderr next to the Python traceback. The event is now checked in the PyO3 handlers, before the control flow value reaches the core, and a RuntimeError is raised instead. The core keeps its panic as an assertion for Rust callers. The docstrings for the three searches already noted that an exception was raised in that event but did not name the type, which is now RuntimeError. They also referred to finish_vertex with :class: rather than :meth:. Fixes Qiskit#1383
Coverage Report for CI Build 33434805102Coverage increased (+0.01%) to 94.702%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
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.
Fixes #1383.
Raising
PruneSearchfrom thefinish_vertexevent reachedrustworkx-core, which panics because there is nothing left to prune once a vertex is finished. The resultingPanicExceptionderives fromBaseException, so it was not caught by a plainexcept Exception, and a panic message was printed to stderr next to the Python traceback:The issue proposes raising a
RuntimeErrorinstead, which is what this does. The event is checked in the PyO3 handlers, before the control flow value reaches the core, so the core never panics and the stderr message is gone as well.rustworkx-coreis left untouched: itspanic!stays as an assertion for Rust callers, where returningPruneonFinishreally is a programming error.The panic was reachable from all three traversals, not just DFS (
dfs_visit.rs:270,bfs_visit.rs:243,dijkstra_visit.rs:283), so all three handlers are fixed.I checked the whole event surface to make sure the change is neither too narrow nor too wide: every event of the three traversals, with both
PruneSearchandStopSearch, onPyGraphandPyDiGraph.PruneSearchonfinish_vertexis the only combination that panicked, pruning on every other event still prunes, andStopSearchis unaffected. DFS propagatingStopSearchback to the caller is existing documented behaviour and is left alone.Documentation
The docstrings for the three searches already noted that an exception was raised in that event but did not name the type, which is now
RuntimeError. That note is duplicated inrustworkx/__init__.py, which is whathelp()and the published docs show, so both copies are updated. Those lines also referred tofinish_vertexwith:class:rather than:meth:, fixed in passing since they were being rewritten anyway.Tests
24 tests across the six
test_{bfs,dfs,dijkstra}_search.pyfiles, intests/graph/andtests/digraph/:prune_finish_vertex— aRuntimeErroris raised.prune_finish_vertex_mid_traversal— the visitor prunes on its secondfinish_vertex, which pins the exact finish order up to that point and shows the error is not raised eagerly.prune_finish_vertex_no_starting_point— same withsource=None, so more than one component is walked.stop_search_finish_vertex— a guard thatStopSearchin the same event is unaffected.Reverting only the three handler files to
mainand rebuilding, 18 of the 24 fail with the oldPanicException; the 6stop_search_finish_vertextests pass either way, which is what makes them useful as guards.cargo test --workspaceis 524 passed and the Python suite is 2418 passed / 32 skipped, withcargo fmt,cargo clippy --workspace --all-targets -- -D warnings,ruff format,ruff check,find_stray_release_notes.pyandcargo doc -p rustworkx-corewith-D warningsall clean.