Repository navigation
Raise ParseError for truncated stoch plane arrays - #159
Merged
Merged
Conversation
Closes #145 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Closes #145
What was wrong
_read_stoch_planeread the slip, rise and trup arrays withnp.fromfile(..., sep=" ", count=nx*ny).reshape((ny, nx)). If the file ended, or held a token numpy could not parse, partway through an array,np.fromfilereturned a shorter array. The reshape then failed with a bareValueError: cannot reshape array of size K into shape (ny,nx). That error named neither the array nor the problem, and it was not theparse_utils.ParseErrorthat the rest of the parser raises for malformed input.The
StochFile.from_filedocstring also said it raisesValueErrorfor a non-positive plane count. The code raisesparse_utils.ParseError, which is not aValueErrorsubclass.What changed
_read_array(handle, header, label)insource_modelling/stoch.py. It readsnx*nyvalues and raisesparse_utils.ParseError(f"Expected {n} values for {label}, got {size}")if it gets fewer._read_stoch_planenow uses it for slip, rise and trup, and its docstring gains aRaisessection.Raisessection of theStochFile.from_filedocstring now saysparse_utils.ParseError, and covers truncated or malformed data too.Parsing of well-formed files is unchanged: same dtype, same shape, same cast.
Tests
test_stoch_file_truncated_array_raises_parse_erroruses the reproducer from the issue:nx=ny=2, with only 3 trup values. Before the fix it failed withValueError: cannot reshape array of size 3 into shape (2,2). After the fix it passes, and the error isParseError("Expected 4 values for trup, got 3"). The full suite passes (801 tests), andruff checkandruff format --checkare clean on the changed files.For reviewers
ValueErrorfrom a truncated array will no longer catch it, becauseParseErrorsubclassesException. This matches what the issue asks for, and how parse_utils.read_float/read_int loop forever at end of file, so a truncated stoch file hangs StochFile.from_file #122 handles a missing plane.🤖 Generated with Claude Code