more flexible function creation syntax

This commit is contained in:
Wenzel Jakob 2015-07-11 17:41:48 +02:00
parent 38bd71134a
commit bd4a529319
8 changed files with 103 additions and 89 deletions

1
.gitignore vendored
View File

@ -18,3 +18,4 @@ Debug
.vs .vs
CTestTestfile.cmake CTestTestfile.cmake
Testing Testing
autogen

View File

@ -8,12 +8,11 @@
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
*/ */
#if !defined(__PYBIND_CAST) #pragma once
#define __PYBIND_CAST
#include "pytypes.h" #include <pybind/pytypes.h>
#include "mpl.h" #include <pybind/mpl.h>
#include "typeid.h" #include <pybind/typeid.h>
#include <map> #include <map>
#include <array> #include <array>
@ -474,10 +473,11 @@ TYPE_CASTER_PYTYPE(int_)
TYPE_CASTER_PYTYPE(list) TYPE_CASTER_PYTYPE(list)
TYPE_CASTER_PYTYPE(slice) TYPE_CASTER_PYTYPE(slice)
TYPE_CASTER_PYTYPE(tuple) TYPE_CASTER_PYTYPE(tuple)
TYPE_CASTER_PYTYPE(function)
#undef TYPE_CASTER #undef TYPE_CASTER
#undef TYPE_CASTER_NUMBER
#undef TYPE_CASTER_PYTYPE #undef TYPE_CASTER_PYTYPE
#undef TYPE_CASTER_NUMBER
NAMESPACE_END(detail) NAMESPACE_END(detail)
@ -522,5 +522,3 @@ template <typename ... Args> inline object handle::call(Args&&... args_) {
} }
NAMESPACE_END(pybind) NAMESPACE_END(pybind)
#endif /* __PYBIND_CAST */

View File

@ -7,8 +7,7 @@
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
*/ */
#if !defined(__PYBIND_COMMON_H) #pragma once
#define __PYBIND_COMMON_H
#if !defined(NAMESPACE_BEGIN) #if !defined(NAMESPACE_BEGIN)
#define NAMESPACE_BEGIN(name) namespace name { #define NAMESPACE_BEGIN(name) namespace name {
@ -145,5 +144,3 @@ inline internals &get_internals();
NAMESPACE_END(detail) NAMESPACE_END(detail)
NAMESPACE_END(pybind) NAMESPACE_END(pybind)
#endif /* __PYBIND_COMMON_H */

View File

@ -7,10 +7,9 @@
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
*/ */
#if !defined(__PYBIND_MPL_H) #pragma once
#define __PYBIND_MPL_H
#include "common.h" #include <pybind/common.h>
#include <tuple> #include <tuple>
NAMESPACE_BEGIN(pybind) NAMESPACE_BEGIN(pybind)
@ -80,7 +79,15 @@ template <> struct tuple_dispatch<void> {
NAMESPACE_END(detail) NAMESPACE_END(detail)
/// For lambda functions delegate to their 'operator()' /// For lambda functions delegate to their 'operator()'
template <typename T> struct function_traits : public function_traits<typename detail::make_function_type<T>> { }; template <typename T> struct function_traits : function_traits<typename detail::make_function_type<T>> { };
/* Deal with reference arguments */
template <typename ReturnType, typename... Args>
struct function_traits<ReturnType(*&)(Args...)> : function_traits<ReturnType(*)(Args...)> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*&)(Args...)> : function_traits<ReturnType(ClassType::*)(Args...)> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*&)(Args...) const> : function_traits<ReturnType(ClassType::*)(Args...) const> {};
/// Type traits for function pointers /// Type traits for function pointers
template <typename ReturnType, typename... Args> template <typename ReturnType, typename... Args>
@ -186,5 +193,3 @@ struct function_traits<std::function<ReturnType(Args...)>> {
NAMESPACE_END(mpl) NAMESPACE_END(mpl)
NAMESPACE_END(pybind) NAMESPACE_END(pybind)
#endif /* __PYBIND_MPL_H */

View File

@ -7,10 +7,9 @@
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
*/ */
#if !defined(__PYBIND_OPERATOR) #pragma once
#define __PYBIND_OPERATOR
#include "pybind.h" #include <pybind/pybind.h>
#include <type_traits> #include <type_traits>
NAMESPACE_BEGIN(pybind) NAMESPACE_BEGIN(pybind)
@ -147,5 +146,3 @@ NAMESPACE_END(detail)
using detail::self; using detail::self;
NAMESPACE_END(pybind) NAMESPACE_END(pybind)
#endif /* __PYBIND_OPERATOR */

View File

@ -7,8 +7,7 @@
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
*/ */
#if !defined(__PYBIND_H) #pragma once
#define __PYBIND_H
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma warning(push) #pragma warning(push)
@ -19,32 +18,31 @@
#pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted #pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted
#endif #endif
#include "cast.h" #include <pybind/cast.h>
NAMESPACE_BEGIN(pybind) NAMESPACE_BEGIN(pybind)
class function : public object { class cpp_function : public function {
private: public:
struct function_entry { struct function_entry {
std::function<PyObject* (PyObject *)> impl; std::function<PyObject* (PyObject *)> impl;
std::string signature, doc; std::string signature, doc;
bool is_constructor; bool is_constructor;
function_entry *next = nullptr; function_entry *next = nullptr;
}; };
public:
PYTHON_OBJECT_DEFAULT(function, object, PyFunction_Check)
template <typename Func> cpp_function() { }
function(const char *name, Func _func, bool is_method, template <typename Func> cpp_function(
function overload_sibling = function(), const char *doc = nullptr, Func &&_func, const char *name = nullptr, const char *doc = nullptr,
return_value_policy policy = return_value_policy::automatic) { return_value_policy policy = return_value_policy::automatic,
function sibling = function(), bool is_method = false) {
/* Function traits extracted from the template type 'Func' */ /* Function traits extracted from the template type 'Func' */
typedef mpl::function_traits<Func> f_traits; typedef mpl::function_traits<Func> f_traits;
/* Suitable input and output casters */ /* Suitable input and output casters */
typedef typename detail::type_caster<typename f_traits::args_type> cast_in; typedef typename detail::type_caster<typename f_traits::args_type> cast_in;
typedef typename detail::type_caster<typename mpl::normalize_type<typename f_traits::return_type>::type> cast_out; typedef typename detail::type_caster<typename mpl::normalize_type<typename f_traits::return_type>::type> cast_out;
typename f_traits::f_type func = f_traits::cast(_func); typename f_traits::f_type func = f_traits::cast(std::forward<Func>(_func));
auto impl = [func, policy](PyObject *pyArgs) -> PyObject *{ auto impl = [func, policy](PyObject *pyArgs) -> PyObject *{
cast_in args; cast_in args;
@ -53,20 +51,13 @@ public:
PyObject *parent = policy != return_value_policy::reference_internal PyObject *parent = policy != return_value_policy::reference_internal
? nullptr : PyTuple_GetItem(pyArgs, 0); ? nullptr : PyTuple_GetItem(pyArgs, 0);
return cast_out::cast( return cast_out::cast(
f_traits::dispatch(func, (typename f_traits::args_type) args), f_traits::dispatch(func, args.operator typename f_traits::args_type()),
policy, parent); policy, parent);
}; };
/* Linked list of function call handlers (for overloading) */ initialize(name, doc, cast_in::name() + std::string(" -> ") + cast_out::name(),
function_entry *entry = new function_entry(); sibling, is_method, std::move(impl));
entry->impl = impl;
entry->signature = std::string(name) + cast_in::name() + std::string(" -> ") + cast_out::name();
entry->is_constructor = !strcmp(name, "__init__");
if (doc) entry->doc = doc;
install_function(name, entry, is_method, overload_sibling);
} }
private: private:
static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject * /* kwargs */) { static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject * /* kwargs */) {
function_entry *overloads = (function_entry *) PyCapsule_GetPointer(self, nullptr); function_entry *overloads = (function_entry *) PyCapsule_GetPointer(self, nullptr);
@ -107,19 +98,31 @@ private:
} }
} }
void install_function(const char *name, function_entry *entry, bool is_method, function overload_sibling) { void initialize(const char *name, const char *doc,
if (!overload_sibling.ptr() || !PyCFunction_Check(overload_sibling.ptr())) { const std::string &signature, function sibling,
bool is_method, std::function<PyObject *(PyObject *)> &&impl) {
if (name == nullptr)
name = "";
/* Linked list of function call handlers (for overloading) */
function_entry *entry = new function_entry();
entry->impl = std::move(impl);
entry->is_constructor = !strcmp(name, "__init__");
entry->signature = signature;
if (doc) entry->doc = doc;
if (!sibling.ptr() || !PyCFunction_Check(sibling.ptr())) {
PyMethodDef *def = new PyMethodDef(); PyMethodDef *def = new PyMethodDef();
memset(def, 0, sizeof(PyMethodDef)); memset(def, 0, sizeof(PyMethodDef));
def->ml_name = strdup(name); def->ml_name = name != nullptr ? strdup(name) : name;
def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher); def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher);
def->ml_flags = METH_VARARGS | METH_KEYWORDS; def->ml_flags = METH_VARARGS | METH_KEYWORDS;
capsule entry_capsule(entry); capsule entry_capsule(entry);
m_ptr = PyCFunction_New(def, entry_capsule.ptr()); m_ptr = PyCFunction_New(def, entry_capsule.ptr());
if (!m_ptr) if (!m_ptr)
throw std::runtime_error("function::function(): Could not allocate function object"); throw std::runtime_error("cpp_function::cpp_function(): Could not allocate function object");
} else { } else {
m_ptr = overload_sibling.ptr(); m_ptr = sibling.ptr();
inc_ref(); inc_ref();
capsule entry_capsule(PyCFunction_GetSelf(m_ptr), true); capsule entry_capsule(PyCFunction_GetSelf(m_ptr), true);
function_entry *parent = (function_entry *) entry_capsule, *backup = parent; function_entry *parent = (function_entry *) entry_capsule, *backup = parent;
@ -144,12 +147,22 @@ private:
if (is_method) { if (is_method) {
m_ptr = PyInstanceMethod_New(m_ptr); m_ptr = PyInstanceMethod_New(m_ptr);
if (!m_ptr) if (!m_ptr)
throw std::runtime_error("function::function(): Could not allocate instance method object"); throw std::runtime_error("cpp_function::cpp_function(): Could not allocate instance method object");
Py_DECREF(func); Py_DECREF(func);
} }
} }
}; };
class cpp_method : public cpp_function {
public:
cpp_method () { }
template <typename Func>
cpp_method(Func &&_func, const char *name = nullptr, const char *doc = nullptr,
return_value_policy policy = return_value_policy::automatic,
function sibling = function())
: cpp_function(std::forward<Func>(_func), name, doc, policy, sibling, true) { }
};
class module : public object { class module : public object {
public: public:
PYTHON_OBJECT_DEFAULT(module, object, PyModule_Check) PYTHON_OBJECT_DEFAULT(module, object, PyModule_Check)
@ -167,17 +180,21 @@ public:
inc_ref(); inc_ref();
} }
template <typename Func> module& def(const char *name, Func f, const char *doc = nullptr) { template <typename Func>
function func(name, f, false, (function) attr(name), doc); module &def(const char *name, Func f, const char *doc = nullptr,
return_value_policy policy = return_value_policy::automatic) {
cpp_function func(f, name, doc, policy, (function) attr(name));
func.inc_ref(); /* The following line steals a reference to 'func' */ func.inc_ref(); /* The following line steals a reference to 'func' */
PyModule_AddObject(ptr(), name, func.ptr()); PyModule_AddObject(ptr(), name, func.ptr());
return *this; return *this;
} }
module def_submodule(const char *name) { module def_submodule(const char *name, const char *doc = nullptr) {
std::string full_name = std::string(PyModule_GetName(m_ptr)) std::string full_name = std::string(PyModule_GetName(m_ptr))
+ std::string(".") + std::string(name); + std::string(".") + std::string(name);
module result(PyImport_AddModule(full_name.c_str()), true); module result(PyImport_AddModule(full_name.c_str()), true);
if (doc)
result.attr("__doc__") = pybind::str(doc);
attr(name) = result; attr(name) = result;
return result; return result;
} }
@ -218,7 +235,6 @@ public:
type->ht_name = type->ht_qualname = name; type->ht_name = type->ht_qualname = name;
type->ht_type.tp_name = strdup(full_name.c_str()); type->ht_type.tp_name = strdup(full_name.c_str());
type->ht_type.tp_basicsize = instance_size; type->ht_type.tp_basicsize = instance_size;
type->ht_type.tp_doc = doc;
type->ht_type.tp_init = (initproc) init; type->ht_type.tp_init = (initproc) init;
type->ht_type.tp_new = (newfunc) new_instance; type->ht_type.tp_new = (newfunc) new_instance;
type->ht_type.tp_dealloc = dealloc; type->ht_type.tp_dealloc = dealloc;
@ -244,6 +260,8 @@ public:
type_info.type_size = type_size; type_info.type_size = type_size;
type_info.init_holder = init_holder; type_info.init_holder = init_holder;
attr("__pybind__") = capsule(&type_info); attr("__pybind__") = capsule(&type_info);
if (doc)
attr("__doc__") = pybind::str(doc);
scope.attr(name) = *this; scope.attr(name) = *this;
} }
@ -366,14 +384,14 @@ public:
template <typename Func> template <typename Func>
class_ &def(const char *name, Func f, const char *doc = nullptr, class_ &def(const char *name, Func f, const char *doc = nullptr,
return_value_policy policy = return_value_policy::automatic) { return_value_policy policy = return_value_policy::automatic) {
attr(name) = function(name, f, true, (function) attr(name), doc, policy); attr(name) = cpp_method(f, name, doc, policy, (function) attr(name));
return *this; return *this;
} }
template <typename Func> class_ & template <typename Func> class_ &
def_static(const char *name, Func f, const char *doc = nullptr, def_static(const char *name, Func f, const char *doc = nullptr,
return_value_policy policy = return_value_policy::automatic) { return_value_policy policy = return_value_policy::automatic) {
attr(name) = function(name, f, false, (function) attr(name), doc, policy); attr(name) = cpp_function(f, name, doc, policy, (function) attr(name));
return *this; return *this;
} }
@ -409,19 +427,19 @@ public:
template <typename C, typename D> template <typename C, typename D>
class_ &def_readwrite(const char *name, D C::*pm, class_ &def_readwrite(const char *name, D C::*pm,
const char *doc = nullptr) { const char *doc = nullptr) {
function fget("", [=](C * ptr) -> D & { return ptr->*pm; }, true, cpp_method fget([pm](const C &c) -> const D &{ return c.*pm; }, nullptr,
function(), doc, return_value_policy::reference_internal), nullptr, return_value_policy::reference_internal),
fset("", [=](C *ptr, const D &value) { ptr->*pm = value; }, true, function(), doc); fset([pm](C &c, const D &value) { c.*pm = value; });
def_property(name, fget, fset, doc); def_property(name, fget, fset, doc);
return *this; return *this;
} }
template <typename C, typename D> template <typename C, typename D>
class_ &def_readonly(const char *name, const D C::*pm, class_ &def_readonly(const char *name, const D C::*pm,
const char *doc = nullptr) { const char *doc = nullptr) {
function fget("", [=](C * ptr) -> const D & { return ptr->*pm; }, true, cpp_method fget([pm](const C &c) -> const D &{ return c.*pm; }, nullptr,
function(), doc, return_value_policy::reference_internal); nullptr, return_value_policy::reference_internal);
def_property(name, fget, doc); def_property(name, fget, doc);
return *this; return *this;
} }
@ -429,8 +447,9 @@ public:
template <typename D> template <typename D>
class_ &def_readwrite_static(const char *name, D *pm, class_ &def_readwrite_static(const char *name, D *pm,
const char *doc = nullptr) { const char *doc = nullptr) {
function fget("", [=](object) -> D & { return *pm; }, true), cpp_function fget([pm](object) -> const D &{ return *pm; }, nullptr,
fset("", [=](object, const D &value) { *pm = value; }, true); nullptr, return_value_policy::reference_internal),
fset([pm](object, const D &value) { *pm = value; });
def_property_static(name, fget, fset, doc); def_property_static(name, fget, fset, doc);
return *this; return *this;
} }
@ -438,25 +457,26 @@ public:
template <typename D> template <typename D>
class_ &def_readonly_static(const char *name, const D *pm, class_ &def_readonly_static(const char *name, const D *pm,
const char *doc = nullptr) { const char *doc = nullptr) {
function fget("", [=](object) -> const D & { return *pm; }, true); cpp_function fget([pm](object) -> const D &{ return *pm; }, nullptr,
nullptr, return_value_policy::reference_internal);
def_property_static(name, fget, doc); def_property_static(name, fget, doc);
return *this; return *this;
} }
class_ &def_property(const char *name, const function &fget, class_ &def_property(const char *name, const cpp_method &fget,
const char *doc = nullptr) { const char *doc = nullptr) {
def_property(name, fget, function(), doc); def_property(name, fget, cpp_method(), doc);
return *this; return *this;
} }
class_ &def_property_static(const char *name, const function &fget, class_ &def_property_static(const char *name, const cpp_function &fget,
const char *doc = nullptr) { const char *doc = nullptr) {
def_property_static(name, fget, function(), doc); def_property_static(name, fget, cpp_function(), doc);
return *this; return *this;
} }
class_ &def_property(const char *name, const function &fget, class_ &def_property(const char *name, const cpp_method &fget,
const function &fset, const char *doc = nullptr) { const cpp_method &fset, const char *doc = nullptr) {
object property( object property(
PyObject_CallFunction((PyObject *)&PyProperty_Type, PyObject_CallFunction((PyObject *)&PyProperty_Type,
const_cast<char *>("OOOs"), fget.ptr() ? fget.ptr() : Py_None, const_cast<char *>("OOOs"), fget.ptr() ? fget.ptr() : Py_None,
@ -465,8 +485,8 @@ public:
return *this; return *this;
} }
class_ &def_property_static(const char *name, const function &fget, class_ &def_property_static(const char *name, const cpp_function &fget,
const function &fset, const cpp_function &fset,
const char *doc = nullptr) { const char *doc = nullptr) {
object property( object property(
PyObject_CallFunction((PyObject *)&PyProperty_Type, PyObject_CallFunction((PyObject *)&PyProperty_Type,
@ -583,5 +603,3 @@ NAMESPACE_END(pybind)
#undef PYTHON_OBJECT #undef PYTHON_OBJECT
#undef PYTHON_OBJECT_DEFAULT #undef PYTHON_OBJECT_DEFAULT
#endif /* __PYBIND_H */

View File

@ -7,10 +7,9 @@
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
*/ */
#if !defined(__PYBIND_PYTYPES_H) #pragma once
#define __PYBIND_PYTYPES_H
#include "common.h" #include <pybind/common.h>
#include <utility> #include <utility>
NAMESPACE_BEGIN(pybind) NAMESPACE_BEGIN(pybind)
@ -208,7 +207,7 @@ inline detail::accessor handle::attr(const char *key) { return detail::accessor(
#define PYTHON_OBJECT_DEFAULT(Name, Parent, CheckFun) \ #define PYTHON_OBJECT_DEFAULT(Name, Parent, CheckFun) \
PYTHON_OBJECT(Name, Parent, CheckFun) \ PYTHON_OBJECT(Name, Parent, CheckFun) \
Name() : object() { } Name() : Parent() { }
class str : public object { class str : public object {
public: public:
@ -295,6 +294,11 @@ public:
void append(const object &object) { PyList_Append(m_ptr, (PyObject *) object.ptr()); } void append(const object &object) { PyList_Append(m_ptr, (PyObject *) object.ptr()); }
}; };
class function : public object {
public:
PYTHON_OBJECT_DEFAULT(function, object, PyFunction_Check)
};
class buffer : public object { class buffer : public object {
public: public:
PYTHON_OBJECT_DEFAULT(buffer, object, PyObject_CheckBuffer) PYTHON_OBJECT_DEFAULT(buffer, object, PyObject_CheckBuffer)
@ -335,5 +339,3 @@ inline internals &get_internals() {
} }
NAMESPACE_END(detail) NAMESPACE_END(detail)
NAMESPACE_END(pybind) NAMESPACE_END(pybind)
#endif /* __PYBIND_PYTYPES_H */

View File

@ -7,10 +7,9 @@
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
*/ */
#if !defined(__PYBIND_TYPEID_H) #pragma once
#define __PYBIND_TYPEID_H
#include "common.h" #include <pybind/typeid.h>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#if defined(__GNUG__) #if defined(__GNUG__)
@ -48,6 +47,3 @@ template <typename T> static std::string type_id() {
} }
NAMESPACE_END(pybind) NAMESPACE_END(pybind)
#endif /* __PYBIND_TYPEID_H */