Skip to content

Commit ec84c33

Browse files
author
AlexanderMueller
committed
test fix
1 parent d21cee3 commit ec84c33

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

include/pybind11/functional.h

+28-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "pybind11.h"
1515

1616
#include <functional>
17+
#include <iostream>
1718

1819
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1920
PYBIND11_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;

tests/test_callbacks.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ def __call__(self, a):
112112
return a
113113

114114
assert m.dummy_function_overloaded_std_func_arg(f) == 9
115-
assert m.dummy_function_overloaded_std_func_arg(A()) == 9
115+
a = A()
116+
assert m.dummy_function_overloaded_std_func_arg(a) == 9
116117
assert m.dummy_function_overloaded_std_func_arg(lambda i: i) == 9
117118

118119
def f2(a, b):

0 commit comments

Comments
 (0)