10#if defined(__INTEL_COMPILER) && __cplusplus >= 201703L
14# include <aligned_new>
17#include <pybind11/stl.h>
26# pragma warning(disable : 4324)
41 struct NoConstructor {
42 NoConstructor() =
default;
43 NoConstructor(
const NoConstructor &) =
default;
44 NoConstructor(NoConstructor &&) =
default;
45 static NoConstructor *new_instance() {
46 auto *ptr =
new NoConstructor();
52 struct NoConstructorNew {
53 NoConstructorNew() =
default;
54 NoConstructorNew(
const NoConstructorNew &) =
default;
55 NoConstructorNew(NoConstructorNew &&) =
default;
56 static NoConstructorNew *new_instance() {
57 auto *ptr =
new NoConstructorNew();
64 py::class_<NoConstructor>(m,
"NoConstructor")
65 .def_static(
"new_instance", &NoConstructor::new_instance,
"Return an instance");
67 py::class_<NoConstructorNew>(m,
"NoConstructorNew")
68 .def(py::init([](
const NoConstructorNew &
self) {
return self; }))
69 .def_static(
"__new__",
70 [](
const py::object &) {
return NoConstructorNew::new_instance(); });
75 Pet(
const std::string &
name,
const std::string &species)
76 : m_name(
name), m_species(species) {}
77 std::string
name()
const {
return m_name; }
78 std::string species()
const {
return m_species; }
82 std::string m_species;
85 class Dog :
public Pet {
87 explicit Dog(
const std::string &
name) : Pet(
name,
"dog") {}
88 std::string
bark()
const {
return "Woof!"; }
91 class Rabbit :
public Pet {
93 explicit Rabbit(
const std::string &
name) : Pet(
name,
"parrot") {}
96 class Hamster :
public Pet {
98 explicit Hamster(
const std::string &
name) : Pet(
name,
"rodent") {}
101 class Chimera :
public Pet {
102 Chimera() : Pet(
"Kimmy",
"chimera") {}
105 py::class_<Pet> pet_class(m,
"Pet");
106 pet_class.def(py::init<std::string, std::string>())
107 .def(
"name", &Pet::name)
108 .def(
"species", &Pet::species);
111 py::class_<Dog>(m,
"Dog", pet_class).def(py::init<std::string>());
114 py::class_<Rabbit, Pet>(m,
"Rabbit").def(py::init<std::string>());
117 py::class_<Hamster, Pet>(m,
"Hamster").def(py::init<std::string>());
120 py::class_<Chimera, Pet>(m,
"Chimera");
122 m.def(
"pet_name_species",
123 [](
const Pet &pet) {
return pet.name() +
" is a " + pet.species(); });
124 m.def(
"dog_bark", [](
const Dog &dog) {
return dog.
bark(); });
128 BaseClass() =
default;
129 BaseClass(
const BaseClass &) =
default;
130 BaseClass(BaseClass &&) =
default;
131 virtual ~BaseClass() =
default;
133 struct DerivedClass1 : BaseClass {};
134 struct DerivedClass2 : BaseClass {};
136 py::class_<BaseClass>(m,
"BaseClass").def(py::init<>());
137 py::class_<DerivedClass1>(m,
"DerivedClass1").def(py::init<>());
138 py::class_<DerivedClass2>(m,
"DerivedClass2").def(py::init<>());
140 m.def(
"return_class_1", []() -> BaseClass * {
return new DerivedClass1(); });
141 m.def(
"return_class_2", []() -> BaseClass * {
return new DerivedClass2(); });
142 m.def(
"return_class_n", [](
int n) -> BaseClass * {
144 return new DerivedClass1();
147 return new DerivedClass2();
149 return new BaseClass();
151 m.def(
"return_none", []() -> BaseClass * {
return nullptr; });
154 m.def(
"check_instances", [](
const py::list &l) {
155 return py::make_tuple(py::isinstance<py::tuple>(l[0]),
156 py::isinstance<py::dict>(l[1]),
157 py::isinstance<Pet>(l[2]),
158 py::isinstance<Pet>(l[3]),
159 py::isinstance<Dog>(l[4]),
160 py::isinstance<Rabbit>(l[5]),
161 py::isinstance<UnregisteredType>(l[6]));
167 m.def(
"check_type", [](
int category) {
173 return py::type::of<DerivedClass1>();
175 return py::type::of<Invalid>();
178 m.def(
"get_type_of", [](py::object ob) {
return py::type::of(std::move(ob)); });
180 m.def(
"get_type_classic", [](py::handle h) {
return h.get_type(); });
182 m.def(
"as_type", [](
const py::object &ob) {
return py::type(ob); });
185 struct MismatchBase1 {};
186 struct MismatchDerived1 : MismatchBase1 {};
188 struct MismatchBase2 {};
189 struct MismatchDerived2 : MismatchBase2 {};
191 m.def(
"mismatched_holder_1", []() {
192 auto mod = py::module_::import(
"__main__");
193 py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(mod,
"MismatchBase1");
194 py::class_<MismatchDerived1, MismatchBase1>(mod,
"MismatchDerived1");
196 m.def(
"mismatched_holder_2", []() {
197 auto mod = py::module_::import(
"__main__");
198 py::class_<MismatchBase2>(mod,
"MismatchBase2");
199 py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>, MismatchBase2>(
200 mod,
"MismatchDerived2");
206 static std::unique_ptr<MyBase> make() {
return std::unique_ptr<MyBase>(
new MyBase()); }
209 struct MyDerived : MyBase {
210 static std::unique_ptr<MyDerived> make() {
211 return std::unique_ptr<MyDerived>(
new MyDerived());
215 py::class_<MyBase>(m,
"MyBase").def_static(
"make", &MyBase::make);
217 py::class_<MyDerived, MyBase>(m,
"MyDerived")
218 .def_static(
"make", &MyDerived::make)
219 .def_static(
"make2", &MyDerived::make);
222 struct ConvertibleFromUserType {
225 explicit ConvertibleFromUserType(
UserType u) : i(u.
value()) {}
228 py::class_<ConvertibleFromUserType>(m,
"AcceptsUserType").def(py::init<UserType>());
229 py::implicitly_convertible<UserType, ConvertibleFromUserType>();
231 m.def(
"implicitly_convert_argument", [](
const ConvertibleFromUserType &r) {
return r.i; });
232 m.def(
"implicitly_convert_variable", [](
const py::object &o) {
236 const auto &r = o.cast<
const ConvertibleFromUserType &>();
239 m.add_object(
"implicitly_convert_variable_fail", [&] {
240 auto f = [](PyObject *, PyObject *
args) -> PyObject * {
241 auto o = py::reinterpret_borrow<py::tuple>(
args)[0];
243 o.cast<
const ConvertibleFromUserType &>();
244 }
catch (
const py::cast_error &e) {
245 return py::str(e.what()).release().ptr();
247 return py::str().release().ptr();
250 auto *def =
new PyMethodDef{
"f", f, METH_VARARGS,
nullptr};
251 py::capsule def_capsule(def,
252 [](
void *ptr) {
delete reinterpret_cast<PyMethodDef *
>(ptr); });
253 return py::reinterpret_steal<py::object>(
254 PyCFunction_NewEx(def, def_capsule.ptr(), m.ptr()));
260 static void *
operator new(
size_t s) {
261 py::print(
"A new", s);
262 return ::operator
new(s);
264 static void *
operator new(
size_t s,
void *ptr) {
265 py::print(
"A placement-new", s);
268 static void operator delete(
void *p) {
269 py::print(
"A delete");
270 return ::operator
delete(p);
273 struct HasOpNewDelSize {
275 static void *
operator new(
size_t s) {
276 py::print(
"B new", s);
277 return ::operator
new(s);
279 static void *
operator new(
size_t s,
void *ptr) {
280 py::print(
"B placement-new", s);
283 static void operator delete(
void *p,
size_t s) {
284 py::print(
"B delete", s);
285 return ::operator
delete(p);
288 struct AliasedHasOpNewDelSize {
290 static void *
operator new(
size_t s) {
291 py::print(
"C new", s);
292 return ::operator
new(s);
294 static void *
operator new(
size_t s,
void *ptr) {
295 py::print(
"C placement-new", s);
298 static void operator delete(
void *p,
size_t s) {
299 py::print(
"C delete", s);
300 return ::operator
delete(p);
302 virtual ~AliasedHasOpNewDelSize() =
default;
303 AliasedHasOpNewDelSize() =
default;
304 AliasedHasOpNewDelSize(
const AliasedHasOpNewDelSize &) =
delete;
306 struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize {
307 PyAliasedHasOpNewDelSize() =
default;
308 explicit PyAliasedHasOpNewDelSize(
int) {}
311 struct HasOpNewDelBoth {
313 static void *
operator new(
size_t s) {
314 py::print(
"D new", s);
315 return ::operator
new(s);
317 static void *
operator new(
size_t s,
void *ptr) {
318 py::print(
"D placement-new", s);
321 static void operator delete(
void *p) {
322 py::print(
"D delete");
323 return ::operator
delete(p);
325 static void operator delete(
void *p,
size_t s) {
326 py::print(
"D wrong delete", s);
327 return ::operator
delete(p);
330 py::class_<HasOpNewDel>(m,
"HasOpNewDel").def(py::init<>());
331 py::class_<HasOpNewDelSize>(m,
"HasOpNewDelSize").def(py::init<>());
332 py::class_<HasOpNewDelBoth>(m,
"HasOpNewDelBoth").def(py::init<>());
333 py::class_<AliasedHasOpNewDelSize, PyAliasedHasOpNewDelSize> aliased(m,
334 "AliasedHasOpNewDelSize");
335 aliased.def(py::init<>());
336 aliased.attr(
"size_noalias") = py::int_(
sizeof(AliasedHasOpNewDelSize));
337 aliased.attr(
"size_alias") = py::int_(
sizeof(PyAliasedHasOpNewDelSize));
341 bind_local<LocalExternal, 17>(m,
"LocalExternal", py::module_local());
346 int foo()
const {
return value; }
352 class PublicistA :
public ProtectedA {
354 using ProtectedA::foo;
357 py::class_<ProtectedA>(m,
"ProtectedA")
359#if !defined(_MSC_VER) || _MSC_VER >= 1910
360 .def(
"foo", &PublicistA::foo);
362 .def(
"foo",
static_cast<int (ProtectedA::*)() const
>(&PublicistA::foo));
367 virtual ~ProtectedB() =
default;
368 ProtectedB() =
default;
369 ProtectedB(
const ProtectedB &) =
delete;
372 virtual int foo()
const {
return value; }
378 class TrampolineB :
public ProtectedB {
383 class PublicistB :
public ProtectedB {
388 ~PublicistB()
override{};
389 using ProtectedB::foo;
392 py::class_<ProtectedB, TrampolineB>(m,
"ProtectedB")
394#if !defined(_MSC_VER) || _MSC_VER >= 1910
395 .def(
"foo", &PublicistB::foo);
397 .def(
"foo",
static_cast<int (ProtectedB::*)() const
>(&PublicistB::foo));
401 struct BraceInitialization {
406 py::class_<BraceInitialization>(m,
"BraceInitialization")
407 .def(py::init<int, const std::string &>())
408 .def_readwrite(
"field1", &BraceInitialization::field1)
409 .def_readwrite(
"field2", &BraceInitialization::field2);
413 py::class_<NoBraceInitialization>(m,
"NoBraceInitialization")
414 .def(py::init<std::vector<int>>())
419 struct BogusImplicitConversion {
420 BogusImplicitConversion(
const BogusImplicitConversion &) =
default;
423 py::class_<BogusImplicitConversion>(m,
"BogusImplicitConversion")
424 .def(py::init<const BogusImplicitConversion &>());
426 py::implicitly_convertible<int, BogusImplicitConversion>();
433 py::class_<NestBase>
base(m,
"NestBase");
434 base.def(py::init<>());
435 py::class_<Nested>(
base,
"Nested")
437 .def(
"fn", [](Nested &,
int, NestBase &, Nested &) {})
439 "fa", [](Nested &,
int, NestBase &, Nested &) {},
"a"_a,
"b"_a,
"c"_a);
440 base.def(
"g", [](NestBase &, Nested &) {});
441 base.def(
"h", []() {
return NestBase(); });
448 struct NotRegistered {};
449 struct StringWrapper {
452 m.def(
"test_error_after_conversions", [](
int) {});
453 m.def(
"test_error_after_conversions",
454 [](
const StringWrapper &) -> NotRegistered {
return {}; });
455 py::class_<StringWrapper>(m,
"StringWrapper").def(py::init<std::string>());
456 py::implicitly_convertible<std::string, StringWrapper>();
458#if defined(PYBIND11_CPP17)
459 struct alignas(1024) Aligned {
460 std::uintptr_t ptr()
const {
return (uintptr_t)
this; }
462 py::class_<Aligned>(m,
"Aligned").def(py::init<>()).def(
"ptr", &Aligned::ptr);
466 struct IsFinal final {};
467 py::class_<IsFinal>(m,
"IsFinal", py::is_final());
470 struct IsNonFinalFinal {};
471 py::class_<IsNonFinalFinal>(m,
"IsNonFinalFinal", py::is_final());
474 struct PyPrintDestructor {
475 PyPrintDestructor() =
default;
476 ~PyPrintDestructor() { py::print(
"Print from destructor"); }
477 void throw_something() {
throw std::runtime_error(
"error"); }
479 py::class_<PyPrintDestructor>(m,
"PyPrintDestructor")
481 .def(
"throw_something", &PyPrintDestructor::throw_something);
484 struct SamePointer {};
485 static SamePointer samePointer;
486 py::class_<SamePointer, std::unique_ptr<SamePointer, py::nodelete>>(m,
"SamePointer")
487 .def(py::init([]() {
return &samePointer; }));
490 py::class_<Empty>(m,
"Empty").def(py::init<>());
493 struct BaseWithNested {
497 struct DerivedWithNested : BaseWithNested {
501 py::class_<BaseWithNested> baseWithNested_class(m,
"BaseWithNested");
502 py::class_<DerivedWithNested, BaseWithNested> derivedWithNested_class(m,
"DerivedWithNested");
503 py::class_<BaseWithNested::Nested>(baseWithNested_class,
"Nested")
504 .def_static(
"get_name", []() {
return "BaseWithNested::Nested"; });
505 py::class_<DerivedWithNested::Nested>(derivedWithNested_class,
"Nested")
506 .def_static(
"get_name", []() {
return "DerivedWithNested::Nested"; });
510 struct OtherDuplicate {};
511 struct DuplicateNested {};
512 struct OtherDuplicateNested {};
514 m.def(
"register_duplicate_class_name", [](
const py::module_ &m) {
515 py::class_<Duplicate>(m,
"Duplicate");
516 py::class_<OtherDuplicate>(m,
"Duplicate");
518 m.def(
"register_duplicate_class_type", [](
const py::module_ &m) {
519 py::class_<OtherDuplicate>(m,
"OtherDuplicate");
520 py::class_<OtherDuplicate>(m,
"YetAnotherDuplicate");
522 m.def(
"register_duplicate_nested_class_name", [](
const py::object >) {
523 py::class_<DuplicateNested>(gt,
"DuplicateNested");
524 py::class_<OtherDuplicateNested>(gt,
"DuplicateNested");
526 m.def(
"register_duplicate_nested_class_type", [](
const py::object >) {
527 py::class_<OtherDuplicateNested>(gt,
"OtherDuplicateNested");
528 py::class_<OtherDuplicateNested>(gt,
"YetAnotherDuplicateNested");
544using DoesntBreak3 = py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>>;
549using DoesntBreak8 = py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>>;
550#define CHECK_BASE(N) \
551 static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<(N)>>::value, \
552 "DoesntBreak" #N " has wrong type!")
561#define CHECK_ALIAS(N) \
563 DoesntBreak##N::has_alias \
564 && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<(N)>>::value, \
565 "DoesntBreak" #N " has wrong type_alias!")
566#define CHECK_NOALIAS(N) \
567 static_assert(!DoesntBreak##N::has_alias \
568 && std::is_void<typename DoesntBreak##N::type_alias>::value, \
569 "DoesntBreak" #N " has type alias, but shouldn't!")
578#define CHECK_HOLDER(N, TYPE) \
579 static_assert(std::is_same<typename DoesntBreak##N::holder_type, \
580 std::TYPE##_ptr<BreaksBase<(N)>>>::value, \
581 "DoesntBreak" #N " has wrong holder_type!")
596#define CHECK_BROKEN(N) \
597 static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-(N)>>::value, \
598 "Breaks1 has wrong type!");
600#ifdef PYBIND11_NEVER_DEFINED_EVER
BreaksBase(const BreaksBase &)=delete
virtual ~BreaksBase()=default
A user-defined type which is exported and can be used by any test.
PUGI__FN I unique(I begin, I end)
@ move
Use std::move to move the return value contents into a new instance that will be owned by Python.
#define PYBIND11_OVERRIDE(ret_type, cname, fn,...)
\rst Macro to populate the virtual method in the trampoline class.
void print_created(T *inst, Values &&...values)
void print_destroyed(T *inst, Values &&...values)
#define TEST_SUBMODULE(name, variable)
py::class_< BreaksBase< 8 >, std::shared_ptr< BreaksBase< 8 > > > DoesntBreak8
py::class_< BreaksBase< 1 >, std::unique_ptr< BreaksBase< 1 > >, BreaksTramp< 1 > > DoesntBreak1
py::class_< BreaksBase< 5 > > DoesntBreak5
py::class_< BreaksBase< 4 >, BreaksTramp< 4 > > DoesntBreak4
py::class_< BreaksBase< 7 >, BreaksTramp< 7 >, std::shared_ptr< BreaksBase< 7 > > > DoesntBreak7
#define CHECK_HOLDER(N, TYPE)
py::class_< BreaksBase< 2 >, BreaksTramp< 2 >, std::unique_ptr< BreaksBase< 2 > > > DoesntBreak2
py::class_< BreaksBase< 3 >, std::unique_ptr< BreaksBase< 3 > > > DoesntBreak3
py::class_< BreaksBase< 6 >, std::shared_ptr< BreaksBase< 6 > >, BreaksTramp< 6 > > DoesntBreak6
NoBraceInitialization(std::vector< int > v)
NoBraceInitialization(std::initializer_list< T > l)
Annotation indicating that a class derives from another given type.
Annotation for function names.