Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rath3t committed Aug 21, 2024
1 parent d21cee3 commit ec84c33
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
34 changes: 28 additions & 6 deletions include/pybind11/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "pybind11.h"

#include <functional>
#include <iostream>

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
Expand Down Expand Up @@ -129,7 +130,7 @@ struct type_caster<std::function<Return(Args...)>> {
// See PR #1413 for full details
} else {
// Check number of arguments of Python function
auto argCountFromFuncCode = [&](handle &obj) {
auto argCountFromFuncCode = [](handle &obj) {
// This is faster then doing import inspect and
// inspect.signature(obj).parameters

Expand All @@ -138,20 +139,41 @@ struct type_caster<std::function<Return(Args...)>> {
};
size_t argCount = 0;

handle codeAttr = PyObject_GetAttrString(src.ptr(), "__code__");
if (codeAttr) {
std::cout << "BEFORE " << std::endl;
object codeAttr = getattr(src, "__code__");
// = reinterpret_borrow<object>(PyObject_GetAttrString(src.ptr(), "__code__"));
assert((static_cast<bool>(codeAttr)
== static_cast<bool>(PyObject_HasAttrString(src.ptr(), "__code__")))
&& "ptr and "
"HasAttrString "
"inconsistent for __code__");
if (static_cast<bool>(PyObject_HasAttrString(src.ptr(), "__code__"))) {
std::cout << "__code__ exists" << std::endl;
argCount = argCountFromFuncCode(codeAttr);
} else {
handle callAttr = PyObject_GetAttrString(src.ptr(), "__call__");
if (callAttr) {
handle codeAttr2 = PyObject_GetAttrString(callAttr.ptr(), "__code__");
object callAttr = getattr(src ,"__call__");
// = reinterpret_borrow<object>(PyObject_GetAttrString(src.ptr(), "__call__"));
assert((static_cast<bool>(callAttr)
== static_cast<bool>(PyObject_HasAttrString(src.ptr(), "__call__")))
&& "ptr and "
"HasAttrString "
"inconsistent for __call__");
if (static_cast<bool>(PyObject_HasAttrString(src.ptr(), "__call__"))) {
std::cout << "__call__ exists" << std::endl;
object codeAttr2 = getattr(callAttr ,"__code__");
// reinterpret_borrow<object>(
// PyObject_GetAttrString(callAttr.ptr(), "__code__"));
argCount = argCountFromFuncCode(codeAttr2)
- 1; // we have to remove the self argument
} else {
// No __code__ or __call__ attribute, this is not a proper Python function
std::cout << "No __code__ or __call__ attribute, this is not a proper Python "
"function"
<< std::endl;
return false;
}
}
std::cout << "AFTER " << std::endl;
// if we are a method, we have to correct the argument count since we are not counting
// the self argument
const size_t self_offset = static_cast<bool>(PyMethod_Check(src.ptr())) ? 1 : 0;
Expand Down
3 changes: 2 additions & 1 deletion tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def __call__(self, a):
return a

assert m.dummy_function_overloaded_std_func_arg(f) == 9
assert m.dummy_function_overloaded_std_func_arg(A()) == 9
a = A()
assert m.dummy_function_overloaded_std_func_arg(a) == 9
assert m.dummy_function_overloaded_std_func_arg(lambda i: i) == 9

def f2(a, b):
Expand Down

0 comments on commit ec84c33

Please sign in to comment.