pybind11/include/pybind11
Ralf W. Grosse-Kunstleve e9b961d9b9
Elide to-python conversion of setter return values (#4621)
* Reproducer for property setter with return type that is not wrapped.

* Use `py::class_<OptionsBase>()` to work around the return value conversion issue.

* WIP drop_return_value

* Remove struct drop_return_value

* Introduce `return_value_policy::return_none` for use by setters.

* Add `is_setter` to attr.h and use from `.def_property()`

* Merge the new test into test_methods_and_attributes

* Remove return_none return_value_policy again.

* Fix oversight (NOLINTNEXTLINE placement).

* Simplification (for the better) found while searching for a way to resolve GCC build failures.

Example of failure resolved by this change:

g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

```
cd /build/tests && /usr/bin/c++ -DPYBIND11_TEST_EIGEN -Dpybind11_tests_EXPORTS -I/mounted_pybind11/include -isystem /usr/include/python3.8 -isystem /build/_deps/eigen-src -g -std=c++17 -fPIC -fvisibility=hidden -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -MD -MT tests/CMakeFiles/pybind11_tests.dir/test_buffers.cpp.o -MF CMakeFiles/pybind11_tests.dir/test_buffers.cpp.o.d -o CMakeFiles/pybind11_tests.dir/test_buffers.cpp.o -c /mounted_pybind11/tests/test_buffers.cpp
In file included from /mounted_pybind11/include/pybind11/stl.h:12,
                 from /mounted_pybind11/tests/test_buffers.cpp:10:
/mounted_pybind11/include/pybind11/pybind11.h: In instantiation of ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_property(const char*, const Getter&, const Setter&, const Extra& ...) [with Getter = pybind11::cpp_function; Setter = std::nullptr_t; Extra = {pybind11::return_value_policy}; type_ = pybind11::buffer_info; options = {}]’:
/mounted_pybind11/include/pybind11/pybind11.h:1716:58:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_property_readonly(const char*, const pybind11::cpp_function&, const Extra& ...) [with Extra = {pybind11::return_value_policy}; type_ = pybind11::buffer_info; options = {}]’
/mounted_pybind11/include/pybind11/pybind11.h:1684:9:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_readonly(const char*, const D C::*, const Extra& ...) [with C = pybind11::buffer_info; D = long int; Extra = {}; type_ = pybind11::buffer_info; options = {}]’
/mounted_pybind11/tests/test_buffers.cpp:209:61:   required from here
/mounted_pybind11/include/pybind11/pybind11.h:1740:25: error: call of overloaded ‘cpp_function(std::nullptr_t&, pybind11::is_setter)’ is ambiguous
 1740 |             name, fget, cpp_function(method_adaptor<type>(fset), is_setter()), extra...);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/mounted_pybind11/include/pybind11/pybind11.h:101:5: note: candidate: ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = std::nullptr_t&; Extra = {pybind11::is_setter}; <template-parameter-1-3> = void]’
  101 |     cpp_function(Func &&f, const Extra &...extra) {
      |     ^~~~~~~~~~~~
In file included from /mounted_pybind11/include/pybind11/stl.h:12,
                 from /mounted_pybind11/tests/test_buffers.cpp:10:
/mounted_pybind11/include/pybind11/pybind11.h:87:5: note: candidate: ‘pybind11::cpp_function::cpp_function(std::nullptr_t, const Extra& ...) [with Extra = {pybind11::is_setter}; std::nullptr_t = std::nullptr_t]’
   87 |     cpp_function(std::nullptr_t, const Extra &...) {}
      |     ^~~~~~~~~~~~
```

* Bug fix: obvious in hindsight. I thought the original version was incrementing the reference count for None, but no.

Discovered via many failing tests in the wild (10s of thousands).

It is very tricky to construct a meaningful unit test for this bug specifically. It's unlikely to come back, because 10s of thousands of tests will fail again.
2023-05-08 10:13:54 -07:00
..
detail Add type_caster<PyObject> (#4601) 2023-05-07 10:15:53 -07:00
eigen Make warning suppressions MINGW-specific again. (#4515) 2023-02-17 12:58:35 -08:00
stl feature: support compilers that use std::experimental::filesystem (#3840) 2022-03-31 22:16:10 -07:00
attr.h Elide to-python conversion of setter return values (#4621) 2023-05-08 10:13:54 -07:00
buffer_info.h Fully-automatic clang-format with include reordering (#3713) 2022-02-10 12:17:07 -08:00
cast.h Add type_caster<PyObject> (#4601) 2023-05-07 10:15:53 -07:00
chrono.h chore: drop Python 3.5 (#3719) 2022-02-11 19:06:16 -05:00
common.h Add a dummy common.h header with a deprecation warning 2017-09-06 15:22:26 +02:00
complex.h Fully-automatic clang-format with include reordering (#3713) 2022-02-10 12:17:07 -08:00
eigen.h First draft of Eigen::Tensor support (#4201) 2022-10-18 16:54:16 -07:00
embed.h Introduce get_python_state_dict() for Python 3.12 compatibility. (#4570) 2023-03-27 17:52:57 -07:00
eval.h fix: missing move in eval.h (#3775) 2022-03-02 15:25:43 -05:00
functional.h Fix functional.h bug + introduce test to verify that it is fixed (#4254) 2022-11-02 11:32:53 -07:00
gil.h bugfix: delete proper ctors in gil.h (#4490) 2023-02-04 13:40:13 -05:00
iostream.h chore: add err guard to capsule destructor and add a move to iostream (#3958) 2022-05-27 14:32:57 -04:00
numpy.h Inconsistent comments between 2 templates of unchecked(). (#4519) 2023-02-20 14:58:37 -08:00
operators.h Reproducer and fix for issue encountered in smart_holder update. (#4228) 2022-10-09 21:50:35 -07:00
options.h Add option for enable/disable enum members in docstring. (#2768) 2022-12-08 23:10:10 -08:00
pybind11.h Elide to-python conversion of setter return values (#4621) 2023-05-08 10:13:54 -07:00
pytypes.h fix: issuses detected by static analyzer (#4440) 2023-01-03 10:20:39 -05:00
stl_bind.h Introduce recursive_container_traits (#4623) 2023-05-04 22:39:05 -07:00
stl.h chore: update clang-tidy to 15 (#4387) 2022-12-27 15:14:10 -05:00
type_caster_pyobject_ptr.h Add type_caster<PyObject> (#4601) 2023-05-07 10:15:53 -07:00