mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 13:15:12 +00:00
fix: enable py::implicitly_convertible<py::none, ...> for py::class_-wrapped types (#3059)
* Allow casting from None to a custom object, closes #2778 * ci.yml patch from the smart_holder branch for full CI coverage.
This commit is contained in:
parent
484b0f0433
commit
93e69191c1
26
.github/workflows/ci.yml
vendored
26
.github/workflows/ci.yml
vendored
@ -100,7 +100,7 @@ jobs:
|
||||
run: >
|
||||
cmake -S . -B .
|
||||
-DPYBIND11_WERROR=ON
|
||||
-DDOWNLOAD_CATCH=ON
|
||||
-DDOWNLOAD_CATCH=OFF
|
||||
-DDOWNLOAD_EIGEN=ON
|
||||
-DCMAKE_CXX_STANDARD=11
|
||||
${{ matrix.args }}
|
||||
@ -111,10 +111,10 @@ jobs:
|
||||
- name: Python tests C++11
|
||||
run: cmake --build . --target pytest -j 2
|
||||
|
||||
- name: C++11 tests
|
||||
# TODO: Figure out how to load the DLL on Python 3.8+
|
||||
if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))"
|
||||
run: cmake --build . --target cpptest -j 2
|
||||
#- name: C++11 tests
|
||||
# # TODO: Figure out how to load the DLL on Python 3.8+
|
||||
# if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))"
|
||||
# run: cmake --build . --target cpptest -j 2
|
||||
|
||||
- name: Interface test C++11
|
||||
run: cmake --build . --target test_cmake_build
|
||||
@ -127,7 +127,7 @@ jobs:
|
||||
run: >
|
||||
cmake -S . -B build2
|
||||
-DPYBIND11_WERROR=ON
|
||||
-DDOWNLOAD_CATCH=ON
|
||||
-DDOWNLOAD_CATCH=OFF
|
||||
-DDOWNLOAD_EIGEN=ON
|
||||
-DCMAKE_CXX_STANDARD=17
|
||||
${{ matrix.args }}
|
||||
@ -139,10 +139,10 @@ jobs:
|
||||
- name: Python tests
|
||||
run: cmake --build build2 --target pytest
|
||||
|
||||
- name: C++ tests
|
||||
# TODO: Figure out how to load the DLL on Python 3.8+
|
||||
if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))"
|
||||
run: cmake --build build2 --target cpptest
|
||||
#- name: C++ tests
|
||||
# # TODO: Figure out how to load the DLL on Python 3.8+
|
||||
# if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))"
|
||||
# run: cmake --build build2 --target cpptest
|
||||
|
||||
- name: Interface test
|
||||
run: cmake --build build2 --target test_cmake_build
|
||||
@ -754,7 +754,7 @@ jobs:
|
||||
cmake -S . -B build
|
||||
-G "Visual Studio 16 2019" -A Win32
|
||||
-DPYBIND11_WERROR=ON
|
||||
-DDOWNLOAD_CATCH=ON
|
||||
-DDOWNLOAD_CATCH=OFF
|
||||
-DDOWNLOAD_EIGEN=ON
|
||||
${{ matrix.args }}
|
||||
- name: Build C++11
|
||||
@ -800,7 +800,7 @@ jobs:
|
||||
cmake -S . -B build
|
||||
-G "Visual Studio 14 2015" -A x64
|
||||
-DPYBIND11_WERROR=ON
|
||||
-DDOWNLOAD_CATCH=ON
|
||||
-DDOWNLOAD_CATCH=OFF
|
||||
-DDOWNLOAD_EIGEN=ON
|
||||
|
||||
- name: Build C++14
|
||||
@ -849,7 +849,7 @@ jobs:
|
||||
cmake -S . -B build
|
||||
-G "Visual Studio 15 2017" -A x64
|
||||
-DPYBIND11_WERROR=ON
|
||||
-DDOWNLOAD_CATCH=ON
|
||||
-DDOWNLOAD_CATCH=OFF
|
||||
-DDOWNLOAD_EIGEN=ON
|
||||
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
|
||||
${{ matrix.args }}
|
||||
|
@ -657,12 +657,6 @@ public:
|
||||
PYBIND11_NOINLINE bool load_impl(handle src, bool convert) {
|
||||
if (!src) return false;
|
||||
if (!typeinfo) return try_load_foreign_module_local(src);
|
||||
if (src.is_none()) {
|
||||
// Defer accepting None to other overloads (if we aren't in convert mode):
|
||||
if (!convert) return false;
|
||||
value = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
auto &this_ = static_cast<ThisT &>(*this);
|
||||
this_.check_holder_compat();
|
||||
@ -731,7 +725,19 @@ public:
|
||||
}
|
||||
|
||||
// Global typeinfo has precedence over foreign module_local
|
||||
return try_load_foreign_module_local(src);
|
||||
if (try_load_foreign_module_local(src)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Custom converters didn't take None, now we convert None to nullptr.
|
||||
if (src.is_none()) {
|
||||
// Defer accepting None to other overloads (if we aren't in convert mode):
|
||||
if (!convert) return false;
|
||||
value = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
@ -118,6 +118,14 @@ int none3(std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
|
||||
int none4(std::shared_ptr<NoneTester> *obj) { return obj && *obj ? (*obj)->answer : -1; }
|
||||
int none5(const std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
|
||||
|
||||
// Issue #2778: implicit casting from None to object (not pointer)
|
||||
class NoneCastTester {
|
||||
public:
|
||||
int answer = -1;
|
||||
NoneCastTester() = default;
|
||||
NoneCastTester(int v) : answer(v) {};
|
||||
};
|
||||
|
||||
struct StrIssue {
|
||||
int val = -1;
|
||||
|
||||
@ -354,6 +362,16 @@ TEST_SUBMODULE(methods_and_attributes, m) {
|
||||
m.def("no_none_kwarg", &none2, "a"_a.none(false));
|
||||
m.def("no_none_kwarg_kw_only", &none2, py::kw_only(), "a"_a.none(false));
|
||||
|
||||
// test_casts_none
|
||||
// Issue #2778: implicit casting from None to object (not pointer)
|
||||
py::class_<NoneCastTester>(m, "NoneCastTester")
|
||||
.def(py::init<>())
|
||||
.def(py::init<int>())
|
||||
.def(py::init([](py::none const&) { return NoneCastTester{}; }));
|
||||
py::implicitly_convertible<py::none, NoneCastTester>();
|
||||
m.def("ok_obj_or_none", [](NoneCastTester const& foo) { return foo.answer; });
|
||||
|
||||
|
||||
// test_str_issue
|
||||
// Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
|
||||
py::class_<StrIssue>(m, "StrIssue")
|
||||
|
@ -431,6 +431,17 @@ def test_accepts_none(msg):
|
||||
assert "incompatible function arguments" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_casts_none(msg):
|
||||
"""#2778: implicit casting from None to object (not pointer)"""
|
||||
a = m.NoneCastTester()
|
||||
assert m.ok_obj_or_none(a) == -1
|
||||
a = m.NoneCastTester(4)
|
||||
assert m.ok_obj_or_none(a) == 4
|
||||
a = m.NoneCastTester(None)
|
||||
assert m.ok_obj_or_none(a) == -1
|
||||
assert m.ok_obj_or_none(None) == -1
|
||||
|
||||
|
||||
def test_str_issue(msg):
|
||||
"""#283: __str__ called on uninitialized instance when constructor arguments invalid"""
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user