Skip to content

pybind: Add S1Interval bindings (#524)#534

Open
deustis wants to merge 6 commits intogoogle:masterfrom
deustis:deustis/s1interval_bindings
Open

pybind: Add S1Interval bindings (#524)#534
deustis wants to merge 6 commits intogoogle:masterfrom
deustis:deustis/s1interval_bindings

Conversation

@deustis
Copy link
Contributor

@deustis deustis commented Jan 28, 2026

Add pybind11 bindings for S1Interval.

Also add interface notes to the python README

(Part of a series of addressing #524)

@deustis deustis force-pushed the deustis/s1interval_bindings branch 4 times, most recently from 596f5f6 to 85ae427 Compare January 28, 2026 00:42
Add pybind11 bindings for S1Interval.

Also add interface notes to the python README
@deustis deustis force-pushed the deustis/s1interval_bindings branch from 85ae427 to 5bb24fa Compare January 28, 2026 00:49
@deustis
Copy link
Contributor Author

deustis commented Jan 28, 2026

@jmr, next installment here. I've added some updates to the README that explain some of the user-facing interface choices for the python bindings. That might be a good place to start for the review. Feel free to suggest any changes.

@deustis deustis changed the title pybind: Add S2Interval bindings (#524) pybind: Add S1Interval bindings (#524) Jan 28, 2026
@deustis
Copy link
Contributor Author

deustis commented Jan 28, 2026

@jmr, thanks for the detailed comments. I think once we iron out some of these interface choices it will be smooth sailing. Definitely worth spending the time up-front to ensure consistency/correctness.

I've addressed most of your comments. The main open question is about how to handle the debug assertions. Let me know your thoughts there and I'll make those changes.

@jmr
Copy link
Member

jmr commented Jan 30, 2026

Is there a reason you picked S1Interval? Seems obscure and low level.

@deustis
Copy link
Contributor Author

deustis commented Feb 9, 2026

Hi there, just a quick note -- I was traveling last week and working against some deadlines this week but still intending to circle back on this!

Is there a reason you picked S1Interval? Seems obscure and low level.

No particular reason... starting with the lowest level primitives and working my way up to S2Cell.

@deustis
Copy link
Contributor Author

deustis commented Feb 21, 2026

@jmr, apologies for the delay. I'm back and have updated the the PR as discussed.

The python interface will now raise value errors for invalid inputs to avoid any possibility of triggering the DCHECKS. In the case of S1Angle this basically means ensuring all of the points are within +/- pi. I've added a static helper to the C++ code to make this easier so we aren't having to do any math in the bindings themselves.

@deustis deustis mentioned this pull request Feb 22, 2026
@deustis
Copy link
Contributor Author

deustis commented Feb 25, 2026

@jmr, friendly ping on this PR? I believe I've addressed your earlier comments.

@jmr
Copy link
Member

jmr commented Feb 25, 2026

I will take a closer look at this tomorrow.

namespace {

void MaybeThrowInvalidPoint(double p) {
if (!S1Interval::IsValidPoint(p)) throw py::value_error("Invalid S1 point");
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, so we actually throw a C++ exception here. Is there a way to make this work without C++ exceptions, like returning an error-like type that then gets converted to an exception in Python?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm pretty sure this is the recommended approach:
https://pybind11.readthedocs.io/en/stable/advanced/exceptions.html

I haven't found any other ways to make pybind raise a Python exception without throwing a C++ exception.

Copy link
Member

Choose a reason for hiding this comment

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

Add a comment to ehe BUILD and/or README that exceptions need to be enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, added to README in the Invalid Values section

@deustis deustis force-pushed the deustis/s1interval_bindings branch from 52fb522 to 3137a7d Compare February 26, 2026 16:49
Comment on lines 11 to 13

void MaybeThrowInvalidPoint(double p) {
if (!S1Interval::IsValidPoint(p)) throw py::value_error("Invalid S1 point: " + std::to_string(p));
Copy link
Member

Choose a reason for hiding this comment

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

Line length.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@deustis deustis requested a review from jmr February 27, 2026 16:36
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