Skip to content

Commit

Permalink
moved heavy templated code into non-templated base class (-1% object …
Browse files Browse the repository at this point in the history
…code reduction)
  • Loading branch information
wjakob committed Dec 3, 2016
1 parent bcfc786 commit 1e884c4
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,31 @@ class generic_type : public object {
}

static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; }

void def_property_static_impl(const char *name,
handle fget, handle fset,
detail::function_record *rec_fget) {
pybind11::str doc_obj = pybind11::str(
(rec_fget->doc && pybind11::options::show_user_defined_docstrings())
? rec_fget->doc : "");
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->is_method && rec_fget->scope) {
attr(name) = property;
} else {
auto mclass = handle((PyObject *) PYBIND11_OB_TYPE(*((PyTypeObject *) m_ptr)));

if ((PyTypeObject *) mclass.ptr() == &PyType_Type)
pybind11_fail(
"Adding static properties to the type '" +
std::string(((PyTypeObject *) m_ptr)->tp_name) +
"' requires the type to have a custom metaclass. Please "
"ensure that one is created by supplying the pybind11::metaclass() "
"annotation to the associated class_<>(..) invocation.");
mclass.attr(name) = property;
}
}
};

NAMESPACE_END(detail)
Expand Down Expand Up @@ -1148,26 +1173,7 @@ class class_ : public detail::generic_type {
rec_fset->doc = strdup(rec_fset->doc);
}
}
pybind11::str doc_obj = pybind11::str(
(rec_fget->doc && pybind11::options::show_user_defined_docstrings())
? rec_fget->doc : "");
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->is_method && rec_fget->scope) {
attr(name) = property;
} else {
auto mclass = handle((PyObject *) PYBIND11_OB_TYPE(*((PyTypeObject *) m_ptr)));

if ((PyTypeObject *) mclass.ptr() == &PyType_Type)
pybind11_fail(
"Adding static properties to the type '" +
std::string(((PyTypeObject *) m_ptr)->tp_name) +
"' requires the type to have a custom metaclass. Please "
"ensure that one is created by supplying the pybind11::metaclass() "
"annotation to the associated class_<>(..) invocation.");
mclass.attr(name) = property;
}
def_property_static_impl(name, fget, fset, rec_fget);
return *this;
}

Expand Down

0 comments on commit 1e884c4

Please sign in to comment.