Make ResdataKW.read_grdecl use pybind#1163
Conversation
988158e to
3240962
Compare
There was a problem hiding this comment.
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 switchedResdataKW.read_grdecl,fseek_grdecl, andwrite_grdeclto 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.
9f00f62 to
baf3209
Compare
There was a problem hiding this comment.
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.cppimplements public functions (rd_kw_fscanf_alloc_grdecl,rd_kw_fprintf_grdecl) but does not include its corresponding public headerrd_kw_grdecl.hpp. This makes it easy for the implementation signature to drift from the installed API without the compiler noticing. Consider includingresdata/rd_kw_grdecl.hpphere 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>
3865df4 to
3ea980e
Compare
ajaust
left a comment
There was a problem hiding this comment.
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.
af11301 to
347e1d2
Compare
ajaust
left a comment
There was a problem hiding this comment.
Looks good overall. One verification questions and otherwise nitpicks. Check if you want to address these, but feel free to ignore them. :)
| } | ||
| ptr.reset(static_cast<T *>(new_raw_ptr)); | ||
| } | ||
| template <typename T> const inline std::string value_format(); |
There was a problem hiding this comment.
Nitpick:
Empty line missing above definition of value_format.
69f1a4f to
c018ef9
Compare
Fixing some bugs in the model test surfaced these bugs.
Also removes rd_kw_fscanf_alloc_current_grdecl__
Co-authored-by: Alexander Jaust <18347253+ajaust@users.noreply.github.com>
c018ef9 to
cd54dea
Compare
There was a problem hiding this comment.
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.cppthrows 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>
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.