@@ -21,6 +21,33 @@ class ArgsSubclass : public py::args {
21
21
class KWArgsSubclass : public py ::kwargs {
22
22
using py::kwargs::kwargs;
23
23
};
24
+ class MoveOrCopyInt {
25
+ public:
26
+ MoveOrCopyInt () { print_default_created (this ); }
27
+ explicit MoveOrCopyInt (int v) : value{v} { print_created (this , value); }
28
+ MoveOrCopyInt (MoveOrCopyInt &&m) noexcept {
29
+ print_move_created (this , m.value );
30
+ std::swap (value, m.value );
31
+ }
32
+ MoveOrCopyInt &operator =(MoveOrCopyInt &&m) noexcept {
33
+ print_move_assigned (this , m.value );
34
+ std::swap (value, m.value );
35
+ return *this ;
36
+ }
37
+ MoveOrCopyInt (const MoveOrCopyInt &c) {
38
+ print_copy_created (this , c.value );
39
+ // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
40
+ value = c.value ;
41
+ }
42
+ MoveOrCopyInt &operator =(const MoveOrCopyInt &c) {
43
+ print_copy_assigned (this , c.value );
44
+ value = c.value ;
45
+ return *this ;
46
+ }
47
+ ~MoveOrCopyInt () { print_destroyed (this ); }
48
+
49
+ int value;
50
+ };
24
51
namespace pybind11 {
25
52
namespace detail {
26
53
template <>
@@ -31,6 +58,19 @@ template <>
31
58
struct handle_type_name <KWArgsSubclass> {
32
59
static constexpr auto name = const_name(" **KWArgs" );
33
60
};
61
+ template <>
62
+ struct type_caster <MoveOrCopyInt> {
63
+ PYBIND11_TYPE_CASTER (MoveOrCopyInt*, const_name(" MoveOrCopyInt" ));
64
+ bool load (handle src, bool ) {
65
+ auto as_class = MoveOrCopyInt (src.cast <int >());
66
+ value = &as_class;
67
+ return true ;
68
+ }
69
+ static handle cast (int v, return_value_policy r, handle p) {
70
+ auto as_class = MoveOrCopyInt (v);
71
+ return pybind11::handle (as_class, r, p);
72
+ }
73
+ };
34
74
} // namespace detail
35
75
} // namespace pybind11
36
76
@@ -348,4 +388,10 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
348
388
[](const ArgsSubclass &args, const KWArgsSubclass &kwargs) {
349
389
return py::make_tuple (args, kwargs);
350
390
});
391
+
392
+ // Test that support for args and kwargs subclasses skips checking arguments passed in as pointers
393
+ m.def (" args_kwargs_subclass_function_with_pointer_arg" ,
394
+ [](MoveOrCopyInt* pointer, const ArgsSubclass &args, const KWArgsSubclass &kwargs) {
395
+ return py::make_tuple (pointer->value , args, kwargs);
396
+ });
351
397
}
0 commit comments