|
14 | 14 | #include "pybind11.h"
|
15 | 15 |
|
16 | 16 | #include <functional>
|
| 17 | +#include <iostream> |
17 | 18 |
|
18 | 19 | PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
19 | 20 | PYBIND11_NAMESPACE_BEGIN(detail)
|
@@ -129,24 +130,23 @@ struct type_caster<std::function<Return(Args...)>> {
|
129 | 130 | // See PR #1413 for full details
|
130 | 131 | } else {
|
131 | 132 | // Check number of arguments of Python function
|
132 |
| - auto argCountFromFuncCode = [&](handle &obj) { |
133 |
| - // This is faster then doing import inspect and |
134 |
| - // inspect.signature(obj).parameters |
135 |
| - |
136 |
| - object argCount = obj.attr("co_argcount"); |
137 |
| - return argCount.template cast<size_t>(); |
| 133 | + auto get_argument_count = [](const handle &obj) -> size_t { |
| 134 | + // Faster then `import inspect` and `inspect.signature(obj).parameters` |
| 135 | + return obj.attr("co_argcount").cast<size_t>(); |
138 | 136 | };
|
139 | 137 | size_t argCount = 0;
|
140 | 138 |
|
141 |
| - handle codeAttr = PyObject_GetAttrString(src.ptr(), "__code__"); |
| 139 | + handle empty; |
| 140 | + object codeAttr = getattr(src, "__code__", empty); |
| 141 | + |
142 | 142 | if (codeAttr) {
|
143 |
| - argCount = argCountFromFuncCode(codeAttr); |
| 143 | + argCount = get_argument_count(codeAttr); |
144 | 144 | } else {
|
145 |
| - handle callAttr = PyObject_GetAttrString(src.ptr(), "__call__"); |
| 145 | + object callAttr = getattr(src, "__call__", empty); |
| 146 | + |
146 | 147 | if (callAttr) {
|
147 |
| - handle codeAttr2 = PyObject_GetAttrString(callAttr.ptr(), "__code__"); |
148 |
| - argCount = argCountFromFuncCode(codeAttr2) |
149 |
| - - 1; // we have to remove the self argument |
| 148 | + object codeAttr2 = getattr(callAttr, "__code__"); |
| 149 | + argCount = get_argument_count(codeAttr2) - 1; // removing the self argument |
150 | 150 | } else {
|
151 | 151 | // No __code__ or __call__ attribute, this is not a proper Python function
|
152 | 152 | return false;
|
|
0 commit comments