Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(binaryread): raise/handle EOFError, deprecate vartype=str#2226

Merged
wpbonelli merged 1 commit into
modflowpy:developfrom
mwtoews:fix-binaryfile-eof
Jun 13, 2024
Merged

fix(binaryread): raise/handle EOFError, deprecate vartype=str#2226
wpbonelli merged 1 commit into
modflowpy:developfrom
mwtoews:fix-binaryfile-eof

Conversation

@mwtoews

@mwtoews mwtoews commented Jun 12, 2024

Copy link
Copy Markdown
Contributor

This fixes issues while reading some binaryfiles with auto precision detection, and also modernizes a few aspects of flopy.utils.binaryfile left-over from python2.

There are two changes to flopy.utils.binaryfile.binaryread():

  1. Raises EOFError if attempting to read data beyond the end-of-file
  2. Deprecate vartype=str, since bytes is the the return type with Python3

Other refactors:

  • Simplify conventional ASCII range checks by converting bytes to a list of int, then check if bytes are within range
  • Remove checks if bytes are not str, and use .encode("ascii") where appropriate

Examples of issues

CellBudgetFile

Reading a cell budget file that contains imeth=5 with precision="auto" (the default):

python -c "from flopy.utils import *; print(CellBudgetFile('examples/data/preserve_unitnums/testsfr2.ghb.cbc').headers)"

previously this will hang indefinitely since it is attempting to read data at the end of the file without raising any exception. This example is fixed with this PR.

HeadFile

And this is not so much a fix, but a less confusing error:

python -c "from flopy.utils import *; HeadFile('examples/data/mf6/create_tests/test_transport/expected_output/gwt_mst03.ucn')"

the exception is changed from:

IndexError: index 0 is out of bounds for axis 0 with size 0

to:

EOFError

I have some pending changes to resolve this issue, due after this one.

@codecov

codecov Bot commented Jun 12, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 96.07843% with 2 lines in your changes missing coverage. Please review.

Project coverage is 70.8%. Comparing base (e2d16df) to head (ac52c31).

Additional details and impacted files
@@            Coverage Diff            @@
##           develop   #2226     +/-   ##
=========================================
+ Coverage     68.0%   70.8%   +2.8%     
=========================================
  Files          294     294             
  Lines        58880   59026    +146     
=========================================
+ Hits         40061   41826   +1765     
+ Misses       18819   17200   -1619     
Files Coverage Δ
flopy/utils/binaryfile.py 81.5% <96.0%> (+1.7%) ⬆️

... and 212 files with indirect coverage changes

@wpbonelli wpbonelli merged commit e2a85a3 into modflowpy:develop Jun 13, 2024
@christianlangevin

Copy link
Copy Markdown

Thanks for this @mwtoews. The binary readers have been needing some updates. Appreciate you taking it on. Time to get past the python2 hangover.

@mwtoews mwtoews deleted the fix-binaryfile-eof branch June 13, 2024 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants