Commit 6fd32021 authored by Murray Cumming's avatar Murray Cumming
Browse files

Fix the build (and tests) with Python 3

* configure.ac: Added some hints in comments.
* glom/libglom/python_embed/pygdavalue_conversions.cc:
  Do not use PyInt_Check if building with python 3,
  because python 3 does not seem to have integer types.
  Use PyUnicode_Check() instead of PyString_Check().
  This seems to be appropriate for python 2 too.
* glom/main.cc: Do not call PySys_SetArgv() if building
  with python 3 because we would need to massage the
  arguments into wchar*, and we do not know why we call
  it anyway.
* glom/python_embed/glom_python.cc: Do not call
  Py_FlushLine() if building with python 3, because it
  does not exist in python 3 and is apparently
  unnecessary.
* glom/test_pyembed.cc: USe PyUnicode_Check() instead
  of PyString_Check(). See above.
  Again, do not call Py_FlushLine() with python 3.
* glom/test_pyembed_singleline.cc: Do not call
  PyFlushLine() with python 3.

To build for Python 3, you'll need to do something like this:
export PYTHON=python3.3
  (do this before starting jhbuild, if you are using jhbuild).
configure --with-boost-python=boost_python-mt-py33
  (or autogen.sh if building from git.)
You will need to have built pygobject with Python3 support.
parent 3d04eaa0
......@@ -225,15 +225,19 @@ AC_CHECK_FUNCS([strptime])
AM_PATH_PYTHON
# Get the compiler and linker flags for embedding Python.
# To specify a particular python version set an environment variable.
# For instance: PYTHON=python3.3
# To specify a particular python version you must set an environment variable.
# For instance:
# export PYTHON=python3.3
# When using jhbuild, note that you will need to do that before starting jhbuild,
# so that jhbuild sets PYTHONPATH correctly.
#
# See http://www.gnu.org/software/autoconf-archive/ax_python_devel.html
# TODO: Use AX_PYTHON_DEVEL(>= '3.0.0') when we can make the build use the python3 version of boost-python by default.
AX_PYTHON_DEVEL
# Get the CFLAGS and LIBS for boost::python.
# To use a non-default Boost::Python library, for instance to use
# the version for Python3, if you already have set PYTHON=python3.3,
# the version for Python3, if you already have set, for instance, PYTHON=python3.3,
# you'll need to pass something like this to configure to make it
# link to the correct library (Yes, this is tedious).
# --with-boost-python=boost_python-mt-py33
......
......@@ -48,6 +48,8 @@ glom_pygda_value_from_pyobject(GValue* boxed, const boost::python::object& input
//We check for bool first,
//because bool is derived from int in Python,
//so PyInt_Check() would also succeed on a bool object.
// This comment probably only applies to Python 2,
// because Python 3 doesn't seem to have an integer type any more.
if(PyBool_Check(input_c))
{
boost::python::extract<bool> extractor_bool(input);
......@@ -59,7 +61,9 @@ glom_pygda_value_from_pyobject(GValue* boxed, const boost::python::object& input
return true;
}
}
#if PY_MAJOR_VERSION < 3
//Python 3 doesn't seem to have an Integer type.
if(PyInt_Check(input_c))
{
boost::python::extract<int> extractor_int(input);
......@@ -71,6 +75,7 @@ glom_pygda_value_from_pyobject(GValue* boxed, const boost::python::object& input
return true;
}
}
#endif
if(PyLong_Check(input_c))
{
......@@ -96,7 +101,7 @@ glom_pygda_value_from_pyobject(GValue* boxed, const boost::python::object& input
}
}
if(PyString_Check(input_c))
if(PyUnicode_Check(input_c))
{
boost::python::extract<std::string> extractor_string(input);
if(extractor_string.check())
......
......@@ -462,7 +462,11 @@ main(int argc, char* argv[])
Glom::libglom_init(); //Also initializes python.
//We use python for calculated-fields:
#if PY_MAJOR_VERSION < 3
//Python 3 uses wchar* so we can't just pass argv.
//TODO: Find out why we would want to do this anyway.
PySys_SetArgv(argc, argv);
#endif
try
{
......
......@@ -356,7 +356,10 @@ static boost::python::object glom_python_call(Field::glom_field_type result_type
}
//TODO: Why do we do this?
Py_FlushLine();
#if PY_MAJOR_VERSION < 3
//There is no Py_FlushLine in Python 3
//Py_FlushLine();
#endif
PyErr_Clear();
//We did this in main(): Py_Finalize();
......
......@@ -73,13 +73,16 @@ void evaluate_function_implementation(const Glib::ustring& func_impl)
PyObject* pyStringObject = PyObject_Str(pyValue);
if(pyStringObject)
{
if(PyString_Check(pyStringObject))
if(PyUnicode_Check(pyStringObject))
{
const char* pchResult = PyString_AsString(pyStringObject);
PyObject* pyStr = PyUnicode_AsEncodedString(pyStringObject, "utf-8", "Error ~");
const char* pchResult = PyBytes_AS_STRING(pyStr);
if(pchResult)
g_warning("result is %s", pchResult);
else
g_warning("pchResult is null");
Py_DECREF(pyStr);
}
else
g_warning("PyString_Check returned false");
......@@ -91,7 +94,10 @@ void evaluate_function_implementation(const Glib::ustring& func_impl)
}
}
Py_FlushLine();
#if PY_MAJOR_VERSION < 3
//There is no Py_FlushLine in Python 3
//Py_FlushLine();
#endif
PyErr_Clear();
......
......@@ -39,7 +39,10 @@ void evaluate_function_implementation(const Glib::ustring& func_impl)
Py_DECREF(pyValue);
}
Py_FlushLine();
#if PY_MAJOR_VERSION < 3
//There is no Py_FlushLine in Python 3
//Py_FlushLine();
#endif
PyErr_Clear();
......
Supports Markdown
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