Skip to content

Use pybind for rd_region, rd_sum_tstep and rd_sum_vector#1259

Open
eivindjahren wants to merge 12 commits into
mainfrom
pybind_the_rest
Open

Use pybind for rd_region, rd_sum_tstep and rd_sum_vector#1259
eivindjahren wants to merge 12 commits into
mainfrom
pybind_the_rest

Conversation

@eivindjahren

Copy link
Copy Markdown
Collaborator

No description provided.

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 migrates the Python ResdataRegion implementation from cwrap ResdataPrototype bindings to a dedicated pybind11 extension module (resdata.grid._rd_region), aligning it with the codebase’s ongoing move toward pybind-based bindings.

Changes:

  • Replaced ResdataRegion’s cwrap prototypes with calls into a new pybind11 module (resdata.grid._rd_region).
  • Added new pybind11 module implementation for rd_region in C++.
  • Registered the new module in lib/CMakeLists.txt for build integration.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
python/resdata/grid/rd_region.py Switches ResdataRegion to use the new _rd_region pybind11 functions and adjusts copying/equality accordingly.
lib/resdata/rd_region_pybind.cpp Introduces the pybind11 module _rd_region implementing the rd_region API surface.
lib/CMakeLists.txt Adds _rd_region to the set of built pybind11 modules.
Comments suppressed due to low confidence (2)

lib/resdata/rd_region_pybind.cpp:238

  • The active index list is returned as a borrowed C reference but createCReference is called without a parent object. This breaks the expected .parent() relationship (see region tests) and can allow the IntVector to outlive the owning region. Pass self as the parent when creating the reference.
    m.def("_get_active_list", [](py::handle self) {
        return IntVector().attr("createCReference")(
            reinterpret_cast<std::uintptr_t>(
                rd_region_get_active_list(from_cwrap<rd_region_type>(self))));
    });

lib/resdata/rd_region_pybind.cpp:243

  • The global index list is created as a C reference without associating it with its owning region. To preserve lifetime/ownership semantics (and keep parity with other bindings using createCReference(..., self)), pass self as the parent.
    m.def("_get_global_list", [](py::handle self) {
        return IntVector().attr("createCReference")(
            reinterpret_cast<std::uintptr_t>(
                rd_region_get_global_list(from_cwrap<rd_region_type>(self))));
    });

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

Comment thread lib/resdata/rd_region_pybind.cpp

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

Comments suppressed due to low confidence (2)

lib/resdata/rd_region_pybind.cpp:420

  • The closing } // namespace at the end of the file matches the anonymous namespace that currently wraps PYBIND11_MODULE. If the module is moved to global scope, this trailing namespace-closer should be removed as well; otherwise you’ll end up with an extra unmatched brace or keep the init symbol hidden.
}
} // namespace

lib/resdata/rd_region.cpp:334

  • Throwing a string literal here (throw("...")) throws a const char* and bypasses std::exception-based handling (including pybind11’s default translators). Prefer std::invalid_argument (or similar) so the exception is well-typed and consistently translated.
    rd_data_type data_type = rd_kw_get_data_type(rd_kw);
    rd_region_assert_kw(region, rd_kw, &global_kw);
    if (!rd_type_is_numeric(data_type))
        throw("select by in_interval is only supported for float and integer "
              "keywords");

Comment thread lib/resdata/rd_region_pybind.cpp
Comment thread lib/resdata/rd_region.cpp
Comment thread lib/resdata/rd_region.cpp Outdated
@eivindjahren
eivindjahren force-pushed the pybind_the_rest branch 2 times, most recently from a6e950a to 7f3298c Compare July 24, 2026 12:01
@eivindjahren
eivindjahren requested a review from Copilot July 24, 2026 12:02

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

Comment thread python/resdata/summary/rd_sum_tstep.py
Comment thread python/resdata/summary/rd_sum_keyword_vector.py
Comment thread lib/include/resdata/rd_sum_vector.hpp
Comment thread lib/include/resdata/rd_sum_tstep.hpp
Comment thread lib/include/resdata/rd_region.hpp

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

Comment thread tests/rd_tests/test_sum.py
Comment thread lib/tests/test_rd_region.cpp
@eivindjahren
eivindjahren force-pushed the pybind_the_rest branch 2 times, most recently from 56acf04 to cbd327f Compare July 24, 2026 12:25
@eivindjahren
eivindjahren requested a review from Copilot July 24, 2026 12: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 16 out of 16 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

lib/resdata/rd_region.cpp:648

  • The error message for an invalid k-slice interval is incorrect: this branch checks k1 > k2 but throws "i1 > i2". The message should reflect the actual parameters being validated.
    if (k1 > k2)
        throw std::logic_error("i1 > i2");

Comment thread lib/resdata/rd_region.cpp Outdated
Comment thread lib/CMakeLists.txt
@eivindjahren eivindjahren changed the title Use pybind for rd_region Use pybind for rd_region, rd_sum_tstep and rd_sum_vector Jul 24, 2026

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

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.

2 participants