Skip to content

Make ResdataKW.read_grdecl use pybind#1163

Merged
eivindjahren merged 19 commits into
mainfrom
fix_oom_of_grdecl
May 13, 2026
Merged

Make ResdataKW.read_grdecl use pybind#1163
eivindjahren merged 19 commits into
mainfrom
fix_oom_of_grdecl

Conversation

@eivindjahren
Copy link
Copy Markdown
Collaborator

@eivindjahren eivindjahren commented May 11, 2026

Also fixes a segfault for zero-length min_max.

With regards to returning None or not, I changed the documentation of "If the keyword can not be found the method will return None." This is because we consider this legacy code so its more likely that someone depends on behavior rather than documentation.

Copy link
Copy Markdown

Copilot AI left a comment

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 migrates GRDECL keyword I/O for ResdataKW from the old cwrap/ctypes prototype layer to a new pybind11 extension (resdata.resfile._kw), while refactoring the underlying C++ GRDECL reader/writer APIs and updating C++/Python tests accordingly.

Changes:

  • Added a new pybind11 module (_kw) and switched ResdataKW.read_grdecl, fseek_grdecl, and write_grdecl to use it.
  • Refactored GRDECL parsing/writing in C++ (rd_kw_grdecl.*) to use exceptions/RAII and updated call sites to the new function signatures.
  • Added Python tests for empty GRDECL bodies and strict-mode malformed token errors.

Reviewed changes

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

Show a summary per file
File Description
tests/rd_tests/test_rd_kw.py Adds new Python regression tests for GRDECL reading (empty + malformed strict parsing).
python/resdata/resfile/rd_kw.py Switches GRDECL read/seek/write paths to the new pybind module and adjusts get_min_max for empty keywords.
lib/tests/test_rd_grid_misc.cpp Updates GRDECL read helper to new rd_kw_fscanf_alloc_grdecl argument order.
lib/resdata/tests/rd_layer_equinor.cpp Updates GRDECL keyword read call + includes new GRDECL header.
lib/resdata/tests/rd_kw_grdecl.cpp Updates GRDECL read/write tests to new APIs (including “special header” printing).
lib/resdata/tests/rd_kw_fread.cpp Updates GRDECL read call ordering + includes GRDECL header.
lib/resdata/tests/rd_fault_block_layer_equinor.cpp Updates GRDECL keyword read call ordering + includes GRDECL header.
lib/resdata/rd_type.cpp Introduces rd_type_name() returning std::string for improved error formatting.
lib/resdata/rd_type_python.cpp Removes the old GRDECL dynamic python shim function (no longer needed with pybind).
lib/resdata/rd_kw.cpp Makes max/min on zero-length keywords abort (paired with Python-side guard).
lib/resdata/rd_kw_pybind.cpp New pybind11 module providing GRDECL load/seek/print entry points for Python.
lib/resdata/rd_kw_grdecl.cpp Refactors GRDECL parsing/writing; changes APIs and error handling to exceptions.
lib/resdata/rd_grid.cpp Updates GRDECL printing to use new rd_kw_fprintf_grdecl API.
lib/include/resdata/rd_type.hpp Declares new rd_type_name() C++ API.
lib/include/resdata/rd_kw.hpp Removes implicit inclusion of rd_kw_grdecl.hpp (callers now include explicitly).
lib/include/resdata/rd_kw_grdecl.hpp Reworks GRDECL header declarations (now C++-style with optional “special header”).
lib/CMakeLists.txt Adds build/install rules for the new _kw pybind module.
applications/resdata/grdecl_test.cpp Updates deprecated app to new GRDECL read API.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread python/resdata/resfile/rd_kw.py
Comment thread lib/resdata/rd_kw_pybind.cpp Outdated
Comment thread lib/include/resdata/rd_kw_grdecl.hpp Outdated
Comment thread lib/include/resdata/rd_kw_grdecl.hpp Outdated
@eivindjahren eivindjahren force-pushed the fix_oom_of_grdecl branch 5 times, most recently from 9f00f62 to baf3209 Compare May 11, 2026 11:42
@eivindjahren eivindjahren requested a review from Copilot May 11, 2026 13:12
Copy link
Copy Markdown

Copilot AI left a comment

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 18 out of 18 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

lib/resdata/rd_kw_grdecl.cpp:12

  • rd_kw_grdecl.cpp implements public functions (rd_kw_fscanf_alloc_grdecl, rd_kw_fprintf_grdecl) but does not include its corresponding public header rd_kw_grdecl.hpp. This makes it easy for the implementation signature to drift from the installed API without the compiler noticing. Consider including resdata/rd_kw_grdecl.hpp here and moving default arguments to the header only (omit them from the definition) to ensure consistency.
#include <cstring>
#include <cctype>
#include <optional>
#include <memory>

#include <ert/util/util.hpp>
#include <fmt/format.h>

#include <resdata/rd_kw.hpp>
#include <resdata/rd_type.hpp>
#include <resdata/rd_util.hpp>

Comment thread python/resdata/resfile/rd_kw.py Outdated
Comment thread lib/resdata/rd_kw_pybind.cpp
Comment thread lib/include/resdata/rd_kw_grdecl.hpp Outdated
Copy link
Copy Markdown
Contributor

@ajaust ajaust left a comment

Choose a reason for hiding this comment

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

I have mostly minor points. I have some concerns how checked_realloc is defined vs. how it is used (using element count vs byte size for allocation).

I don't know if all my comments appear at the correct location nor whether they are still all relevent. Some commits changed while I was preparing the review. Therefore, there might be the some comment duplications.

Comment thread python/resdata/resfile/rd_kw.py
Comment thread lib/resdata/rd_kw.cpp
Comment thread tests/rd_tests/test_rd_kw.py Outdated
Comment thread tests/rd_tests/test_rd_kw.py
Comment thread tests/rd_tests/test_rd_kw.py Outdated
Comment thread lib/include/resdata/rd_kw_grdecl.hpp Outdated
Comment thread lib/resdata/rd_kw_grdecl.cpp Outdated
Comment thread lib/resdata/rd_kw_grdecl.cpp Outdated
Comment thread lib/resdata/rd_kw_grdecl.cpp
Comment thread lib/resdata/rd_kw_grdecl.cpp Outdated
@ajaust ajaust added this to SCOUT May 12, 2026
@ajaust ajaust moved this to Reviewed in SCOUT May 12, 2026
@eivindjahren eivindjahren force-pushed the fix_oom_of_grdecl branch 3 times, most recently from af11301 to 347e1d2 Compare May 13, 2026 06:45
Copy link
Copy Markdown
Contributor

@ajaust ajaust left a comment

Choose a reason for hiding this comment

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

Looks good overall. One verification questions and otherwise nitpicks. Check if you want to address these, but feel free to ignore them. :)

Comment thread tests/rd_tests/test_rd_kw.py
Comment thread tests/rd_tests/test_rd_kw.py
Comment thread lib/resdata/rd_kw_grdecl.cpp
Comment thread lib/resdata/rd_kw_grdecl.cpp Outdated
}
ptr.reset(static_cast<T *>(new_raw_ptr));
}
template <typename T> const inline std::string value_format();
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.

Nitpick:
Empty line missing above definition of value_format.

Comment thread lib/resdata/rd_kw_grdecl.cpp Outdated
Comment thread lib/resdata/rd_kw_grdecl.cpp
Comment thread lib/resdata/rd_kw_grdecl.cpp Outdated
@eivindjahren eivindjahren force-pushed the fix_oom_of_grdecl branch 2 times, most recently from 69f1a4f to c018ef9 Compare May 13, 2026 09:56
@eivindjahren eivindjahren requested a review from Copilot May 13, 2026 10:05
Copy link
Copy Markdown

Copilot AI left a comment

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 18 out of 18 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (1)

lib/resdata/rd_kw_grdecl.cpp:12

  • rd_kw_grdecl.cpp throws standard exceptions (std::invalid_argument, std::bad_alloc) but does not include the corresponding standard headers (<stdexcept> / <new>). Adding them explicitly avoids relying on transitive includes and prevents build failures on some compilers.
#include <cstring>
#include <cctype>
#include <optional>
#include <memory>

#include <ert/util/util.hpp>
#include <fmt/format.h>

#include <resdata/rd_kw.hpp>
#include <resdata/rd_type.hpp>
#include <resdata/rd_util.hpp>

Comment thread lib/include/resdata/rd_kw_grdecl.hpp
Comment thread lib/resdata/rd_type.cpp
Comment thread python/resdata/resfile/rd_kw.py
Comment thread tests/rd_tests/test_rd_kw.py
@eivindjahren eivindjahren merged commit 6909edd into main May 13, 2026
16 checks passed
@eivindjahren eivindjahren deleted the fix_oom_of_grdecl branch May 13, 2026 10:15
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in SCOUT May 13, 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.

3 participants