pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.11k stars 2.05k forks source link

Enable `"factory sets error and returns nullptr"` #4846

Open rwgk opened 10 months ago

rwgk commented 10 months ago

Description

(The equivalent of this PR was merged as https://github.com/google/pywrapcc/pull/30064 on September 12, 2023.)

This is a very simple change in support of the PyCLIF-pybind11 integration work:

Merely an extra if to pre-empt TypeError: pybind11::init(): factory function returned nullptr. This allows the factory function to set a custom Python error instead.

Manually written factory functions could throw error_already_set() directly, but in the context of PyCLIF that is very difficult (compared to this very simple PR), because the factory function can be completely unaware of pybind11.

Suggested changelog entry:

Python errors set in constructor factory functions are no longer clobbered by a generic exception.