Esempio n. 1
0
//! Executes registered Python code of app/model
void
message_execute(DBusMessage *msg, const char *app, const char *model, const char *method)
{
    PyObject *py_ret;
    PyObject *py_args = pydbus_import(msg);
    char *eStr, *vStr;

    // Validate model and method
    if (validate_model_member(model, method, 0) != 0) {
        bus_reply_unknown_method(msg);
        return;
    }

    // Check policy

    PyObject *model_def = PyDict_GetItemString(PyDict_GetItemString(py_core, "models"), model);
    PyObject *method_def = PyDict_GetItemString(model_def, method);
    char *signature = script_signature(model, method, 1);

    if (PyDict_GetItemString(model_def, method) != Py_None) {
        const char *action_id = PyString_AsString(PyTuple_GetItem(method_def, 1));
        const char *sender = dbus_message_get_sender(my_proc.bus_msg);

        if (strcmp(action_id, "") != 0) {
            int result;
            if (policy_check(sender, action_id, &result) == 0) {
                if (result != POLICY_YES) {
                    bus_reply_error(msg, "Comar.PolicyKit", action_id);
                    return;
                }
            }
            else {
                bus_reply_error(msg, "Comar.PolicyKit", "error");
                return;
            }
        }
    }

    // Execute method
    switch (py_execute(app, model, method, py_args, &py_ret)) {
        case 0:
            // Success
            bus_reply_object(msg, py_ret, signature);
            break;
        case -1:
        case -2:
            // Python exception raised
            py_catch(&eStr, &vStr, 1);
            bus_reply_error(msg, eStr, vStr);
            break;
        case -3:
        case -4:
            // PolicyKit exception raised
            py_catch(&eStr, &vStr, 0);
            bus_reply_error(msg, eStr, vStr);
            break;
    }
}
Esempio n. 2
0
int main(int ac, const char** av)
{
  const struct test_desc tests[] =
  {
    /* TEST_DESC(hi), */
    /* TEST_DESC(hix), */
    TEST_DESC(matmul),
    /* TEST_DESC(arr), */
    TEST_DESC_INVALID
  };

  static const size_t n = 10000;

  size_t i;
  size_t j;
  uint64_t ticks[2];
  double us[2];

  if (py_init())
  {
    printf("py_init() error\n");
    return -1;
  }

  for (i = 0; tests[i].name != NULL; ++i)
  {
    const struct test_desc* const t = &tests[i];
    py_handle_t py;

    printf("---");
    printf("--- %s test\n", t->name);

    if (py_open(&py))
    {
      printf("py_open error\n");
      continue ;
    }

    if (py_compile(&py, t->py_str))
    {
      printf("py_compile error\n");
      goto on_close;
    }

    /* add variables */
    if (t->post_compile(&py))
    {
      printf("py_post_compile error\n");
      goto on_close;
    }

    /* execute python version */
    ticks[0] = rdtsc();
    for (j = 0; j != n; ++j)
    {
      /* if (j && ((j % 100) == 0)) py_collect(&py); */

      t->pre_exec(&py);

      if (py_execute(&py))
      {
	printf("py_execute error (%zu)\n", j);
	goto on_close;
      }
    }
    ticks[1] = rdtsc();
    us[0] = (double)ticks_to_us(sub_ticks(ticks[0], ticks[1])) / 1000000.0;

    py_print_out_vars(&py);

    /* execute c version */
    ticks[0] = rdtsc();
    for (j = 0; j != n; ++j)
    {
      t->pre_exec(&py);
      t->c(&py);
    }
    ticks[1] = rdtsc();
    us[1] = (double)ticks_to_us(sub_ticks(ticks[0], ticks[1])) / 1000000.0;

    py_print_out_vars(&py);

    printf("py = %lfus, c = %lfus\n", us[0], us[1]);

  on_close:
    py_close(&py);
  }

  py_fini();

  return 0;
}