Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert Python stuff but the py_context instantiation change
[simgrid.git] / src / bindings / python / simgrid_python.cpp
index 87b68bd..96e067b 100644 (file)
@@ -84,6 +84,9 @@ PYBIND11_MODULE(simgrid, m)
   // Swapped contexts are broken, starting from pybind11 v2.8.0.  Use thread contexts by default.
   simgrid::s4u::Engine::set_config("contexts/factory:thread");
 
+  // Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack)
+  static py::object pyForcefulKillEx(py::register_exception<simgrid::ForcefulKillException>(m, "ActorKilled"));
+
   py::register_exception<simgrid::NetworkFailureException>(m, "NetworkFailureException");
   py::register_exception<simgrid::TimeoutException>(m, "TimeoutException");
   py::register_exception<simgrid::HostFailureException>(m, "HostFailureException");
@@ -124,8 +127,8 @@ PYBIND11_MODULE(simgrid, m)
             py::function fun = py::reinterpret_borrow<py::function>(cb);
             fun.inc_ref(); // FIXME: why is this needed for tests like actor-kill and actor-lifetime?
             simgrid::s4u::this_actor::on_exit([fun](bool /*failed*/) {
+              py::gil_scoped_acquire py_context; // need a new context for callback
               try {
-                py::gil_scoped_acquire py_context; // need a new context for callback
                 fun();
               } catch (const py::error_already_set& e) {
                 xbt_die("Error while executing the on_exit lambda: %s", e.what());
@@ -206,8 +209,8 @@ PYBIND11_MODULE(simgrid, m)
           "register_actor",
           [](Engine* e, const std::string& name, py::object fun_or_class) {
             e->register_actor(name, [fun_or_class](std::vector<std::string> args) {
+              py::gil_scoped_acquire py_context;
               try {
-                py::gil_scoped_acquire py_context;
                 /* Convert the std::vector into a py::tuple */
                 py::tuple params(args.size() - 1);
                 for (size_t i = 1; i < args.size(); i++)
@@ -218,16 +221,10 @@ PYBIND11_MODULE(simgrid, m)
                 if (py::isinstance<py::function>(res))
                   res();
               } catch (const py::error_already_set& ex) {
-                XBT_VERB("Actor killed because %s",ex.what());
-               if(ex.matches(PyExc_FileNotFoundError)) {
-                  XBT_INFO("Took if");
-                 simgrid::ForcefulKillException::do_throw();
-               } 
-                XBT_INFO("Over");
-               //if(ex.matches(PyExc_RuntimeError)) {
-                //  simgrid::ForcefulKillException::do_throw();
-               //} else
-               //  xbt_die("Did not expect this kind of exception from Python");
+                if (ex.matches(pyForcefulKillEx)) {
+                  XBT_VERB("Actor killed");
+                  simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
+                }
                 throw;
               }
             });
@@ -446,8 +443,8 @@ PYBIND11_MODULE(simgrid, m)
           [](py::object cb) {
             Host::on_creation_cb([cb](Host& h) {
               py::function fun = py::reinterpret_borrow<py::function>(cb);
+              py::gil_scoped_acquire py_context; // need a new context for callback
               try {
-                py::gil_scoped_acquire py_context; // need a new context for callback
                 fun(&h);
               } catch (const py::error_already_set& e) {
                 xbt_die("Error while executing the on_creation lambda : %s", e.what());
@@ -746,16 +743,14 @@ PYBIND11_MODULE(simgrid, m)
             fun.inc_ref();  // FIXME: why is this needed for tests like exec-async, exec-dvfs and exec-remote?
             args.inc_ref(); // FIXME: why is this needed for tests like actor-migrate?
             return simgrid::s4u::Actor::create(name, h, [fun, args]() {
+              py::gil_scoped_acquire py_context;
               try {
-                py::gil_scoped_acquire py_context;
                 fun(*args);
               } catch (const py::error_already_set& ex) {
-                XBT_INFO("Actor killed because %s",ex.what());
-               if(ex.matches(PyExc_FileNotFoundError)) {
-                  XBT_INFO("Took if");
-                 simgrid::ForcefulKillException::do_throw();
-               } 
-                XBT_INFO("Over");
+                if (ex.matches(pyForcefulKillEx)) {
+                  XBT_VERB("Actor killed");
+                  simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
+                }
                 throw;
               }
             });