14 #if !defined(PYBIND11_OVERLOAD_CAST)
15 template <
typename... Args>
28 std::string
toString()
const {
return "ExampleMandA[value=" + std::to_string(
value) +
"]"; }
108 const UserType &
get1()
const {
return v1; }
109 const UserType &
get2()
const {
return v2; }
124 int none3(std::shared_ptr<NoneTester> &obj) {
return obj ? obj->answer : -1; }
125 int none4(std::shared_ptr<NoneTester> *obj) {
return obj && *obj ? (*obj)->answer : -1; }
126 int none5(
const std::shared_ptr<NoneTester> &obj) {
return obj ? obj->answer : -1; }
160 using UnregisteredBase::UnregisteredBase;
181 namespace exercise_is_setter {
198 py::module sm =
m.def_submodule(
"exercise_is_setter");
200 py::class_<Field>(sm,
"Field")
214 py::class_<ExampleMandA> emna(
m,
"ExampleMandA");
215 emna.def(py::init<>())
216 .def(py::init<int>())
217 .def(py::init<std::string &&>())
218 .def(py::init<const ExampleMandA &>())
240 #if defined(PYBIND11_OVERLOAD_CAST)
249 .def(
"overloaded_const",
251 .def(
"overloaded_const",
253 .def(
"overloaded_const",
255 .def(
"overloaded_const",
274 .def_static(
"add_mixed_overloads1",
276 auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(
277 py::module_::import(
"pybind11_tests.methods_and_attributes")
278 .attr(
"ExampleMandA"));
279 emna.def(
"overload_mixed1",
286 .def_static(
"add_mixed_overloads2",
288 auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(
289 py::module_::import(
"pybind11_tests.methods_and_attributes")
290 .attr(
"ExampleMandA"));
291 emna.def_static(
"overload_mixed2",
293 .def(
"overload_mixed2",
302 emna.attr(
"add2b") = emna.attr(
"add2");
305 py::class_<TestProperties>(
m,
"TestProperties")
309 .def_property(
"def_writeonly",
nullptr, [](
TestProperties &
s,
int v) {
s.value =
v; })
313 .def_property(
"def_property_impossible",
nullptr,
nullptr)
316 .def_property_static(
"def_writeonly_static",
319 .def_property_readonly_static(
320 "def_property_readonly_static",
322 .def_property_static(
323 "def_property_writeonly_static",
326 .def_property_static(
327 "def_property_static",
330 .def_property_static(
332 [](py::object cls) {
return cls; },
333 [](
const py::object &cls,
const py::function &
f) {
f(cls); });
335 py::class_<TestPropertiesOverride, TestProperties>(
m,
"TestPropertiesOverride")
340 auto static_get1 = [](
const py::object &) ->
const UserType & {
return TestPropRVP::sv1; };
341 auto static_get2 = [](
const py::object &) ->
const UserType & {
return TestPropRVP::sv2; };
347 py::class_<TestPropRVP>(
m,
"TestPropRVP")
351 .def_property_readonly(
"ro_func", py::cpp_function(&
TestPropRVP::get2, rvp_copy))
356 .def_property_readonly_static(
"static_ro_ref", static_get1)
357 .def_property_readonly_static(
"static_ro_copy", static_get2, rvp_copy)
358 .def_property_readonly_static(
"static_ro_func", py::cpp_function(static_get2, rvp_copy))
359 .def_property_static(
"static_rw_ref", static_get1, static_set1)
360 .def_property_static(
"static_rw_copy", static_get2, static_set2, rvp_copy)
361 .def_property_static(
362 "static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
365 .def_property_readonly_static(
"static_rvalue",
366 [](
const py::object &) {
return UserType(1); });
369 struct MetaclassOverride {};
370 py::class_<MetaclassOverride>(
m,
"MetaclassOverride", py::metaclass((PyObject *) &PyType_Type))
371 .def_property_readonly_static(
"readonly", [](
const py::object &) {
return 1; });
374 m.def(
"overload_order", [](
const std::string &) {
return 1; });
375 m.def(
"overload_order", [](
const std::string &) {
return 2; });
376 m.def(
"overload_order", [](
int) {
return 3; });
377 m.def(
"overload_order", [](
int) {
return 4; }, py::prepend{});
379 #if !defined(PYPY_VERSION)
384 DynamicClass(
const DynamicClass &) =
delete;
387 py::class_<DynamicClass>(
m,
"DynamicClass", py::dynamic_attr()).def(
py::init());
389 class CppDerivedDynamicClass :
public DynamicClass {};
390 py::class_<CppDerivedDynamicClass, DynamicClass>(
m,
"CppDerivedDynamicClass").def(
py::init());
395 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
396 m.attr(
"detailed_error_messages_enabled") =
true;
398 m.attr(
"detailed_error_messages_enabled") =
false;
400 m.def(
"bad_arg_def_named", [] {
401 auto m = py::module_::import(
"pybind11_tests");
408 m.def(
"bad_arg_def_unnamed", [] {
409 auto m = py::module_::import(
"pybind11_tests");
420 py::class_<NoneTester, std::shared_ptr<NoneTester>>(
m,
"NoneTester").def(py::init<>());
426 m.def(
"ok_none1", &
none1);
428 m.def(
"ok_none3", &
none3);
430 m.def(
"ok_none5", &
none5);
432 m.def(
"no_none_kwarg", &
none2,
"a"_a.none(
false));
433 m.def(
"no_none_kwarg_kw_only", &
none2, py::kw_only(),
"a"_a.none(
false));
437 py::class_<NoneCastTester>(
m,
"NoneCastTester")
439 .def(py::init<int>())
441 py::implicitly_convertible<py::none, NoneCastTester>();
446 py::class_<StrIssue>(
m,
"StrIssue")
447 .def(py::init<int>())
450 [](
const StrIssue &si) {
return "StrIssue[" + std::to_string(si.
val) +
"]"; });
458 py::class_<RegisteredDerived>(
m,
"RegisteredDerived")
478 py::class_<RefQualified>(
m,
"RefQualified")
484 py::class_<RValueRefParam>(
m,
"RValueRefParam")