1414#include " pybind11.h"
1515
1616#include < functional>
17+ #include < iostream>
1718
1819PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE )
1920PYBIND11_NAMESPACE_BEGIN (detail)
@@ -129,7 +130,7 @@ struct type_caster<std::function<Return(Args...)>> {
129130 // See PR #1413 for full details
130131 } else {
131132 // Check number of arguments of Python function
132- auto argCountFromFuncCode = [& ](handle &obj) {
133+ auto argCountFromFuncCode = [](handle &obj) {
133134 // This is faster then doing import inspect and
134135 // inspect.signature(obj).parameters
135136
@@ -138,20 +139,41 @@ struct type_caster<std::function<Return(Args...)>> {
138139 };
139140 size_t argCount = 0 ;
140141
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;
143152 argCount = argCountFromFuncCode (codeAttr);
144153 } 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__"));
148166 argCount = argCountFromFuncCode (codeAttr2)
149167 - 1 ; // we have to remove the self argument
150168 } else {
151169 // 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;
152173 return false ;
153174 }
154175 }
176+ std::cout << " AFTER " << std::endl;
155177 // if we are a method, we have to correct the argument count since we are not counting
156178 // the self argument
157179 const size_t self_offset = static_cast <bool >(PyMethod_Check (src.ptr ())) ? 1 : 0 ;
0 commit comments