X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/bc3a566b71f9e9acf92551b1799abf0a9b891aca..62a552e4489a97a71f3cf9c35a8b6c7dadf89d13:/src/bindings/python/simgrid_python.cpp diff --git a/src/bindings/python/simgrid_python.cpp b/src/bindings/python/simgrid_python.cpp index 57fb111115..4b70a2df59 100644 --- a/src/bindings/python/simgrid_python.cpp +++ b/src/bindings/python/simgrid_python.cpp @@ -8,10 +8,19 @@ #define _hypot hypot #endif +#if defined(__GNUG__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-value" +#endif + #include #include // Must come before our own stuff #include +#if defined(__GNUG__) +#pragma GCC diagnostic pop +#endif + #include "src/kernel/context/Context.hpp" #include #include @@ -59,7 +68,7 @@ PYBIND11_MODULE(simgrid, m) m.attr("simgrid_version") = simgrid_version; // Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack) - py::object pyForcefulKillEx = py::register_exception(m, "ActorKilled"); + static py::object pyForcefulKillEx(py::register_exception(m, "ActorKilled")); /* this_actor namespace */ void (*sleep_for_fun)(double) = &simgrid::s4u::this_actor::sleep_for; // pick the right overload @@ -118,33 +127,30 @@ PYBIND11_MODULE(simgrid, m) ":cpp:func:`simgrid::s4u::Engine::load_deployment()`") .def("run", &Engine::run, "Run the simulation") .def("register_actor", - [pyForcefulKillEx](Engine*, const std::string& name, py::object fun_or_class) { - simgrid::simix::register_function( - name, [pyForcefulKillEx, fun_or_class](std::vector args) -> simgrid::simix::ActorCode { - return [pyForcefulKillEx, fun_or_class, args]() { - try { - /* Convert the std::vector into a py::tuple */ - py::tuple params(args.size() - 1); - for (size_t i = 1; i < args.size(); i++) - params[i - 1] = py::cast(args[i]); + [](Engine* e, const std::string& name, py::object fun_or_class) { + e->register_actor(name, [fun_or_class](std::vector args) { + try { + /* Convert the std::vector into a py::tuple */ + py::tuple params(args.size() - 1); + for (size_t i = 1; i < args.size(); i++) + params[i - 1] = py::cast(args[i]); - py::object res = fun_or_class(*params); + py::object res = fun_or_class(*params); - /* If I was passed a class, I just built an instance, so I need to call it now */ - if (py::isinstance(res)) - res(); - } catch (py::error_already_set& ex) { - if (ex.matches(pyForcefulKillEx)) { - XBT_VERB("Actor killed"); - /* Stop here that ForcefulKill exception which was meant to free the RAII stuff on the stack */ - } else { - throw; - } - } - }; - }); + /* If I was passed a class, I just built an instance, so I need to call it now */ + if (py::isinstance(res)) + res(); + } catch (py::error_already_set& ex) { + if (ex.matches(pyForcefulKillEx)) { + XBT_VERB("Actor killed"); + /* Stop here that ForcefulKill exception which was meant to free the RAII stuff on the stack */ + } else { + throw; + } + } + }); }, - "Registers the main function of an actor, see :cpp:func:`simgrid::s4u::Engine::register_function()`"); + "Registers the main function of an actor, see :cpp:func:`simgrid::s4u::Engine::register_actor()`"); /* Class Host */ py::class_>(m, "Host", "Simulation Engine, see :ref:`class s4u::Host `") @@ -164,23 +170,23 @@ PYBIND11_MODULE(simgrid, m) /* Class Mailbox */ py::class_>(m, "Mailbox", "Mailbox, see :ref:`class s4u::Mailbox `") - .def("__str__", [](Mailbox self) -> const std::string { - return std::string("Mailbox(")+self.get_cname()+")"; + .def("__str__", [](Mailbox* self) -> const std::string { + return std::string("Mailbox(") + self->get_cname() + ")"; }, "Textual representation of the Mailbox`") .def("by_name", &Mailbox::by_name, "Retrieve a Mailbox from its name, see :cpp:func:`simgrid::s4u::Mailbox::by_name()`") .def_property_readonly("name", [](Mailbox* self) -> const std::string { return std::string(self->get_name().c_str()); // Convert from xbt::string because of MC }, "The name of that mailbox, see :cpp:func:`simgrid::s4u::Mailbox::get_name()`") - .def("put", [](Mailbox self, py::object data, int size) { + .def("put", [](Mailbox* self, py::object data, int size) { data.inc_ref(); - self.put(data.ptr(), size); + self->put(data.ptr(), size); }, "Blocking data transmission, see :cpp:func:`void simgrid::s4u::Mailbox::put(void*, uint64_t)`") - .def("put_async", [](Mailbox self, py::object data, int size) -> simgrid::s4u::CommPtr { + .def("put_async", [](Mailbox* self, py::object data, int size) -> simgrid::s4u::CommPtr { data.inc_ref(); - return self.put_async(data.ptr(), size); + return self->put_async(data.ptr(), size); }, "Non-blocking data transmission, see :cpp:func:`void simgrid::s4u::Mailbox::put_async(void*, uint64_t)`") - .def("get", [](Mailbox self) -> py::object { - py::object data = pybind11::reinterpret_steal(pybind11::handle(static_cast(self.get()))); + .def("get", [](Mailbox* self) -> py::object { + py::object data = pybind11::reinterpret_steal(static_cast(self->get())); data.dec_ref(); return data; }, "Blocking data reception, see :cpp:func:`void* simgrid::s4u::Mailbox::get()`"); @@ -228,9 +234,9 @@ PYBIND11_MODULE(simgrid, m) "application, see :ref:`class s4u::Actor `") .def("create", - [pyForcefulKillEx](py::str name, py::object host, py::object fun, py::args args) { + [](py::str name, py::object host, py::object fun, py::args args) { - return simgrid::s4u::Actor::create(name, host.cast(), [fun, args, pyForcefulKillEx]() { + return simgrid::s4u::Actor::create(name, host.cast(), [fun, args]() { try { fun(*args);