pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

Properly translate C++ exception to Python exception when creating Python buffer from wrapped object

Open vnlitvinov opened this issue 1 year ago • 1 comments

Description

  • Refactor exception translation a little - extract inline translation to a callable helper;
  • Call the helper if an exception occurs when trying to get a buffer from wrapped object;
  • Added the test, which crashes with Fatal Python error without the fix (unhandlable from within Python) and passes after it is done.

Closes: #2764, #3336

Suggested changelog entry:

* Properly translate C++ exception to Python exception when creating Python buffer from wrapped object

vnlitvinov avatar Aug 21 '24 18:08 vnlitvinov

I need to find a block of time to look carefully.

But high-level: There is already too much stuff in internals.h, adding in the exception translator code seems very unfortunate.

pybind11 is poorly factored in general, I'd love to not make it worse, even though it can get tricky sometimes.

Could you please try to move the exception translator code into its own header, maybe pybind11/detail/exception_translators.h?

You'll have to update a couple files when adding a new header. An easy way to pin-point the locations is e.g.:

$ git grep detail/value_and_holder.h
CMakeLists.txt:    include/pybind11/detail/value_and_holder.h
tests/extra_python_package/test_files.py:    "include/pybind11/detail/value_and_holder.h",

rwgk avatar Aug 23 '24 18:08 rwgk

Thanks a lot @EthanSteinberg for the review!

rwgk avatar Sep 02 '24 16:09 rwgk