diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 2e6dec104..c00099a7f 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -115,15 +115,15 @@ struct function_record { /// True if the function has a '**kwargs' argument bool has_kwargs : 1; + /// True if this is a method + bool is_method : 1; + /// Number of arguments uint16_t nargs; /// Python method object PyMethodDef *def = nullptr; - /// Python handle to the associated class (if this is method) - handle class_; - /// Python handle to the parent scope (a class or a module) handle scope; @@ -235,7 +235,7 @@ template <> struct process_attribute : process_attribute_default struct process_attribute : process_attribute_default { - static void init(const is_method &s, function_record *r) { r->class_ = s.class_; r->scope = s.class_; } + static void init(const is_method &s, function_record *r) { r->is_method = true; r->scope = s.class_; } }; /// Process an attribute which indicates the parent scope of a method @@ -251,7 +251,7 @@ template <> struct process_attribute : process_attribute_default struct process_attribute : process_attribute_default { static void init(const arg &a, function_record *r) { - if (r->class_ && r->args.empty()) + if (r->is_method && r->args.empty()) r->args.emplace_back("self", nullptr, handle()); r->args.emplace_back(a.name, nullptr, handle()); } @@ -260,17 +260,17 @@ template <> struct process_attribute : process_attribute_default { /// Process a keyword argument attribute (*with* a default value) template <> struct process_attribute : process_attribute_default { static void init(const arg_v &a, function_record *r) { - if (r->class_ && r->args.empty()) + if (r->is_method && r->args.empty()) r->args.emplace_back("self", nullptr, handle()); if (!a.value) { #if !defined(NDEBUG) auto descr = "'" + std::string(a.name) + ": " + a.type + "'"; - if (r->class_) { + if (r->is_method) { if (r->name) - descr += " in method '" + (std::string) str(r->class_) + "." + (std::string) r->name + "'"; + descr += " in method '" + (std::string) str(r->scope) + "." + (std::string) r->name + "'"; else - descr += " in method of '" + (std::string) str(r->class_) + "'"; + descr += " in method of '" + (std::string) str(r->scope) + "'"; } else if (r->name) { descr += " in function named '" + (std::string) r->name + "'"; } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 1221ba6dd..b3b97cd80 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -194,10 +194,10 @@ protected: if (type_depth == 0 && text[char_index] != '*' && arg_index < args) { if (!rec->args.empty()) { signature += rec->args[arg_index].name; - } else if (arg_index == 0 && rec->class_) { + } else if (arg_index == 0 && rec->is_method) { signature += "self"; } else { - signature += "arg" + std::to_string(arg_index - (rec->class_ ? 1 : 0)); + signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0)); } signature += ": "; } @@ -337,8 +337,8 @@ protected: std::free((char *) func->m_ml->ml_doc); func->m_ml->ml_doc = strdup(signatures.c_str()); - if (rec->class_) { - m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->class_.ptr()); + if (rec->is_method) { + m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr()); if (!m_ptr) pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object"); Py_DECREF(func); @@ -1120,7 +1120,7 @@ public: const auto property = reinterpret_steal( PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None, fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr)); - if (rec_fget->class_) + if (rec_fget->is_method && rec_fget->scope) attr(name) = property; else metaclass().attr(name) = property;