From 9530a176c5921d1e388bdb66b650b86feacc39c5 Mon Sep 17 00:00:00 2001 From: AlexanderMueller Date: Tue, 20 Aug 2024 17:09:24 +0200 Subject: [PATCH] test fix --- include/pybind11/functional.h | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 8a8c32c0ec..00ccff82f5 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -14,6 +14,7 @@ #include "pybind11.h" #include +#include PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) @@ -138,20 +139,30 @@ struct type_caster> { }; size_t argCount = 0; - handle codeAttr = PyObject_GetAttrString(src.ptr(), "__code__"); - if (codeAttr) { + std::cout << "BEFORE " << std::endl; + auto codeAttr + = reinterpret_borrow (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 (PyObject_GetAttrString(src.ptr(), "__call__")); + if (callAttr.ptr()!=nullptr) { + std::cout << "__call__ exists" << std::endl; + auto codeAttr2 = reinterpret_borrow (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(PyMethod_Check(src.ptr())) ? 1 : 0;