/* Call python function to initialize the python script */ int ices_playlist_python_initialize (playlist_module_t* pm) { PyObject* res; int rc = 1; pm->get_next = playlist_python_get_next; pm->get_metadata = playlist_python_get_metadata; pm->get_lineno = playlist_python_get_lineno; pm->reload = playlist_python_reload; pm->shutdown = playlist_python_shutdown; if (python_init () < 0) return -1; if (pl_init_hook) { if ((res = python_eval (pl_init_hook)) && PyInt_Check (res)) rc = PyInt_AsLong (res); else ices_log_error ("ices_init failed"); Py_XDECREF (res); } return rc; }
/** @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; }
/* * Do any per-module initialization that is separate to each * configured instance of the module. e.g. set up connections * to external databases, read configuration files, set up * dictionary entries, etc. * * If configuration information is given in the config section * that must be referenced in later calls, store a handle to it * in *instance otherwise put a null pointer there. * */ static int python_instantiate(CONF_SECTION *conf, void **instance) { struct rlm_python_t *data = NULL; /* * Set up a storage area for instance data */ if ((data = malloc(sizeof(*data))) == NULL) return -1; memset(data, 0, sizeof(*data)); if (python_init() != 0) { free(data); return -1; } /* * If the configuration parameters can't be parsed, then * fail. */ if (cf_section_parse(conf, data, module_config) < 0) { free(data); return -1; } #define A(x) if (python_load_function(&data->x) < 0) goto failed A(instantiate); A(authenticate); A(authorize); A(preacct); A(accounting); A(checksimul); A(pre_proxy); A(post_proxy); A(post_auth); #ifdef WITH_COA A(recv_coa); A(send_coa); #endif A(detach); #undef A *instance = data; /* * Call the instantiate function. No request. Use the * return value. */ return python_function(NULL, data->instantiate.function, "instantiate"); failed: python_error(); python_instance_clear(data); free(data); return -1; }
PyObject* init(PyObject* self, PyObject* args, PyObject *kwargs) { if (!PyArg_ParseTuple(args, "s", &ini_filename)) { PyErr_SetString(PyExc_ValueError, "Parameter must be string containing path to XML configuration file"); return NULL; } int ret = python_init(ini_filename); if (ret == 0) { bInitialized = 1; return Py_BuildValue("i", 0); } return NULL; }
int main (int argc, char **argv) { signal (SIGSEGV, sig_segv_handler); signal (SIGABRT, sig_abrt_handler); log_level = 10; args_parse (argc, argv); printf ( "Telegram-client version " TG_VERSION ", Copyright (C) 2013 Vitaly Valtman\n" "Telegram-client comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.\n" "This is free software, and you are welcome to redistribute it\n" "under certain conditions; type `show_license' for details.\n" ); running_for_first_time (); parse_config (); get_terminal_attributes (); #ifdef USE_LUA if (lua_file) { lua_init (lua_file); } #endif #ifdef USE_PYTHON if (python_file) { python_init(python_file); } #endif inner_main (); #ifdef USE_PYTHON if (python_file) { python_finalize(); } #endif return 0; }
static bool pythoncore_module_init(KviModule *) { #ifdef COMPILE_PYTHON_SUPPORT // Initialize the Python interpreter Py_Initialize(); PyEval_InitThreads(); // save a pointer to the main PyThreadState object mainThreadState = PyThreadState_Get(); // release the lock PyEval_ReleaseLock(); // Initialize the Python module for KVIrc python_init(); g_pInterpreters = new KviPointerHashTable<QString,KviPythonInterpreter>(17,false); g_pInterpreters->setAutoDelete(false); return true; #endif // COMPILE_PYTHON_SUPPORT return false; }
static gboolean glade_python_setup () { GString *command; const gchar *module_path; const GList *paths; Py_SetProgramName (PY_STRING (PACKAGE_NAME)); /* Initialize the Python interpreter */ python_init (); /* Check and init pygobject */ PyErr_Clear (); glade_python_init_pygobject_check (PYGOBJECT_REQUIRED_MAJOR, PYGOBJECT_REQUIRED_MINOR, PYGOBJECT_REQUIRED_MICRO); if (PyErr_Occurred ()) { PyObject *ptype, *pvalue, *ptraceback, *pstr; char *pvalue_char = ""; PyErr_Fetch (&ptype, &pvalue, &ptraceback); PyErr_NormalizeException (&ptype, &pvalue, &ptraceback); if ((pstr = PyObject_Str (pvalue))) pvalue_char = STRING_FROM_PYSTR (pstr); g_warning ("Unable to load pygobject module >= %d.%d.%d, " "please make sure it is in python's path (sys.path). " "(use PYTHONPATH env variable to specify non default paths)\n%s", PYGOBJECT_REQUIRED_MAJOR, PYGOBJECT_REQUIRED_MINOR, PYGOBJECT_REQUIRED_MICRO, pvalue_char); Py_DecRef (ptype); Py_DecRef (pvalue); Py_DecRef (ptraceback); Py_DecRef (pstr); PyErr_Clear (); Py_Finalize (); return TRUE; } pyg_disable_warning_redirections (); /* Generate system path array */ command = g_string_new ("import sys; sys.path+=["); /* GLADE_ENV_MODULE_PATH has priority */ module_path = g_getenv (GLADE_ENV_MODULE_PATH); if (module_path) g_string_append_printf (command, "'%s', ", module_path); /* Append modules directory */ g_string_append_printf (command, "'%s'", glade_app_get_modules_dir ()); /* Append extra paths (declared in the Preferences) */ for (paths = glade_catalog_get_extra_paths (); paths; paths = g_list_next (paths)) g_string_append_printf (command, ", '%s'", (gchar *) paths->data); /* Close python statement */ g_string_append (command, "];\n"); /* Make sure we load Gtk 3 */ g_string_append (command, "import gi; gi.require_version('Gtk', '3.0');\n"); /* Finally run statement in vm */ PyRun_SimpleString (command->str); g_string_free (command, TRUE); return FALSE; }
scoped_test_env() : test_root(random_env_path()) { python_init(test_root); }