File tree Expand file tree Collapse file tree 3 files changed +17
-5
lines changed Expand file tree Collapse file tree 3 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -46,11 +46,17 @@ struct type_caster<std::function<Return(Args...)>> {
46
46
auto c = reinterpret_borrow<capsule>(PyCFunction_GET_SELF (cfunc.ptr ()));
47
47
auto rec = (function_record *) c;
48
48
49
- if (rec && rec->is_stateless &&
50
- same_type (typeid (function_type), *reinterpret_cast <const std::type_info *>(rec->data [1 ]))) {
51
- struct capture { function_type f; };
52
- value = ((capture *) &rec->data )->f ;
53
- return true ;
49
+ while (rec != nullptr ) {
50
+ if (rec->is_stateless
51
+ && same_type (typeid (function_type),
52
+ *reinterpret_cast <const std::type_info *>(rec->data [1 ]))) {
53
+ struct capture {
54
+ function_type f;
55
+ };
56
+ value = ((capture *) &rec->data )->f ;
57
+ return true ;
58
+ }
59
+ rec = rec->next ;
54
60
}
55
61
}
56
62
Original file line number Diff line number Diff line change @@ -97,6 +97,8 @@ TEST_SUBMODULE(callbacks, m) {
97
97
// test_cpp_function_roundtrip
98
98
/* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */
99
99
m.def (" dummy_function" , &dummy_function);
100
+ m.def (" dummy_function_overloaded" , [](int i, int j) { return i + j; });
101
+ m.def (" dummy_function_overloaded" , &dummy_function);
100
102
m.def (" dummy_function2" , [](int i, int j) { return i + j; });
101
103
m.def (" roundtrip" , [](std::function<int (int )> f, bool expect_none = false ) {
102
104
if (expect_none && f)
Original file line number Diff line number Diff line change @@ -93,6 +93,10 @@ def test_cpp_function_roundtrip():
93
93
m .test_dummy_function (m .roundtrip (m .dummy_function ))
94
94
== "matches dummy_function: eval(1) = 2"
95
95
)
96
+ assert (
97
+ m .test_dummy_function (m .dummy_function_overloaded )
98
+ == "matches dummy_function: eval(1) = 2"
99
+ )
96
100
assert m .roundtrip (None , expect_none = True ) is None
97
101
assert (
98
102
m .test_dummy_function (lambda x : x + 2 )
You can’t perform that action at this time.
0 commit comments