Unverified Commit 16730fc6 authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #943

Fix compilation of the python binding and apply clang format
parents a685a67c 59f30d35
...@@ -18,12 +18,12 @@ ...@@ -18,12 +18,12 @@
#include "caf/config.hpp" #include "caf/config.hpp"
#include <set>
#include <chrono> #include <chrono>
#include <thread> #include <functional>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <functional> #include <set>
#include <thread>
#include <unordered_map> #include <unordered_map>
CAF_PUSH_WARNINGS CAF_PUSH_WARNINGS
...@@ -33,8 +33,8 @@ CAF_POP_WARNINGS ...@@ -33,8 +33,8 @@ CAF_POP_WARNINGS
#include "caf/all.hpp" #include "caf/all.hpp"
#include "caf/io/all.hpp" #include "caf/io/all.hpp"
using std::cout;
using std::cerr; using std::cerr;
using std::cout;
using std::endl; using std::endl;
namespace { namespace {
...@@ -91,15 +91,10 @@ def receive(timeout = None, msg_filter = no_receive_filter): ...@@ -91,15 +91,10 @@ def receive(timeout = None, msg_filter = no_receive_filter):
namespace caf { namespace caf {
void register_class(atom_value*, pybind11::module& m, void register_class(atom_value*, pybind11::module& m, const std::string& name) {
const std::string& name) { auto repr_fun = [](atom_value x) { return "atom('" + to_string(x) + "')"; };
auto repr_fun = [](atom_value x) { auto cmp = [](atom_value x, atom_value y) { return x == y; };
return "atom('" + to_string(x) + "')"; std::string (*to_string_fun)(atom_value) = &to_string;
};
auto cmp = [](atom_value x, atom_value y) {
return x == y;
};
std::string (*to_string_fun)(const atom_value&) = &to_string;
pybind11::class_<atom_value>(m, name.c_str()) pybind11::class_<atom_value>(m, name.c_str())
.def("__str__", to_string_fun) .def("__str__", to_string_fun)
.def("__repr__", repr_fun) .def("__repr__", repr_fun)
...@@ -112,13 +107,12 @@ namespace { ...@@ -112,13 +107,12 @@ namespace {
class binding { class binding {
public: public:
binding(std::string py_name, bool builtin_type) binding(std::string py_name, bool builtin_type)
: python_name_(std::move(py_name)), : python_name_(std::move(py_name)), builtin_(builtin_type) {
builtin_(builtin_type) {
// nop // nop
} }
virtual ~binding() { virtual ~binding() {
//nop // nop
} }
inline void docstring(std::string x) { inline void docstring(std::string x) {
...@@ -201,13 +195,14 @@ template <class T> ...@@ -201,13 +195,14 @@ template <class T>
class has_register_class { class has_register_class {
private: private:
template <class U> template <class U>
static auto test(U* x) -> decltype(register_class(x, static auto test(U* x)
std::declval<pybind11::module&>(), -> decltype(register_class(x, std::declval<pybind11::module&>(),
std::declval<const std::string&>())); std::declval<const std::string&>()));
static auto test(...) -> std::false_type; static auto test(...) -> std::false_type;
using type = decltype(test(static_cast<T*>(nullptr))); using type = decltype(test(static_cast<T*>(nullptr)));
public: public:
static constexpr bool value = std::is_same<type, void>::value; static constexpr bool value = std::is_same<type, void>::value;
}; };
...@@ -221,39 +216,29 @@ private: ...@@ -221,39 +216,29 @@ private:
static auto test(...) -> void; static auto test(...) -> void;
using type = decltype(test(static_cast<T*>(nullptr))); using type = decltype(test(static_cast<T*>(nullptr)));
public: public:
static constexpr bool value = std::is_same<type, std::string>::value; static constexpr bool value = std::is_same<type, std::string>::value;
}; };
template <class T> template <class T>
typename std::enable_if< typename std::enable_if<!has_register_class<T>::value
!has_register_class<T>::value && has_to_string<T>::value>::type
&& has_to_string<T>::value
>::type
default_python_class_init(pybind11::module& m, const std::string& name) { default_python_class_init(pybind11::module& m, const std::string& name) {
auto str_impl = [](const T& x) { auto str_impl = [](const T& x) { return to_string(x); };
return to_string(x); pybind11::class_<T>(m, name.c_str()).def("__str__", str_impl);
};
pybind11::class_<T>(m, name.c_str())
.def("__str__", str_impl);
} }
template <class T> template <class T>
typename std::enable_if< typename std::enable_if<!has_register_class<T>::value
!has_register_class<T>::value && !has_to_string<T>::value>::type
&& !has_to_string<T>::value
>::type
default_python_class_init(pybind11::module& m, const std::string& name) { default_python_class_init(pybind11::module& m, const std::string& name) {
auto str_impl = [](const T& x) { auto str_impl = [](const T& x) { return to_string(x); };
return to_string(x);
};
pybind11::class_<T>(m, name.c_str()); pybind11::class_<T>(m, name.c_str());
} }
template <class T> template <class T>
typename std::enable_if< typename std::enable_if<has_register_class<T>::value>::type
has_register_class<T>::value
>::type
default_python_class_init(pybind11::module& m, const std::string& name) { default_python_class_init(pybind11::module& m, const std::string& name) {
register_class(static_cast<T*>(nullptr), m, name); register_class(static_cast<T*>(nullptr), m, name);
} }
...@@ -269,7 +254,8 @@ public: ...@@ -269,7 +254,8 @@ public:
absolute_receive_timeout() = default; absolute_receive_timeout() = default;
absolute_receive_timeout(const absolute_receive_timeout&) = default; absolute_receive_timeout(const absolute_receive_timeout&) = default;
absolute_receive_timeout& operator=(const absolute_receive_timeout&) = default; absolute_receive_timeout& operator=(const absolute_receive_timeout&)
= default;
const clock_type::time_point& value() const { const clock_type::time_point& value() const {
return x_; return x_;
...@@ -307,7 +293,8 @@ public: ...@@ -307,7 +293,8 @@ public:
std::string pre_run; std::string pre_run;
std::string banner = default_banner; std::string banner = default_banner;
using register_fun = std::function<void (pybind11::module&, const std::string&)>; using register_fun = std::function<void(pybind11::module&,
const std::string&)>;
py_config() { py_config() {
// allow CAF to convert native Python types to C++ types // allow CAF to convert native Python types to C++ types
...@@ -359,9 +346,7 @@ public: ...@@ -359,9 +346,7 @@ public:
oss << "import IPython" << endl oss << "import IPython" << endl
<< "c = IPython.Config()" << endl << "c = IPython.Config()" << endl
<< "c.InteractiveShellApp.exec_lines = [" << endl << "c.InteractiveShellApp.exec_lines = [" << endl
<< R"(""")" << R"(""")" << full_pre_run << R"(""")" << endl
<< full_pre_run
<< R"(""")" << endl
<< "]" << endl << "]" << endl
<< "c.PromptManager.in_template = ' $: '" << endl << "c.PromptManager.in_template = ' $: '" << endl
<< "c.PromptManager.in2_template = ' -> '" << endl << "c.PromptManager.in2_template = ' -> '" << endl
...@@ -378,7 +363,8 @@ public: ...@@ -378,7 +363,8 @@ public:
return bindings_; return bindings_;
} }
const std::unordered_map<std::string, cpp_binding*>& portable_bindings() const { const std::unordered_map<std::string, cpp_binding*>&
portable_bindings() const {
return portable_bindings_; return portable_bindings_;
} }
...@@ -417,7 +403,7 @@ private: ...@@ -417,7 +403,7 @@ private:
std::unordered_map<std::string, cpp_binding_ptr> cpp_bindings_; std::unordered_map<std::string, cpp_binding_ptr> cpp_bindings_;
std::unordered_map<std::string, py_binding_ptr> py_bindings_; std::unordered_map<std::string, py_binding_ptr> py_bindings_;
std::vector<std::function<void (pybind11::module&)>> register_funs_; std::vector<std::function<void(pybind11::module&)>> register_funs_;
}; };
struct py_context { struct py_context {
const py_config& cfg; const py_config& cfg;
...@@ -440,7 +426,6 @@ void set_py_exception_fill(std::ostream& oss, T&& x, Ts&&... xs) { ...@@ -440,7 +426,6 @@ void set_py_exception_fill(std::ostream& oss, T&& x, Ts&&... xs) {
set_py_exception_fill(oss << std::forward<T>(x), std::forward<Ts>(xs)...); set_py_exception_fill(oss << std::forward<T>(x), std::forward<Ts>(xs)...);
} }
template <class... Ts> template <class... Ts>
void set_py_exception(Ts&&... xs) { void set_py_exception(Ts&&... xs) {
std::ostringstream oss; std::ostringstream oss;
...@@ -462,8 +447,8 @@ void py_send(const pybind11::args& xs) { ...@@ -462,8 +447,8 @@ void py_send(const pybind11::args& xs) {
std::string type_name = PyEval_GetFuncName((*i).ptr()); std::string type_name = PyEval_GetFuncName((*i).ptr());
auto kvp = bindings.find(type_name); auto kvp = bindings.find(type_name);
if (kvp == bindings.end()) { if (kvp == bindings.end()) {
set_py_exception(R"(Unable to add element of type ")", set_py_exception(R"(Unable to add element of type ")", type_name,
type_name, R"(" to message: type is unknown to CAF)"); R"(" to message: type is unknown to CAF)");
return; return;
} }
kvp->second->append(mb, *i); kvp->second->append(mb, *i);
...@@ -480,8 +465,9 @@ pybind11::tuple tuple_from_message(const type_erased_tuple& msg) { ...@@ -480,8 +465,9 @@ pybind11::tuple tuple_from_message(const type_erased_tuple& msg) {
auto rtti = msg.type(i); auto rtti = msg.type(i);
auto str = types.portable_name(rtti); auto str = types.portable_name(rtti);
if (str == types.default_type_name()) { if (str == types.default_type_name()) {
set_py_exception("Unable to extract element #", i, " from message: ", set_py_exception("Unable to extract element #", i,
"could not get portable name of ", rtti.second->name()); " from message: ", "could not get portable name of ",
rtti.second->name());
return pybind11::tuple{}; return pybind11::tuple{};
} }
auto kvp = bindings.find(str); auto kvp = bindings.find(str);
...@@ -534,8 +520,8 @@ struct foo { ...@@ -534,8 +520,8 @@ struct foo {
template <class Processor> template <class Processor>
void serialize(Processor& proc, foo& f, const unsigned int) { void serialize(Processor& proc, foo& f, const unsigned int) {
proc & f.x; proc& f.x;
proc & f.y; proc& f.y;
} }
std::string to_string(const foo& x) { std::string to_string(const foo& x) {
...@@ -554,11 +540,11 @@ void register_class(foo*, pybind11::module& m, const std::string& name) { ...@@ -554,11 +540,11 @@ void register_class(foo*, pybind11::module& m, const std::string& name) {
} }
#if PY_MAJOR_VERSION == 3 #if PY_MAJOR_VERSION == 3
#define CAF_MODULE_INIT_RES PyObject* # define CAF_MODULE_INIT_RES PyObject*
#define CAF_MODULE_INIT_RET(res) return res; # define CAF_MODULE_INIT_RET(res) return res;
#else #else
#define CAF_MODULE_INIT_RES void # define CAF_MODULE_INIT_RES void
#define CAF_MODULE_INIT_RET(unused) # define CAF_MODULE_INIT_RET(unused)
#endif #endif
CAF_MODULE_INIT_RES caf_module_init() { CAF_MODULE_INIT_RES caf_module_init() {
...@@ -568,7 +554,8 @@ CAF_MODULE_INIT_RES caf_module_init() { ...@@ -568,7 +554,8 @@ CAF_MODULE_INIT_RES caf_module_init() {
// add free functions // add free functions
m.def("send", &py_send, "Sends a message to an actor") m.def("send", &py_send, "Sends a message to an actor")
.def("dequeue_message", &py_dequeue, "Receives the next message") .def("dequeue_message", &py_dequeue, "Receives the next message")
.def("dequeue_message_with_timeout", &py_dequeue_with_timeout, "Receives the next message") .def("dequeue_message_with_timeout", &py_dequeue_with_timeout,
"Receives the next message")
.def("self", &py_self, "Returns the global self handle") .def("self", &py_self, "Returns the global self handle")
.def("atom", &atom_from_string, "Creates an atom from a string"); .def("atom", &atom_from_string, "Creates an atom from a string");
CAF_MODULE_INIT_RET(m.ptr()) CAF_MODULE_INIT_RET(m.ptr())
...@@ -605,7 +592,7 @@ void caf_main(actor_system& system, const config& cfg) { ...@@ -605,7 +592,7 @@ void caf_main(actor_system& system, const config& cfg) {
// create Python module for CAF // create Python module for CAF
int py_res = 0; int py_res = 0;
if (!cfg.py_file.empty()) { if (!cfg.py_file.empty()) {
auto fp = fopen(cfg.py_file.c_str() , "r"); auto fp = fopen(cfg.py_file.c_str(), "r");
if (fp == nullptr) { if (fp == nullptr) {
cerr << "Unable to open file " << cfg.py_file << endl; cerr << "Unable to open file " << cfg.py_file << endl;
Py_Finalize(); Py_Finalize();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment