Skip to content

Check cwrap types when calling via pybind#1191

Open
eivindjahren wants to merge 3 commits into
mainfrom
clean_up_pybind
Open

Check cwrap types when calling via pybind#1191
eivindjahren wants to merge 3 commits into
mainfrom
clean_up_pybind

Conversation

@eivindjahren

@eivindjahren eivindjahren commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Adds TypeError for calls to methods that would lead to undefined behavior.

In order for this to not be a breaking change, we have to be sure that the previous behavior was not defined, at least not in documentation.

The simplest case is that whenever from_cwrap<T>(self) is called, somehow inserting anything other than the instance as self (is that even possible?) would not be supported.

Here are some of the less simple cases:

In the call to _rd_sum._fread_alloc, data_files is a local variable, so this is unproblematic. Similarly, the following have also always been unproblematic:

  • _rd_sum._init_pandas_frame, keywords
  • _rd_sum._init_pandas_frame_interp, keywords
  • _rd_sum._init_pandas_frame_interp, time_points
  • _rd_sum._dump_csv_line, key_words
  • _rd_sum._export_csv, var_list
  • _grid._fprintf_grdecl2, output_unit
  • _grid._fprintf_GRID2, output_unit
  • _grid._fprintf_EGRID2, output_unit
  • _grid._compressed_kw_copy, kw_copy
  • _grid._global_kw_copy, kw_copy
  • _kw._fprintf_grdecl, file
  • _grid._fwrite_grdecl, file
  • _grid._fprintf_grdecl2, file
  • _rd_sum._dump_csv_line, file
  • _grid._equal, other

For the following function, parameter combinations the following holds: In the call to the function, giving None or an invalid BaseCClass would result in UB. After this change all UB cases are TypeError.

  • _rd_sum._resample, time_points
  • _grid._grdecl_create, zcorn
  • _grid._grdecl_create, coord
  • _grid._fwrite_grdecl, kw
  • _grid._load_column, kw
  • _grid._load_column, column
  • _grid._fprintf_grdecl2, kw
  • _kw._load_grdecl, cfile
  • _kw._load_grdecl, kw

The following would give a different exception if given None, but potentially a UB if a different BaseCClass. We have now introduced a TypeError instead.

  • _grid._compressed_kw_copy, kw
  • _grid._global_kw_copy, kw
  • _grid._export_data_as_int, kw
  • _grid._export_data_as_double, kw

For _grid._grdecl_create, actnumand_grid._grdecl_create, mapaxes, the only difference is that these could always be None. However, we have now introduced a TypeError if not the correct BaseCClass.

@eivindjahren eivindjahren force-pushed the clean_up_pybind branch 4 times, most recently from 6500bfd to 62601e0 Compare June 15, 2026 11:29
@eivindjahren eivindjahren requested a review from ajaust June 15, 2026 12:14
@eivindjahren eivindjahren moved this to Ready for Review in SCOUT Jun 15, 2026
@eivindjahren eivindjahren requested a review from Copilot June 15, 2026 12:15

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 centralizes the pybind “cwrap handle → native pointer” conversion logic and adds runtime type-checking to prevent undefined behavior when Python callers pass incorrect object types into pybind/C++ entrypoints.

Changes:

  • Introduces a shared from_cwrap<T>(handle) implementation that validates Python object types before extracting _BaseCClass__c_pointer.
  • Updates _grid, _kw, _rd_sum, and _fortio pybind modules to use the shared helper and adds an optional-kw variant for nullable keyword parameters.
  • Adds/updates Python tests to assert TypeError is raised for wrong-type inputs instead of risking UB.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
tests/rd_tests/test_rd_sum.py Adds tests ensuring wrong-type keywords / time_points raise TypeError.
tests/rd_tests/test_rd_grid_misc.py Switches to public Grid.load_column and adds wrong-type argument TypeError tests.
tests/rd_tests/test_grid.py Adds Grid.create wrong-type argument TypeError tests and verifies None is accepted where intended.
lib/resdata/rd_sum_pybind.cpp Replaces local cwrap casting helper with shared implementation.
lib/resdata/rd_kw_pybind.cpp Replaces local cwrap casting helper with shared implementation.
lib/resdata/rd_grid_pybind.cpp Replaces local cwrap casting helper with shared implementation and uses nullable kw conversion for actnum/mapaxes.
lib/resdata/fortio_pybind.cpp Replaces local cwrap casting helper with shared implementation.
lib/resdata/cwrap_pybind.cpp New shared implementation providing typed from_cwrap<T> specializations.
lib/private-include/detail/resdata/cwrap_pybind.hpp New header exposing from_cwrap<T> and optional-kw helper to pybind modules.
lib/CMakeLists.txt Adds cwrap_pybind.cpp to all relevant pybind extension modules and updates include paths.

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

Comment on lines +5 to +8
#include <resdata/rd_kw.hpp>

template <typename T> T *from_cwrap(pybind11::handle obj);
rd_kw_type *from_cwrap_opt_kw(pybind11::handle obj);
Comment thread lib/resdata/cwrap_pybind.cpp Outdated
Comment thread lib/resdata/cwrap_pybind.cpp Outdated
Comment thread lib/resdata/cwrap_pybind.cpp
Comment thread tests/rd_tests/test_rd_grid_misc.py Outdated
Comment thread tests/rd_tests/test_grid.py Outdated
Comment thread tests/rd_tests/test_grid.py Outdated
Comment thread tests/rd_tests/test_grid.py Outdated

@ajaust ajaust 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.

Nice changes! I only have a single point that might need validation (about changing some target_include_directories).

Comment thread lib/CMakeLists.txt Outdated
@github-project-automation github-project-automation Bot moved this from Ready for Review to Reviewed in SCOUT Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed

Development

Successfully merging this pull request may close these issues.

3 participants