Exemple #1
0
/**
   @param persist: If true, retain the Python interpreter,
                   else finalize it
   @param code: The multiline string of Python code.
                The last line is evaluated to the returned result
   @param output: Store result pointer here
   @return Tcl error code
 */
static int
python_eval(bool persist, const char* code, const char* expression,
            Tcl_Obj** output)
{
  int rc;
  char* result = python_result_default;

  // Initialize:
  rc = python_init();
  TCL_CHECK(rc);

  // Execute code:
  DEBUG_TCL_TURBINE("python: code: %s", code);
  PyObject* localDictionary = PyDict_New();
  PyRun_String(code, Py_file_input, main_dict, localDictionary);
  if (PyErr_Occurred()) return handle_python_exception();

  // Evaluate expression:
  DEBUG_TCL_TURBINE("python: expression: %s", expression);
  PyObject* o = PyRun_String(expression, Py_eval_input, main_dict, localDictionary);
  if (o == NULL) return handle_python_exception();

  // Convert Python result to C string, then to Tcl string:
  rc = PyArg_Parse(o, "s", &result);
  if (rc != 1) return handle_python_non_string(o);
  DEBUG_TCL_TURBINE("python: result: %s\n", result);
  *output = Tcl_NewStringObj(result, -1);

  // Clean up and return:
  Py_DECREF(o);
  if (!persist) python_finalize();
  return TCL_OK;
}
Exemple #2
0
static int python_init(void)
{
  if (initialized) return TCL_OK;
  DEBUG_TCL_TURBINE("python: initializing...");
  Py_InitializeEx(1);
  main_module  = PyImport_AddModule("__main__");
  if (main_module == NULL) return handle_python_exception();
  main_dict = PyModule_GetDict(main_module);
  if (main_dict == NULL) return handle_python_exception();
  initialized = true;
  return TCL_OK;
}
Exemple #3
0
static int
Turbine_Debug_Cmd(ClientData cdata, Tcl_Interp *interp,
                  int objc, Tcl_Obj *const objv[])
{
  TCL_ARGS(2);

  if (turbine_debug_enabled)
  {
    unused char* msg = Tcl_GetString(objv[1]);
    DEBUG_TCL_TURBINE("%s", msg);
    return TCL_OK;
  }
  else
  {
    return TCL_OK;
  }
}