replace redundant function_record->class_ field with 1 bit

This commit is contained in:
Wenzel Jakob 2016-11-20 05:27:05 +01:00
parent 7c2461eefd
commit 31fbf18ac7
2 changed files with 14 additions and 14 deletions

View File

@ -115,15 +115,15 @@ struct function_record {
/// True if the function has a '**kwargs' argument /// True if the function has a '**kwargs' argument
bool has_kwargs : 1; bool has_kwargs : 1;
/// True if this is a method
bool is_method : 1;
/// Number of arguments /// Number of arguments
uint16_t nargs; uint16_t nargs;
/// Python method object /// Python method object
PyMethodDef *def = nullptr; 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) /// Python handle to the parent scope (a class or a module)
handle scope; 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 /// Process an attribute which indicates that this function is a method
template <> struct process_attribute<is_method> : process_attribute_default<is_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 /// 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) /// Process a keyword argument attribute (*without* a default value)
template <> struct process_attribute<arg> : process_attribute_default<arg> { template <> struct process_attribute<arg> : process_attribute_default<arg> {
static void init(const arg &a, function_record *r) { 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("self", nullptr, handle());
r->args.emplace_back(a.name, 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) /// Process a keyword argument attribute (*with* a default value)
template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> { template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> {
static void init(const arg_v &a, function_record *r) { 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()); r->args.emplace_back("self", nullptr, handle());
if (!a.value) { if (!a.value) {
#if !defined(NDEBUG) #if !defined(NDEBUG)
auto descr = "'" + std::string(a.name) + ": " + a.type + "'"; auto descr = "'" + std::string(a.name) + ": " + a.type + "'";
if (r->class_) { if (r->is_method) {
if (r->name) 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 else
descr += " in method of '" + (std::string) str(r->class_) + "'"; descr += " in method of '" + (std::string) str(r->scope) + "'";
} else if (r->name) { } else if (r->name) {
descr += " in function named '" + (std::string) r->name + "'"; descr += " in function named '" + (std::string) r->name + "'";
} }

View File

@ -194,10 +194,10 @@ protected:
if (type_depth == 0 && text[char_index] != '*' && arg_index < args) { if (type_depth == 0 && text[char_index] != '*' && arg_index < args) {
if (!rec->args.empty()) { if (!rec->args.empty()) {
signature += rec->args[arg_index].name; signature += rec->args[arg_index].name;
} else if (arg_index == 0 && rec->class_) { } else if (arg_index == 0 && rec->is_method) {
signature += "self"; signature += "self";
} else { } 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 += ": "; signature += ": ";
} }
@ -337,8 +337,8 @@ protected:
std::free((char *) func->m_ml->ml_doc); std::free((char *) func->m_ml->ml_doc);
func->m_ml->ml_doc = strdup(signatures.c_str()); func->m_ml->ml_doc = strdup(signatures.c_str());
if (rec->class_) { if (rec->is_method) {
m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->class_.ptr()); m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
if (!m_ptr) if (!m_ptr)
pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object"); pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
Py_DECREF(func); Py_DECREF(func);
@ -1120,7 +1120,7 @@ public:
const auto property = reinterpret_steal<object>( const auto property = reinterpret_steal<object>(
PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None, PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None,
fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr)); 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; attr(name) = property;
else else
metaclass().attr(name) = property; metaclass().attr(name) = property;