Skip to content

Feature/adr 003 pybind11#24

Merged
simondsmart merged 1 commit into
mainfrom
feature/ADR-002-pybind11
Sep 22, 2025
Merged

Feature/adr 003 pybind11#24
simondsmart merged 1 commit into
mainfrom
feature/ADR-002-pybind11

Conversation

@tbkr

@tbkr tbkr commented Aug 1, 2025

Copy link
Copy Markdown
Contributor

Description

Contributor Declaration

By opening this pull request, I affirm the following:

  • All authors agree to the Contributor License Agreement.
  • The code follows the project's coding standards.
  • I have performed self-review and added comments where needed.
  • I have added or updated tests to verify that my changes are effective and functional.
  • I have run all existing tests and confirmed they pass.

while explicitly having reduced compatibility with `std` and third party modules.
It is not header-only and requires linking to a shared or static library.

[ ] Listed all seriously considered alternatives

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.

would we consider Cython an Option here? Some of our cpp2python interfaces are built with it (eckit, mir)

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.

Done.

Comment thread ADR/ADR-002-PyBind11-For-CPP-Bindings.md Outdated
@marcosbento

Copy link
Copy Markdown

+1, for adoption of pybind11.

As a matter of fact, I've spent a couple of afternoons and converted ecflow's python extension from Boost.python to pybind11 (the result is still in a local branch, since the CI runners don't have pybind11).

The result is quite admirable, and the support for std type allowed to simplify parts of the existing implementation.
Moving away from Boost.python, and specifically not having to link against static libraries, is a great added bonus!

@pgeier pgeier force-pushed the feature/ADR-002-pybind11 branch 2 times, most recently from 7c419b4 to 6141180 Compare September 18, 2025 08:29
@pgeier pgeier changed the title Feature/adr 002 pybind11 Feature/adr 003 pybind11 Sep 18, 2025
@pgeier pgeier force-pushed the feature/ADR-002-pybind11 branch from 6141180 to fad5245 Compare September 18, 2025 08:32
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
* call C functions: python code can now call C functions and manipulate C data
structures as if they were native Python objects

Using CFFI usally requires to write a certain amount of wrapping code to hide

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.

I would go beyond this. It typically requires a 2-layer python interface.

One layer that uses the C-interface, and one layer on top to make it more pythonic.

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.

Reformulated the CFFI parts.

Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
## Decision

The decision is to use Pybind11 for C++ bindings (see [analysis](#analysis)).
Developers should follow the [design guidline](#design-guidlines).

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.

Where we do not need/envisage fortran bindings.

Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md
@pgeier pgeier force-pushed the feature/ADR-002-pybind11 branch 2 times, most recently from 15a19f8 to 79a8854 Compare September 18, 2025 14:48

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

While I understand and share the enthusiasm around PyBind11, I am not sure I am convinced it is a better alternative than Cython.

Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
Comment thread ADR/ADR-003-PyBind11-For-CPP-Bindings.md Outdated
to call C functions and use C data types. With CFFI it is possible to

* Extract C declarations from your to-be-wrapped API, note that this is a
preprocessing step because CFFI cannot parse C or C++ header files.

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.

Not true. CFFI does pars C header files. But is quite restricted on what it can parse.

[here](https://pybind11.readthedocs.io/en/stable/advanced/cast/overview.html#conversion-table).
* Allows exchanging wrapped types across bindings, see [module local
bindings](https://pybind11.readthedocs.io/en/stable/advanced/classes.html#module-local-class-bindings)
* Allows setting call policies that affect object lifetimes. These policies allow to

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.

"These policies enable the definition of dependencies ..." or similar. "allow to" is bad English.

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

Looking good. I'm happy.

@Ozaq Ozaq force-pushed the feature/ADR-002-pybind11 branch from c00f23f to 09c914a Compare September 22, 2025 10:56

@tlmquintino tlmquintino left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

minor comments. needs a References section.

## Status

[**Proposed** | <s>Accepted</s> | <s>Deprecated</s> | <s>Superseded by [ADR-XXX]</s>]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remember to change to Accepted before merging.

[bindings](https://github.com/ecmwf/multio/blob/feature/multiom-cpp-python/src/pymars2grib_bindings/pymars2grib_bindings.cc),
[python
wrapper)(https://github.com/ecmwf/multio/tree/feature/multiom-cpp-python/src/pymars2grib)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

there is a References section missing (see template).
I would suggest to add references to other similar discussions that you may or not have used, e.g:

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.

Will do

@Ozaq Ozaq force-pushed the feature/ADR-002-pybind11 branch 2 times, most recently from 45edae9 to 61659a8 Compare September 22, 2025 13:03
* Automatically translates C++ exceptions to matching Python exceptions, see
[details](https://pybind11.readthedocs.io/en/stable/advanced/exceptions.html).
This includes the ability to register custom type conversions that map domain specific
exceptions, e.g. `eckit::Exception` to `eckit.Excpeption`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
exceptions, e.g. `eckit::Exception` to `eckit.Excpeption`.
exceptions, e.g. `eckit::Exception` to `eckit.Exception`.

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.

Done

Co-authored-by: Kai Kratz <kai.kratz@ecmwf.int>
Co-authored-by: Tobias Kremer <tobias.kremer@ecmwf.int>
@Ozaq Ozaq force-pushed the feature/ADR-002-pybind11 branch from 61659a8 to 974f1ee Compare September 22, 2025 14:43
@simondsmart simondsmart merged commit 7c3be8a into main Sep 22, 2025
1 check passed
@simondsmart simondsmart deleted the feature/ADR-002-pybind11 branch September 22, 2025 15:08
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.

8 participants