Pybind rd file view#1244
Merged
Merged
Conversation
7e107f3 to
5dac25e
Compare
There was a problem hiding this comment.
Pull request overview
This PR modernizes the rd_file “view” layer by replacing the legacy C-style rd_file_view_type API with a C++ rd::FileView/FileKW implementation and exposes it to Python via new pybind11 modules. It also introduces a strongly-typed FileMode flag enum that replaces ad-hoc integer flag usage across C++ and Python bindings.
Changes:
- Replaces
rd_file_viewinternals withrd::FileView+FileKWand updates call sites to use the new API (has_kw,get_kw,blockview, restart/summary views). - Adds pybind11 bindings for
ResdataFileView(rd_file_viewmodule) and for theFileModeenum (_file_modemodule), updating Python code to consume these types. - Expands/updates unit tests (C++ Catch2 + Python pytest) to validate new view behavior, keyword saving semantics, and overflow-guarded allocations.
Reviewed changes
Copilot reviewed 69 out of 69 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/rd_tests/test_rd_file.py | Updates index-file opening API; adds save_kw roundtrip and error-path tests. |
| tests/rd_tests/test_rd_file_view.py | New pytest suite covering ResdataFileView behavior (len/contains/indexing/block/restart views, FileMode flags). |
| python/resdata/summary/rd_sum.py | Switches file_options to FileMode and updates _rd_sum calls accordingly. |
| python/resdata/resfile/rd_file.py | Uses pybind-returned ResdataFileView object directly for global_view. |
| python/resdata/resfile/rd_file_view.py | Removes legacy cwrap-based Python wrapper (now provided by pybind). |
| python/resdata/rd_util.py | Moves FileMode to _file_mode pybind and updates exports/docs. |
| lib/tests/test_well_info.cpp | Updates view/file access to new FileView API and open_rd_file. |
| lib/tests/test_rd_util.cpp | Adds tests for checked allocation overflow/zero-size behavior. |
| lib/tests/test_rd_sum.cpp | Updates restart/summary view usage to new FileView API and open_rd_file. |
| lib/tests/test_rd_kw.cpp | Adds overflow-guard test for FileKW::read. |
| lib/tests/test_rd_file_kw.cpp | Reworks tests to target new FileKW class behaviors (construction, equality, IO, lazy-load, clear, inplace-write failure). |
| lib/tests/test_geertsma.cpp | Updates restart view handling to shared_ptr-based API. |
| lib/tests/grid_fixtures.hpp | Adds convenience overload for writing int keywords from std::string. |
| lib/resdata/well_state.cpp | Migrates well restart parsing to rd::FileView interface. |
| lib/resdata/well_rseg_loader.cpp | Migrates loader to rd::FileView + FileMode flag checks. |
| lib/resdata/well_info.cpp | Migrates restart-step iteration and view lifecycle management to new API (clear() instead of transactions). |
| lib/resdata/tests/well_state_load.cpp | Updates standalone test program to new view API. |
| lib/resdata/tests/well_state_load_missing_RSEG.cpp | Updates standalone test program to new view API. |
| lib/resdata/tests/well_segment_load.cpp | Updates loader allocation call to accept rd::FileView*. |
| lib/resdata/tests/well_dualp.cpp | Updates file open signature. |
| lib/resdata/tests/well_conn_CF.cpp | Updates file open signature and view access. |
| lib/resdata/tests/test_transactions.cpp | Removes deprecated transaction-based test (transactions removed from view implementation). |
| lib/resdata/tests/test_rd_file_index.cpp | Updates fast-open/open signatures and removes obsolete global_view assertion. |
| lib/resdata/tests/rdxx_kw.cpp | Updates file open signature. |
| lib/resdata/tests/rd_rsthead.cpp | Updates to shared_ptr-based views for header reading. |
| lib/resdata/tests/rd_restart_test.cpp | Updates file open signature. |
| lib/resdata/tests/rd_nnc_test.cpp | Updates blockview creation/cleanup to shared_ptr-based API. |
| lib/resdata/tests/rd_lgr_test.cpp | Updates file open signature. |
| lib/resdata/tests/rd_lfs.cpp | Updates file open signature. |
| lib/resdata/tests/rd_grid_volume.cpp | Switches to open_rd_file. |
| lib/resdata/tests/rd_grid_init_fwrite.cpp | Updates file open signature. |
| lib/resdata/tests/rd_grid_export.cpp | Updates file open signature. |
| lib/resdata/tests/rd_grid_dx_dy_dz.cpp | Updates file open signature. |
| lib/resdata/tests/rd_file.cpp | Updates writable/close-stream usage to FileMode and new view API. |
| lib/resdata/tests/rd_file_view.cpp | Removes legacy C-style rd_file_view test (replaced by new C++/pybind tests). |
| lib/resdata/tests/rd_file_equinor.cpp | Updates file open flags to FileMode and adjusts view usage. |
| lib/resdata/tests/rd_dualp.cpp | Switches to open_rd_file. |
| lib/resdata/tests/rd_coarse_test.cpp | Switches to open_rd_file. |
| lib/resdata/rsthead_pybind.cpp | Updates RSTHead construction to accept std::shared_ptr<rd::FileView>. |
| lib/resdata/rd_unsmry_loader.cpp | Migrates to rd::FileView methods for indexed reads and report-step scanning. |
| lib/resdata/rd_sum.cpp | Propagates FileMode through summary loading APIs. |
| lib/resdata/rd_sum_pybind.cpp | Updates pybind API to accept FileMode instead of int. |
| lib/resdata/rd_sum_file_data.cpp | Uses FileView for iterating/reading summary blocks and filename retrieval. |
| lib/resdata/rd_sum_data.cpp | Propagates FileMode through rd_sum_data_fread. |
| lib/resdata/rd_subsidence.cpp | Migrates restart keyword access to rd::FileView. |
| lib/resdata/rd_subsidence_pybind.cpp | Updates bindings to pass std::shared_ptr<rd::FileView>. |
| lib/resdata/rd_smspec.cpp | Switches to open_rd_file. |
| lib/resdata/rd_grid.cpp | Migrates NNC block handling to shared_ptr-based blockviews and adds error handling. |
| lib/resdata/rd_grav.cpp | Migrates gravity survey calculations to rd::FileView and modernizes keyword lookup. |
| lib/resdata/rd_grav_pybind.cpp | Updates bindings to accept std::shared_ptr<rd::FileView>/rd::FileView*. |
| lib/resdata/rd_file.cpp | Refactors rd_file to hold std::unique_ptr<FortIO>, shared_ptr views, FileMode, and FileKW-based indexing; updates open/fast-open/view APIs. |
| lib/resdata/rd_file_view.cpp | Replaces legacy C implementation with rd::FileView implementation (indexing, block/restart/summary views, load/clear, index IO). |
| lib/resdata/rd_file_view_pybind.cpp | New pybind module providing Python ResdataFileView API. |
| lib/resdata/rd_file_pybind.cpp | Updates rd_file pybind to use FileMode and return std::shared_ptr<rd::FileView> from _get_global_view. |
| lib/resdata/rd_file_kw.cpp | Replaces legacy rd_file_kw and inv_map implementation with FileKW and checked allocation helpers. |
| lib/resdata/file_mode_pybind.cpp | New pybind module for exposing FileMode as resdata._file_mode.FileMode. |
| lib/resdata/cwrap_pybind.cpp | Removes legacy ResdataFileView cwrap type-caster (no longer used). |
| lib/private-include/detail/resdata/rd_unsmry_loader.hpp | Updates loader interface to use FileMode and std::shared_ptr<rd::FileView>. |
| lib/private-include/detail/resdata/rd_sum_file_data.hpp | Updates summary file data interface to use FileMode and rd::FileView&. |
| lib/private-include/detail/resdata/cwrap_pybind.hpp | Adds ResdataKW() accessor used by the new rd_file_view pybind. |
| lib/include/resdata/well/well_state.hpp | Updates well APIs to consume rd::FileView*. |
| lib/include/resdata/well/well_rseg_loader.hpp | Updates loader API to accept rd::FileView* and modernizes header guards. |
| lib/include/resdata/well/well_info.hpp | Updates well APIs to consume rd::FileView*. |
| lib/include/resdata/rd_util.hpp | Adds checked allocation helpers and includes required headers. |
| lib/include/resdata/rd_sum.hpp | Updates summary API surface to accept FileMode. |
| lib/include/resdata/rd_sum_data.hpp | Updates rd_sum_data_fread signature to accept FileMode. |
| lib/include/resdata/rd_subsidence.hpp | Updates subsidence API to accept rd::FileView*. |
| lib/include/resdata/rd_rsthead.hpp | Updates header reader to use rd::FileView API. |
| lib/include/resdata/rd_grav.hpp | Updates gravity APIs to accept rd::FileView*. |
| lib/include/resdata/rd_file.hpp | Updates rd_file public API to use FileMode and shared_ptr-based views; modernizes open_rd_file helpers. |
| lib/include/resdata/rd_file_view.hpp | Replaces legacy C API with rd::FileView class declaration. |
| lib/include/resdata/rd_file_kw.hpp | Replaces legacy rd_file_kw_type C API with FileKW class declaration. |
| lib/include/resdata/rd_file_flag.hpp | New strongly-typed FileMode flag enum with bitwise operators. |
| lib/include/resdata/FortIO.hpp | Adds filename() accessor returning std::string. |
| lib/CMakeLists.txt | Adds new pybind modules (rd_file_view, _file_mode) and removes deprecated tests/targets. |
| applications/resdata/rd_unpack.cpp | Migrates unpack logic to open_rd_file + FileView::write/get_kw. |
| applications/resdata/rd_pack.cpp | Migrates pack logic to open_rd_file. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
5dac25e to
e24492b
Compare
dbb1820 to
7839942
Compare
63edb2f to
1dbba70
Compare
5455fad to
0c505a1
Compare
7ce4d6f to
f38ac6a
Compare
Also introduces a default argument to rd_file_open to avoid calling it with flags=0 repeatedly.
43cd5c5 to
43c725f
Compare
5f468f0 to
5fe845f
Compare
Since we now have better control over the integer type conversions, get_kw now takes size_t. This fixes a object life-time issue with ResdataFile where all read values would be cleaned up after close, meaning all use of kws read inside an open_rd_file context be use-after-free.
5fe845f to
290c771
Compare
MagnusSletten
approved these changes
Jul 14, 2026
MagnusSletten
left a comment
Contributor
There was a problem hiding this comment.
Looks great! Formatter still complains but fixing that is trivial.
9aead9f to
cacc8a6
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.
This changes the behavior of calling ResdataFile::close. Before, the pointer got "invalidated" which meant the pointer got set to NULL which was never handled correctly. Now, the file handle gets held open until .close() is called, however, reading or writing to the file view then just re-opens the file.
Resolves #1246
We change to
size_tfor a number of parameters and return values ofFileViewmethods, but leaverd_file_get_num_named_kwandindex_fload_kwindices as int because changing those require a lot of changes.