14
14
#include " pybind11.h"
15
15
16
16
#include < functional>
17
+ #include < iostream>
17
18
18
19
PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE)
19
20
PYBIND11_NAMESPACE_BEGIN(detail)
@@ -129,7 +130,7 @@ struct type_caster<std::function<Return(Args...)>> {
129
130
// See PR #1413 for full details
130
131
} else {
131
132
// Check number of arguments of Python function
132
- auto argCountFromFuncCode = [& ](handle &obj) {
133
+ auto argCountFromFuncCode = [](handle &obj) {
133
134
// This is faster then doing import inspect and
134
135
// inspect.signature(obj).parameters
135
136
@@ -138,20 +139,41 @@ struct type_caster<std::function<Return(Args...)>> {
138
139
};
139
140
size_t argCount = 0 ;
140
141
141
- handle codeAttr = PyObject_GetAttrString (src.ptr (), " __code__" );
142
- if (codeAttr) {
142
+ std::cout << " BEFORE " << std::endl;
143
+ object codeAttr = getattr (src, " __code__" );
144
+ // = reinterpret_borrow<object>(PyObject_GetAttrString(src.ptr(), "__code__"));
145
+ assert ((static_cast <bool >(codeAttr)
146
+ == static_cast <bool >(PyObject_HasAttrString (src.ptr (), " __code__" )))
147
+ && " ptr and "
148
+ " HasAttrString "
149
+ " inconsistent for __code__" );
150
+ if (static_cast <bool >(PyObject_HasAttrString (src.ptr (), " __code__" ))) {
151
+ std::cout << " __code__ exists" << std::endl;
143
152
argCount = argCountFromFuncCode (codeAttr);
144
153
} else {
145
- handle callAttr = PyObject_GetAttrString (src.ptr (), " __call__" );
146
- if (callAttr) {
147
- handle codeAttr2 = PyObject_GetAttrString (callAttr.ptr (), " __code__" );
154
+ object callAttr = getattr (src ," __call__" );
155
+ // = reinterpret_borrow<object>(PyObject_GetAttrString(src.ptr(), "__call__"));
156
+ assert ((static_cast <bool >(callAttr)
157
+ == static_cast <bool >(PyObject_HasAttrString (src.ptr (), " __call__" )))
158
+ && " ptr and "
159
+ " HasAttrString "
160
+ " inconsistent for __call__" );
161
+ if (static_cast <bool >(PyObject_HasAttrString (src.ptr (), " __call__" ))) {
162
+ std::cout << " __call__ exists" << std::endl;
163
+ object codeAttr2 = getattr (callAttr ," __code__" );
164
+ // reinterpret_borrow<object>(
165
+ // PyObject_GetAttrString(callAttr.ptr(), "__code__"));
148
166
argCount = argCountFromFuncCode (codeAttr2)
149
167
- 1 ; // we have to remove the self argument
150
168
} else {
151
169
// No __code__ or __call__ attribute, this is not a proper Python function
170
+ std::cout << " No __code__ or __call__ attribute, this is not a proper Python "
171
+ " function"
172
+ << std::endl;
152
173
return false ;
153
174
}
154
175
}
176
+ std::cout << " AFTER " << std::endl;
155
177
// if we are a method, we have to correct the argument count since we are not counting
156
178
// the self argument
157
179
const size_t self_offset = static_cast <bool >(PyMethod_Check (src.ptr ())) ? 1 : 0 ;
0 commit comments