Example #1
0
//
// Try running a script on the initiator
bool try_reset_script(RESET_DATA *reset, void *initiator, int initiator_type,
		      const char *locale) {
  PyObject *pyme = NULL;
  PyObject *dict = NULL;
  if(initiator_type == INITIATOR_ROOM)
    pyme = roomGetPyFormBorrowed(initiator);
  else if(initiator_type == INITIATOR_THEN_OBJ)
    pyme = objGetPyFormBorrowed(initiator);
  else if(initiator_type == INITIATOR_THEN_MOB)
    pyme = charGetPyFormBorrowed(initiator);
  else
    return FALSE;

  // build our dictionary and add ourself to it as 'me'
  dict = restricted_script_dict();
  PyDict_SetItemString(dict, "me", pyme);

  // run the script
  run_script(dict, resetGetArg(reset), locale);

  // check to see if we had an error
  if(!last_script_ok())
    log_pyerr("Reset script in locale %s terminated with an error:\r\n%s",
	      locale, resetGetArg(reset));

  // garbage collection and return our outcome
  Py_DECREF(dict);
  return last_script_ok();
}
PyObject *run_script_forcode(PyObject *dict, const char *script, 
			     const char *locale) {
  // try compiling the code
  PyObject *retval = Py_CompileString(script, "<string>", Py_file_input);

  // try running the code
  if(retval != NULL)
    run_code(retval, dict, locale);
  
  // did we end up with an error?
  if(retval == NULL || !last_script_ok())
    log_pyerr("Script terminated with an error:\r\n%s", script);

  // return our code object
  return retval;
}