diff options
author | Juan Pablo Ugarte <juanpablougarte@gmail.com> | 2015-06-10 21:08:33 -0300 |
---|---|---|
committer | Juan Pablo Ugarte <juanpablougarte@gmail.com> | 2015-06-10 21:08:33 -0300 |
commit | 7bd059da4292281883f181b76809dc3b40d30706 (patch) | |
tree | 3985c45153c681e967034c727f0bd7786a98f520 /plugins/python | |
parent | f7a6f46863d1be478d56340489579fb2a5f8ff9b (diff) | |
download | glade-7bd059da4292281883f181b76809dc3b40d30706.tar.gz |
plugins/python/glade-python.c: add extra catalog paths to python path.
General cleanup to reduce Python 3 specific code.
Diffstat (limited to 'plugins/python')
-rw-r--r-- | plugins/python/glade-python.c | 115 |
1 files changed, 52 insertions, 63 deletions
diff --git a/plugins/python/glade-python.c b/plugins/python/glade-python.c index 4ba8ac78..41116838 100644 --- a/plugins/python/glade-python.c +++ b/plugins/python/glade-python.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2011 Juan Pablo Ugarte. + * Copyright (C) 2006-2015 Juan Pablo Ugarte. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as @@ -27,47 +27,32 @@ #include <gladeui/glade.h> #if PY_MAJOR_VERSION >= 3 -#define IS_PY3K -#define PY_CHARTYPE wchar_t -wchar_t *char_to_wchar(char *ch) { - wchar_t *res = NULL; - PyObject *pydecoded = PyUnicode_FromString(ch); - - if (pydecoded) { - res = PyUnicode_AsWideCharString(pydecoded, NULL); - Py_DECREF(pydecoded); - } - return res; -} + +#define WCHAR(str) L##str + +#define PY_STRING(str) WCHAR(str) +#define STRING_FROM_PYSTR(pstr) PyUnicode_AsUTF8 (pstr) + #else + /* include bytesobject.h to map PyBytes_* to PyString_* */ #include <bytesobject.h> -#define PY_CHARTYPE char + +#define PY_STRING(str) str +#define STRING_FROM_PYSTR(pstr) PyBytes_AsString (pstr) + #endif static void python_init (void) { - const gchar *argv = g_get_prgname (); -#ifdef IS_PY3K - wchar_t *argv_wide = NULL; -#endif - if (Py_IsInitialized ()) return; Py_InitializeEx (0); -#ifdef IS_PY3K - argv_wide = char_to_wchar((char *) argv); - if (!argv_wide) { - argv_wide = L""; - } - PySys_SetArgv (1, (wchar_t **) &argv_wide); -#else - PySys_SetArgv (1, (char **) &argv); -#endif - + /* if argc is 0 an empty string is prepended to sys.path */ + PySys_SetArgvEx (0, NULL, 0); } static void @@ -107,17 +92,11 @@ glade_python_init_pygobject_check (gint req_major, gint req_minor, gint req_micr static gboolean glade_python_setup () { - gchar *command; + GString *command; const gchar *module_path; -#ifdef IS_PY3K - wchar_t *pkg_name = char_to_wchar((char *) PACKAGE_NAME); - if (!pkg_name) { - pkg_name = L"glade"; - } - Py_SetProgramName (pkg_name); -#else - Py_SetProgramName ((char *) PACKAGE_NAME); -#endif + const GList *paths; + + Py_SetProgramName (PY_STRING (PACKAGE_NAME)); /* Initialize the Python interpreter */ python_init (); @@ -130,25 +109,25 @@ glade_python_setup () PYGOBJECT_REQUIRED_MICRO); if (PyErr_Occurred ()) { - PyObject *ptype, *pvalue, *ptraceback, *pvalue_repr; - char *pvalue_char; + PyObject *ptype, *pvalue, *ptraceback, *pstr; + char *pvalue_char = ""; PyErr_Fetch (&ptype, &pvalue, &ptraceback); - pvalue_repr = PyObject_Str(pvalue); - if (!pvalue_repr) { - pvalue_char = "ERROR: Unable to get Python error data.\n"; - } else { -#ifdef IS_PY3K - pvalue_char = PyUnicode_AsUTF8(pvalue_repr); -#else - pvalue_char = PyBytes_AsString(pvalue_repr); -#endif - } + 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 (); @@ -157,24 +136,34 @@ glade_python_setup () pyg_disable_warning_redirections (); - /* Set path */ + /* 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 == NULL) - command = g_strdup_printf ("import sys; sys.path+=['%s'];\n", - glade_app_get_modules_dir ()); - else - command = g_strdup_printf ("import sys; sys.path+=['%s', '%s'];\n", - module_path, - glade_app_get_modules_dir ()); - - PyRun_SimpleString (command); - g_free (command); + 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"); + + /* Finally run statement in vm */ + PyRun_SimpleString (command->str); + + g_string_free (command, TRUE); + return FALSE; } void -glade_python_init (const gchar * name) +glade_python_init (const gchar *name) { static gsize init = 0; gchar *import_sentence; |