Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Workaround build error seen with clang 10 on freebsd.
[simgrid.git] / src / bindings / python / simgrid_python.cpp
index 57fb111..4b70a2d 100644 (file)
@@ -8,10 +8,19 @@
 #define _hypot hypot
 #endif
 
+#if defined(__GNUG__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-value"
+#endif
+
 #include <pybind11/functional.h>
 #include <pybind11/pybind11.h> // Must come before our own stuff
 #include <pybind11/stl.h>
 
+#if defined(__GNUG__)
+#pragma GCC diagnostic pop
+#endif
+
 #include "src/kernel/context/Context.hpp"
 #include <simgrid/Exception.hpp>
 #include <simgrid/s4u/Actor.hpp>
@@ -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<simgrid::ForcefulKillException>(m, "ActorKilled");
+  static py::object pyForcefulKillEx(py::register_exception<simgrid::ForcefulKillException>(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<std::string> 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<std::string> 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<py::function>(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<py::function>(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_<simgrid::s4u::Host, std::unique_ptr<Host, py::nodelete>>(m, "Host", "Simulation Engine, see :ref:`class s4u::Host <API_s4u_Host>`")
@@ -164,23 +170,23 @@ PYBIND11_MODULE(simgrid, m)
 
   /* Class Mailbox */
   py::class_<simgrid::s4u::Mailbox, std::unique_ptr<Mailbox, py::nodelete>>(m, "Mailbox", "Mailbox, see :ref:`class s4u::Mailbox <API_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<py::object>(pybind11::handle(static_cast<PyObject*>(self.get())));
+      .def("get", [](Mailbox* self) -> py::object {
+         py::object data = pybind11::reinterpret_steal<py::object>(static_cast<PyObject*>(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 <API_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<Host*>(), [fun, args, pyForcefulKillEx]() {
+             return simgrid::s4u::Actor::create(name, host.cast<Host*>(), [fun, args]() {
 
                try {
                  fun(*args);