10#if defined(__INTEL_COMPILER) && __cplusplus >= 201703L
14# include <aligned_new>
17#include <pybind11/stl.h>
38namespace pr4220_tripped_over_this {
45 return "This is really only meant to exercise successful compilation.";
51 py::class_<Empty0>(m,
"Empty0").def(py::init<>()).def(
"get_msg", get_msg<Empty0>);
58 m.def(
"obj_class_name", [](py::handle obj) {
return py::detail::obj_class_name(obj.ptr()); });
61 struct NoConstructor {
62 NoConstructor() =
default;
63 NoConstructor(
const NoConstructor &) =
default;
64 NoConstructor(NoConstructor &&) =
default;
65 static NoConstructor *new_instance() {
66 auto *ptr =
new NoConstructor();
72 struct NoConstructorNew {
73 NoConstructorNew() =
default;
74 NoConstructorNew(
const NoConstructorNew &) =
default;
75 NoConstructorNew(NoConstructorNew &&) =
default;
76 static NoConstructorNew *new_instance() {
77 auto *ptr =
new NoConstructorNew();
84 py::class_<NoConstructor>(m,
"NoConstructor")
85 .def_static(
"new_instance", &NoConstructor::new_instance,
"Return an instance");
87 py::class_<NoConstructorNew>(m,
"NoConstructorNew")
88 .def(py::init([](
const NoConstructorNew &
self) {
return self; }))
89 .def_static(
"__new__",
90 [](
const py::object &) {
return NoConstructorNew::new_instance(); });
95 Pet(
const std::string &
name,
const std::string &species)
96 : m_name(
name), m_species(species) {}
97 std::string
name()
const {
return m_name; }
98 std::string species()
const {
return m_species; }
102 std::string m_species;
105 class Dog :
public Pet {
107 explicit Dog(
const std::string &
name) : Pet(
name,
"dog") {}
108 std::string
bark()
const {
return "Woof!"; }
111 class Rabbit :
public Pet {
113 explicit Rabbit(
const std::string &
name) : Pet(
name,
"parrot") {}
116 class Hamster :
public Pet {
118 explicit Hamster(
const std::string &
name) : Pet(
name,
"rodent") {}
121 class Chimera :
public Pet {
122 Chimera() : Pet(
"Kimmy",
"chimera") {}
125 py::class_<Pet> pet_class(m,
"Pet");
126 pet_class.def(py::init<std::string, std::string>())
127 .def(
"name", &Pet::name)
128 .def(
"species", &Pet::species);
131 py::class_<Dog>(m,
"Dog", pet_class).def(py::init<std::string>());
134 py::class_<Rabbit, Pet>(m,
"Rabbit").def(py::init<std::string>());
137 py::class_<Hamster, Pet>(m,
"Hamster").def(py::init<std::string>());
140 py::class_<Chimera, Pet>(m,
"Chimera");
142 m.def(
"pet_name_species",
143 [](
const Pet &pet) {
return pet.name() +
" is a " + pet.species(); });
144 m.def(
"dog_bark", [](
const Dog &dog) {
return dog.
bark(); });
148 BaseClass() =
default;
149 BaseClass(
const BaseClass &) =
default;
150 BaseClass(BaseClass &&) =
default;
151 virtual ~BaseClass() =
default;
153 struct DerivedClass1 : BaseClass {};
154 struct DerivedClass2 : BaseClass {};
156 py::class_<BaseClass>(m,
"BaseClass").def(py::init<>());
157 py::class_<DerivedClass1>(m,
"DerivedClass1").def(py::init<>());
158 py::class_<DerivedClass2>(m,
"DerivedClass2").def(py::init<>());
160 m.def(
"return_class_1", []() -> BaseClass * {
return new DerivedClass1(); });
161 m.def(
"return_class_2", []() -> BaseClass * {
return new DerivedClass2(); });
162 m.def(
"return_class_n", [](
int n) -> BaseClass * {
164 return new DerivedClass1();
167 return new DerivedClass2();
169 return new BaseClass();
171 m.def(
"return_none", []() -> BaseClass * {
return nullptr; });
174 m.def(
"check_instances", [](
const py::list &l) {
175 return py::make_tuple(py::isinstance<py::tuple>(l[0]),
176 py::isinstance<py::dict>(l[1]),
177 py::isinstance<Pet>(l[2]),
178 py::isinstance<Pet>(l[3]),
179 py::isinstance<Dog>(l[4]),
180 py::isinstance<Rabbit>(l[5]),
181 py::isinstance<UnregisteredType>(l[6]));
187 m.def(
"check_type", [](
int category) {
193 return py::type::of<DerivedClass1>();
195 return py::type::of<Invalid>();
198 m.def(
"get_type_of", [](py::object ob) {
return py::type::of(std::move(ob)); });
200 m.def(
"get_type_classic", [](py::handle h) {
return h.get_type(); });
202 m.def(
"as_type", [](
const py::object &ob) {
return py::type(ob); });
205 struct MismatchBase1 {};
206 struct MismatchDerived1 : MismatchBase1 {};
208 struct MismatchBase2 {};
209 struct MismatchDerived2 : MismatchBase2 {};
211 m.def(
"mismatched_holder_1", []() {
212 auto mod = py::module_::import(
"__main__");
213 py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(mod,
"MismatchBase1");
214 py::class_<MismatchDerived1, MismatchBase1>(mod,
"MismatchDerived1");
216 m.def(
"mismatched_holder_2", []() {
217 auto mod = py::module_::import(
"__main__");
218 py::class_<MismatchBase2>(mod,
"MismatchBase2");
219 py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>, MismatchBase2>(
220 mod,
"MismatchDerived2");
226 static std::unique_ptr<MyBase> make() {
return std::unique_ptr<MyBase>(
new MyBase()); }
229 struct MyDerived : MyBase {
230 static std::unique_ptr<MyDerived> make() {
231 return std::unique_ptr<MyDerived>(
new MyDerived());
235 py::class_<MyBase>(m,
"MyBase").def_static(
"make", &MyBase::make);
237 py::class_<MyDerived, MyBase>(m,
"MyDerived")
238 .def_static(
"make", &MyDerived::make)
239 .def_static(
"make2", &MyDerived::make);
242 struct ConvertibleFromUserType {
245 explicit ConvertibleFromUserType(
UserType u) : i(u.
value()) {}
248 py::class_<ConvertibleFromUserType>(m,
"AcceptsUserType").def(py::init<UserType>());
249 py::implicitly_convertible<UserType, ConvertibleFromUserType>();
251 m.def(
"implicitly_convert_argument", [](
const ConvertibleFromUserType &r) {
return r.i; });
252 m.def(
"implicitly_convert_variable", [](
const py::object &o) {
256 const auto &r = o.cast<
const ConvertibleFromUserType &>();
259 m.add_object(
"implicitly_convert_variable_fail", [&] {
260 auto f = [](PyObject *, PyObject *
args) -> PyObject * {
261 auto o = py::reinterpret_borrow<py::tuple>(
args)[0];
263 o.cast<
const ConvertibleFromUserType &>();
264 }
catch (
const py::cast_error &e) {
265 return py::str(e.what()).release().ptr();
267 return py::str().release().ptr();
270 auto *def =
new PyMethodDef{
"f", f, METH_VARARGS,
nullptr};
271 py::capsule def_capsule(def,
272 [](
void *ptr) {
delete reinterpret_cast<PyMethodDef *
>(ptr); });
273 return py::reinterpret_steal<py::object>(
274 PyCFunction_NewEx(def, def_capsule.ptr(), m.ptr()));
280 static void *
operator new(
size_t s) {
281 py::print(
"A new", s);
282 return ::operator
new(s);
284 static void *
operator new(
size_t s,
void *ptr) {
285 py::print(
"A placement-new", s);
288 static void operator delete(
void *p) {
289 py::print(
"A delete");
290 return ::operator
delete(p);
293 struct HasOpNewDelSize {
295 static void *
operator new(
size_t s) {
296 py::print(
"B new", s);
297 return ::operator
new(s);
299 static void *
operator new(
size_t s,
void *ptr) {
300 py::print(
"B placement-new", s);
303 static void operator delete(
void *p,
size_t s) {
304 py::print(
"B delete", s);
305 return ::operator
delete(p);
308 struct AliasedHasOpNewDelSize {
310 static void *
operator new(
size_t s) {
311 py::print(
"C new", s);
312 return ::operator
new(s);
314 static void *
operator new(
size_t s,
void *ptr) {
315 py::print(
"C placement-new", s);
318 static void operator delete(
void *p,
size_t s) {
319 py::print(
"C delete", s);
320 return ::operator
delete(p);
322 virtual ~AliasedHasOpNewDelSize() =
default;
323 AliasedHasOpNewDelSize() =
default;
324 AliasedHasOpNewDelSize(
const AliasedHasOpNewDelSize &) =
delete;
326 struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize {
327 PyAliasedHasOpNewDelSize() =
default;
328 explicit PyAliasedHasOpNewDelSize(
int) {}
331 struct HasOpNewDelBoth {
333 static void *
operator new(
size_t s) {
334 py::print(
"D new", s);
335 return ::operator
new(s);
337 static void *
operator new(
size_t s,
void *ptr) {
338 py::print(
"D placement-new", s);
341 static void operator delete(
void *p) {
342 py::print(
"D delete");
343 return ::operator
delete(p);
345 static void operator delete(
void *p,
size_t s) {
346 py::print(
"D wrong delete", s);
347 return ::operator
delete(p);
350 py::class_<HasOpNewDel>(m,
"HasOpNewDel").def(py::init<>());
351 py::class_<HasOpNewDelSize>(m,
"HasOpNewDelSize").def(py::init<>());
352 py::class_<HasOpNewDelBoth>(m,
"HasOpNewDelBoth").def(py::init<>());
353 py::class_<AliasedHasOpNewDelSize, PyAliasedHasOpNewDelSize> aliased(m,
354 "AliasedHasOpNewDelSize");
355 aliased.def(py::init<>());
356 aliased.attr(
"size_noalias") = py::int_(
sizeof(AliasedHasOpNewDelSize));
357 aliased.attr(
"size_alias") = py::int_(
sizeof(PyAliasedHasOpNewDelSize));
361 bind_local<LocalExternal, 17>(m,
"LocalExternal", py::module_local());
366 int foo()
const {
return value; }
372 class PublicistA :
public ProtectedA {
374 using ProtectedA::foo;
377 py::class_<ProtectedA>(m,
"ProtectedA").def(py::init<>()).def(
"foo", &PublicistA::foo);
381 virtual ~ProtectedB() =
default;
382 ProtectedB() =
default;
383 ProtectedB(
const ProtectedB &) =
delete;
386 virtual int foo()
const {
return value; }
387 virtual void *void_foo() {
return static_cast<void *
>(&value); }
388 virtual void *get_self() {
return static_cast<void *
>(
this); }
394 class TrampolineB :
public ProtectedB {
401 class PublicistB :
public ProtectedB {
406 ~PublicistB()
override{};
407 using ProtectedB::foo;
408 using ProtectedB::get_self;
409 using ProtectedB::void_foo;
412 m.def(
"read_foo", [](
const void *original) {
413 const int *ptr =
reinterpret_cast<const int *
>(original);
417 m.def(
"pointers_equal",
418 [](
const void *original,
const void *comparison) {
return original == comparison; });
420 py::class_<ProtectedB, TrampolineB>(m,
"ProtectedB")
422 .def(
"foo", &PublicistB::foo)
423 .def(
"void_foo", &PublicistB::void_foo)
424 .def(
"get_self", &PublicistB::get_self);
427 struct BraceInitialization {
432 py::class_<BraceInitialization>(m,
"BraceInitialization")
433 .def(py::init<int, const std::string &>())
434 .def_readwrite(
"field1", &BraceInitialization::field1)
435 .def_readwrite(
"field2", &BraceInitialization::field2);
439 py::class_<NoBraceInitialization>(m,
"NoBraceInitialization")
440 .def(py::init<std::vector<int>>())
445 struct BogusImplicitConversion {
446 BogusImplicitConversion(
const BogusImplicitConversion &) =
default;
449 py::class_<BogusImplicitConversion>(m,
"BogusImplicitConversion")
450 .def(py::init<const BogusImplicitConversion &>());
452 py::implicitly_convertible<int, BogusImplicitConversion>();
459 py::class_<NestBase>
base(m,
"NestBase");
460 base.def(py::init<>());
461 py::class_<Nested>(
base,
"Nested")
463 .def(
"fn", [](Nested &,
int, NestBase &, Nested &) {})
465 "fa", [](Nested &,
int, NestBase &, Nested &) {},
"a"_a,
"b"_a,
"c"_a);
466 base.def(
"g", [](NestBase &, Nested &) {});
467 base.def(
"h", []() {
return NestBase(); });
474 struct NotRegistered {};
475 struct StringWrapper {
478 m.def(
"test_error_after_conversions", [](
int) {});
479 m.def(
"test_error_after_conversions",
480 [](
const StringWrapper &) -> NotRegistered {
return {}; });
481 py::class_<StringWrapper>(m,
"StringWrapper").def(py::init<std::string>());
482 py::implicitly_convertible<std::string, StringWrapper>();
484#if defined(PYBIND11_CPP17)
485 struct alignas(1024) Aligned {
486 std::uintptr_t ptr()
const {
return (uintptr_t)
this; }
488 py::class_<Aligned>(m,
"Aligned").def(py::init<>()).def(
"ptr", &Aligned::ptr);
492 struct IsFinal final {};
493 py::class_<IsFinal>(m,
"IsFinal", py::is_final());
496 struct IsNonFinalFinal {};
497 py::class_<IsNonFinalFinal>(m,
"IsNonFinalFinal", py::is_final());
500 struct PyPrintDestructor {
501 PyPrintDestructor() =
default;
502 ~PyPrintDestructor() { py::print(
"Print from destructor"); }
503 void throw_something() {
throw std::runtime_error(
"error"); }
505 py::class_<PyPrintDestructor>(m,
"PyPrintDestructor")
507 .def(
"throw_something", &PyPrintDestructor::throw_something);
510 struct SamePointer {};
511 static SamePointer samePointer;
512 py::class_<SamePointer, std::unique_ptr<SamePointer, py::nodelete>>(m,
"SamePointer")
513 .def(py::init([]() {
return &samePointer; }));
516 py::class_<Empty>(m,
"Empty").def(py::init<>());
519 struct BaseWithNested {
523 struct DerivedWithNested : BaseWithNested {
527 py::class_<BaseWithNested> baseWithNested_class(m,
"BaseWithNested");
528 py::class_<DerivedWithNested, BaseWithNested> derivedWithNested_class(m,
"DerivedWithNested");
529 py::class_<BaseWithNested::Nested>(baseWithNested_class,
"Nested")
530 .def_static(
"get_name", []() {
return "BaseWithNested::Nested"; });
531 py::class_<DerivedWithNested::Nested>(derivedWithNested_class,
"Nested")
532 .def_static(
"get_name", []() {
return "DerivedWithNested::Nested"; });
536 struct OtherDuplicate {};
537 struct DuplicateNested {};
538 struct OtherDuplicateNested {};
540 m.def(
"register_duplicate_class_name", [](
const py::module_ &m) {
541 py::class_<Duplicate>(m,
"Duplicate");
542 py::class_<OtherDuplicate>(m,
"Duplicate");
544 m.def(
"register_duplicate_class_type", [](
const py::module_ &m) {
545 py::class_<OtherDuplicate>(m,
"OtherDuplicate");
546 py::class_<OtherDuplicate>(m,
"YetAnotherDuplicate");
548 m.def(
"register_duplicate_nested_class_name", [](
const py::object >) {
549 py::class_<DuplicateNested>(gt,
"DuplicateNested");
550 py::class_<OtherDuplicateNested>(gt,
"DuplicateNested");
552 m.def(
"register_duplicate_nested_class_type", [](
const py::object >) {
553 py::class_<OtherDuplicateNested>(gt,
"OtherDuplicateNested");
554 py::class_<OtherDuplicateNested>(gt,
"YetAnotherDuplicateNested");
572using DoesntBreak3 = py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>>;
577using DoesntBreak8 = py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>>;
578#define CHECK_BASE(N) \
579 static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<(N)>>::value, \
580 "DoesntBreak" #N " has wrong type!")
589#define CHECK_ALIAS(N) \
591 DoesntBreak##N::has_alias \
592 && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<(N)>>::value, \
593 "DoesntBreak" #N " has wrong type_alias!")
594#define CHECK_NOALIAS(N) \
595 static_assert(!DoesntBreak##N::has_alias \
596 && std::is_void<typename DoesntBreak##N::type_alias>::value, \
597 "DoesntBreak" #N " has type alias, but shouldn't!")
606#define CHECK_HOLDER(N, TYPE) \
607 static_assert(std::is_same<typename DoesntBreak##N::holder_type, \
608 std::TYPE##_ptr<BreaksBase<(N)>>>::value, \
609 "DoesntBreak" #N " has wrong holder_type!")
624#define CHECK_BROKEN(N) \
625 static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-(N)>>::value, \
626 "Breaks1 has wrong type!");
628#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.
void bind_empty0(py::module_ &m)
std::string get_msg(const T &)
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
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
#define PYBIND11_WARNING_DISABLE_MSVC(name)
#define CHECK_HOLDER(N, TYPE)
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.