Skip to content

Pybind rd file view#1244

Merged
eivindjahren merged 20 commits into
mainfrom
pybind_rd_file_view
Jul 14, 2026
Merged

Pybind rd file view#1244
eivindjahren merged 20 commits into
mainfrom
pybind_rd_file_view

Conversation

@eivindjahren

@eivindjahren eivindjahren commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

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_t for a number of parameters and return values of FileView methods, but leave rd_file_get_num_named_kw and index_fload_kw indices as int because changing those require a lot of changes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_view internals with rd::FileView + FileKW and updates call sites to use the new API (has_kw, get_kw, blockview, restart/summary views).
  • Adds pybind11 bindings for ResdataFileView (rd_file_view module) and for the FileMode enum (_file_mode module), 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.

Comment thread lib/resdata/rd_file.cpp Outdated
Comment thread lib/resdata/rd_file.cpp
Comment thread lib/resdata/rd_file_view_pybind.cpp Outdated
Comment thread lib/resdata/rd_file_view_pybind.cpp Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 69 out of 69 changed files in this pull request and generated 4 comments.

Comment thread lib/resdata/rd_file_view.cpp Outdated
Comment thread lib/resdata/rd_file_view.cpp Outdated
Comment thread lib/resdata/rd_file_pybind.cpp Outdated
Comment thread lib/resdata/rd_file_pybind.cpp Outdated
@eivindjahren eivindjahren force-pushed the pybind_rd_file_view branch 2 times, most recently from dbb1820 to 7839942 Compare July 13, 2026 05:25
@eivindjahren eivindjahren requested a review from Copilot July 13, 2026 05:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 69 out of 69 changed files in this pull request and generated 3 comments.

Comment thread lib/resdata/rd_file.cpp Outdated
Comment thread lib/resdata/rd_file.cpp
Comment thread lib/resdata/rd_file_view_pybind.cpp
Comment thread lib/resdata/rd_file_view_pybind.cpp Outdated
@eivindjahren eivindjahren force-pushed the pybind_rd_file_view branch 2 times, most recently from 63edb2f to 1dbba70 Compare July 13, 2026 09:48
@eivindjahren eivindjahren requested a review from Copilot July 13, 2026 09:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 73 out of 73 changed files in this pull request and generated 2 comments.

Comment thread lib/resdata/rd_file_view_pybind.cpp Outdated
Comment thread lib/resdata/rd_file_view_pybind.cpp Outdated
@eivindjahren eivindjahren force-pushed the pybind_rd_file_view branch 4 times, most recently from 5455fad to 0c505a1 Compare July 13, 2026 10:31
@eivindjahren eivindjahren requested a review from Copilot July 13, 2026 10:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 73 out of 73 changed files in this pull request and generated 3 comments.

Comment thread lib/resdata/rd_sum_file_data.cpp
Comment thread lib/resdata/rd_sum_file_data.cpp
Comment thread lib/resdata/rd_grav_pybind.cpp
@eivindjahren eivindjahren force-pushed the pybind_rd_file_view branch 3 times, most recently from 7ce4d6f to f38ac6a Compare July 13, 2026 11:23
@eivindjahren eivindjahren force-pushed the pybind_rd_file_view branch 2 times, most recently from 43cd5c5 to 43c725f Compare July 14, 2026 06:35
@eivindjahren eivindjahren requested a review from Copilot July 14, 2026 06:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 70 out of 70 changed files in this pull request and generated no new comments.

Comment thread lib/include/resdata/rd_file.hpp
Comment thread lib/include/resdata/rd_file_view.hpp Outdated
@eivindjahren eivindjahren force-pushed the pybind_rd_file_view branch 4 times, most recently from 5f468f0 to 5fe845f Compare July 14, 2026 07:17
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.
@eivindjahren eivindjahren force-pushed the pybind_rd_file_view branch from 5fe845f to 290c771 Compare July 14, 2026 07:26

@MagnusSletten MagnusSletten left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Formatter still complains but fixing that is trivial.

@github-project-automation github-project-automation Bot moved this from Ready for Review to Reviewed in SCOUT Jul 14, 2026
@eivindjahren eivindjahren force-pushed the pybind_rd_file_view branch from 9aead9f to cacc8a6 Compare July 14, 2026 08:04
@eivindjahren eivindjahren merged commit 311d931 into main Jul 14, 2026
11 checks passed
@eivindjahren eivindjahren deleted the pybind_rd_file_view branch July 14, 2026 08:38
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in SCOUT Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

ResdataFile invalidates all read values after close

3 participants