Commit 66c6d21d authored by Bohuslav "Slavek" Kabrda's avatar Bohuslav "Slavek" Kabrda Committed by Juan Pablo Ugarte

plugins/python/glade-python.c: add support for Python 3.

parent dfed45a7
......@@ -43,13 +43,17 @@ AC_DEFUN([AM_CHECK_PYTHON_HEADERS],
[AC_REQUIRE([AM_PATH_PYTHON])
AC_MSG_CHECKING(for headers required to compile python extensions)
dnl deduce PYTHON_INCLUDES
py_prefix=`$PYTHON -c "import sys; print sys.prefix"`
py_exec_prefix=`$PYTHON -c "import sys; print sys.exec_prefix"`
am_python_print_includes="\
from distutils import sysconfig as sc
incldirs = [[]]
for d in sc.get_config_var('INCLDIRSTOMAKE').split():
incldirs.append('-I' + d)
print(' '.join(incldirs))
"
if test "x$PYTHON_INCLUDES" == x; then
PYTHON_INCLUDES="-I${py_prefix}/include/python${PYTHON_VERSION}"
if test "$py_prefix" != "$py_exec_prefix"; then
PYTHON_INCLUDES="$PYTHON_INCLUDES -I${py_exec_prefix}/include/python${PYTHON_VERSION}"
fi
PYTHON_INCLUDES=`$PYTHON -c "$am_python_print_includes"`
fi
AC_SUBST(PYTHON_INCLUDES)
dnl check if the headers exist:
......@@ -70,12 +74,19 @@ AC_DEFUN([AM_CHECK_PYTHON_LIBS],
[AC_REQUIRE([AM_CHECK_PYTHON_HEADERS])
AC_MSG_CHECKING(for libraries required to embed python)
dnl deduce PYTHON_LIBS
py_exec_prefix=`$PYTHON -c "import sys; print sys.exec_prefix"`
am_python_print_libs="\
from distutils import sysconfig as sc
libs = '-L' + sc.get_config_var('LIBDIR').strip()
libs += ' ' + sc.get_config_var('BLDLIBRARY')
print(libs)"
am_python_print_lib_loc="\
from distutils import sysconfig as sc
print(sc.get_config_var('LIBDIR'))"
if test "x$PYTHON_LIBS" == x; then
PYTHON_LIBS="-L${py_prefix}/lib -lpython${PYTHON_VERSION}"
PYTHON_LIBS=`$PYTHON -c "$am_python_print_libs"`
fi
if test "x$PYTHON_LIB_LOC" == x; then
PYTHON_LIB_LOC="${py_prefix}/lib"
PYTHON_LIB_LOC=`$PYTHON -c "$am_python_print_lib_loc"`
fi
AC_SUBST(PYTHON_LIBS)
AC_SUBST(PYTHON_LIB_LOC)
......
......@@ -26,17 +26,48 @@
#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;
}
#else
/* include bytesobject.h to map PyBytes_* to PyString_* */
#include <bytesobject.h>
#define PY_CHARTYPE char
#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
}
static void
......@@ -78,8 +109,15 @@ glade_python_setup ()
{
gchar *command;
const gchar *module_path;
Py_SetProgramName (PACKAGE_NAME);
#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
/* Initialize the Python interpreter */
python_init ();
......@@ -92,13 +130,25 @@ glade_python_setup ()
PYGOBJECT_REQUIRED_MICRO);
if (PyErr_Occurred ())
{
PyObject *ptype, *pvalue, *ptraceback;
PyObject *ptype, *pvalue, *ptraceback, *pvalue_repr;
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
}
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, PyString_AsString (pvalue));
PYGOBJECT_REQUIRED_MICRO, pvalue_char);
PyErr_Clear ();
Py_Finalize ();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment