From 86d859d4300aeb3e0a917a1e7266ca5665f89e09 Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Thu, 19 Apr 2018 01:13:40 +0100 Subject: [PATCH] Allow anonymous Python functions to return values --- src/runner/pythonrunner.cpp | 68 ++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/src/runner/pythonrunner.cpp b/src/runner/pythonrunner.cpp index 5c2e12c..1de2878 100644 --- a/src/runner/pythonrunner.cpp +++ b/src/runner/pythonrunner.cpp @@ -630,6 +630,7 @@ int getArgCount(PyObject *object) { return result; } +template struct Functor0_converter { @@ -638,9 +639,9 @@ struct Functor0_converter FunctorWrapper(boost::python::object callable) : m_Callable(callable) { } - void operator()() { + RET operator()() { GILock lock; - m_Callable(); + return (RET) m_Callable(); } boost::python::object m_Callable; @@ -648,7 +649,7 @@ struct Functor0_converter Functor0_converter() { - bpy::converter::registry::push_back(&convertible, &construct, bpy::type_id>()); + bpy::converter::registry::push_back(&convertible, &construct, bpy::type_id>()); } static void *convertible(PyObject *object) @@ -663,14 +664,55 @@ struct Functor0_converter static void construct(PyObject *object, bpy::converter::rvalue_from_python_stage1_data *data) { bpy::object callable(bpy::handle<>(bpy::borrowed(object))); - void *storage = ((bpy::converter::rvalue_from_python_storage>*)data)->storage.bytes; - new (storage) std::function(FunctorWrapper(callable)); + void *storage = ((bpy::converter::rvalue_from_python_storage>*)data)->storage.bytes; + new (storage) std::function(FunctorWrapper(callable)); data->convertible = storage; } }; -template +template +struct Functor1_converter +{ + + struct FunctorWrapper + { + FunctorWrapper(boost::python::object callable) : m_Callable(callable) { + } + + RET operator()(const PAR1 ¶m1) { + GILock lock; + return (RET) m_Callable(param1); + } + + boost::python::object m_Callable; + }; + + Functor1_converter() + { + bpy::converter::registry::push_back(&convertible, &construct, bpy::type_id>()); + } + + static void *convertible(PyObject *object) + { + if (!PyCallable_Check(object) + || (getArgCount(object) != 1)) { + return nullptr; + } + return object; + } + + static void construct(PyObject *object, bpy::converter::rvalue_from_python_stage1_data *data) + { + bpy::object callable(bpy::handle<>(bpy::borrowed(object))); + void *storage = ((bpy::converter::rvalue_from_python_storage>*)data)->storage.bytes; + new (storage) std::function(FunctorWrapper(callable)); + data->convertible = storage; + } +}; + + +template struct Functor2_converter { @@ -679,9 +721,9 @@ struct Functor2_converter FunctorWrapper(boost::python::object callable) : m_Callable(callable) { } - void operator()(const PAR1 ¶m1, const PAR2 ¶m2) { + RET operator()(const PAR1 ¶m1, const PAR2 ¶m2) { GILock lock; - m_Callable(param1, param2); + return (RET) m_Callable(param1, param2); } boost::python::object m_Callable; @@ -689,7 +731,7 @@ struct Functor2_converter Functor2_converter() { - bpy::converter::registry::push_back(&convertible, &construct, bpy::type_id>()); + bpy::converter::registry::push_back(&convertible, &construct, bpy::type_id>()); } static void *convertible(PyObject *object) @@ -704,8 +746,8 @@ struct Functor2_converter static void construct(PyObject *object, bpy::converter::rvalue_from_python_stage1_data *data) { bpy::object callable(bpy::handle<>(bpy::borrowed(object))); - void *storage = ((bpy::converter::rvalue_from_python_storage>*)data)->storage.bytes; - new (storage) std::function(FunctorWrapper(callable)); + void *storage = ((bpy::converter::rvalue_from_python_storage>*)data)->storage.bytes; + new (storage) std::function(FunctorWrapper(callable)); data->convertible = storage; } }; @@ -894,7 +936,7 @@ BOOST_PYTHON_MODULE(mobase) bpy::to_python_converter>(); QFlags_from_python_obj(); - Functor0_converter(); // converter for the onRefreshed-callback + Functor0_converter(); // converter for the onRefreshed-callback bpy::class_("IPluginList") .def("state", bpy::pure_virtual(&MOBase::IPluginList::state)) @@ -912,7 +954,7 @@ BOOST_PYTHON_MODULE(mobase) bpy::to_python_converter>(); QFlags_from_python_obj(); - Functor2_converter(); // converter for the onModStateChanged-callback + Functor2_converter(); // converter for the onModStateChanged-callback bpy::class_("IModList") .def("displayName", bpy::pure_virtual(&MOBase::IModList::displayName))