mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-08 09:51:57 +00:00
Transfer reduced test here.
Reduced from a PyCLIF use case in the wild by @wangxf123456 (internal change cl/565476030).
This commit is contained in:
parent
4dbe657648
commit
c684f6cec0
@ -134,6 +134,7 @@ set(PYBIND11_TEST_FILES
|
|||||||
test_class_sh_trampoline_shared_from_this
|
test_class_sh_trampoline_shared_from_this
|
||||||
test_class_sh_trampoline_shared_ptr_cpp_arg
|
test_class_sh_trampoline_shared_ptr_cpp_arg
|
||||||
test_class_sh_trampoline_unique_ptr
|
test_class_sh_trampoline_unique_ptr
|
||||||
|
test_class_sh_unique_ptr_custom_deleter
|
||||||
test_class_sh_unique_ptr_member
|
test_class_sh_unique_ptr_member
|
||||||
test_class_sh_virtual_py_cpp_mix
|
test_class_sh_virtual_py_cpp_mix
|
||||||
test_class_sh_void_ptr_capsule
|
test_class_sh_void_ptr_capsule
|
||||||
|
40
tests/test_class_sh_unique_ptr_custom_deleter.cpp
Normal file
40
tests/test_class_sh_unique_ptr_custom_deleter.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include <pybind11/smart_holder.h>
|
||||||
|
|
||||||
|
#include "pybind11_tests.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace pybind11_tests {
|
||||||
|
namespace class_sh_unique_ptr_custom_deleter {
|
||||||
|
|
||||||
|
// Reduced from a PyCLIF use case in the wild by @wangxf123456.
|
||||||
|
class Pet {
|
||||||
|
public:
|
||||||
|
using Ptr = std::unique_ptr<Pet, std::function<void(Pet *)>>;
|
||||||
|
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
static Ptr New(const std::string &name) {
|
||||||
|
return Ptr(new Pet(name), std::default_delete<Pet>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit Pet(const std::string &name) : name(name) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace class_sh_unique_ptr_custom_deleter
|
||||||
|
} // namespace pybind11_tests
|
||||||
|
|
||||||
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_unique_ptr_custom_deleter::Pet)
|
||||||
|
|
||||||
|
namespace pybind11_tests {
|
||||||
|
namespace class_sh_unique_ptr_custom_deleter {
|
||||||
|
|
||||||
|
TEST_SUBMODULE(class_sh_unique_ptr_custom_deleter, m) {
|
||||||
|
py::classh<Pet>(m, "Pet").def_readwrite("name", &Pet::name);
|
||||||
|
|
||||||
|
m.def("create", &Pet::New);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace class_sh_unique_ptr_custom_deleter
|
||||||
|
} // namespace pybind11_tests
|
6
tests/test_class_sh_unique_ptr_custom_deleter.py
Normal file
6
tests/test_class_sh_unique_ptr_custom_deleter.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from pybind11_tests import class_sh_unique_ptr_custom_deleter as m
|
||||||
|
|
||||||
|
|
||||||
|
def test_create():
|
||||||
|
pet = m.create("abc")
|
||||||
|
assert pet.name == "abc"
|
Loading…
Reference in New Issue
Block a user