Fix ctypes memory safety and enrich tests#35
Merged
Conversation
- Wrap RoutingSolution extraction in try/finally so delete_solution always runs even if extraction raises. - Replace arr.astype(c_double).ctypes + cast(...) with named np.ascontiguousarray locals + .ctypes.data_as(c_double_p) so the numpy buffer is pinned through the C call. Also removes up to two redundant n^2 copies of the distance matrix. - Add tests covering the new exception path, dtype/contiguity variants, input validation, solver reuse, and CVRP solution invariants. - Bump CI Python matrix to 3.10 and 3.13. - Bump version to 0.1.0. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
a4a50e6 to
7423f5f
Compare
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.
Summary
delete_solutionwas only called on the happy path. IfRoutingSolutionextraction raised mid-construction, the C-allocatedSolution(plus every route'spathbuffer) leaked. Wrapped the extraction intry/finally.arr.astype(c_double).ctypes+cast(...)idiom never bound the temporary ndarray to a local — it survived only via numpy's_ctypesproxy back-reference. Replaced with namednp.ascontiguousarray(arr, dtype=np.float64)locals +.ctypes.data_as(c_double_p), which pins the buffer through the C call and removes up to two redundant n² copies of the distance matrix (the old code didreshapefollowed by an always-copyingastype).dist_mtx, int demands, list coords), input-validation assertions, solver reuse, and CVRP solution invariants (every customer visited exactly once, depot not in route bodies, capacity respected).["3.11", "3.10"]to["3.10", "3.13"].Coverage on
hygese/hygese.pyis now 95% (8 missed lines are platform-specific branches like Windows-only andsolve_tspvia coords).Test plan
pytest hygese/tests/ -v— all 15 tests pass locally on macOS / Python 3.13 (~16 s).pytest --cov=hygese hygese/tests/— coverage onhygese/hygese.pyat 95%, with the newtry/finallyandascontiguousarraypaths fully covered.test_delete_called_on_exceptionmonkey-patchesRoutingSolution.__init__to raise and assertsdelete_solutionis still invoked exactly once.np.shares_memory(x, np.ascontiguousarray(x, dtype=np.float64))isTruewhenxis alreadyfloat64/C-contiguous.ubuntu-latest/windows-2019/macos-latest× Python3.10/3.13.🤖 Generated with Claude Code