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 9530a17
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 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 @@ -138,20 +139,30 @@ 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;
auto codeAttr
= reinterpret_borrow<object> (PyObject_GetAttrString(src.ptr(), "__code__"));
if (codeAttr.ptr() != nullptr) {
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__");
auto callAttr
= reinterpret_borrow<object> (PyObject_GetAttrString(src.ptr(), "__call__"));
if (callAttr.ptr()!=nullptr) {
std::cout << "__call__ exists" << std::endl;
auto codeAttr2 = 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

0 comments on commit 9530a17

Please sign in to comment.