mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
replace redundant function_record->class_ field with 1 bit
This commit is contained in:
parent
7c2461eefd
commit
31fbf18ac7
@ -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<sibling> : process_attribute_default<siblin
|
||||
|
||||
/// Process an attribute which indicates that this function is a method
|
||||
template <> struct process_attribute<is_method> : process_attribute_default<is_method> {
|
||||
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<is_operator> : process_attribute_default<is
|
||||
/// Process a keyword argument attribute (*without* a default value)
|
||||
template <> struct process_attribute<arg> : process_attribute_default<arg> {
|
||||
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<arg> : process_attribute_default<arg> {
|
||||
/// Process a keyword argument attribute (*with* a default value)
|
||||
template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> {
|
||||
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 + "'";
|
||||
}
|
||||
|
@ -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<object>(
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user