[glom] Fix the build (and tests) with Python 3



commit 6fd320216257280272298315475fc9fcd832e59c
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Sep 12 12:55:59 2013 +0200

    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.

 configure.ac                                       |   10 +++++++---
 .../libglom/python_embed/pygdavalue_conversions.cc |    9 +++++++--
 glom/main.cc                                       |    4 ++++
 glom/python_embed/glom_python.cc                   |    5 ++++-
 glom/test_pyembed.cc                               |   12 +++++++++---
 glom/test_pyembed_singleline.cc                    |    5 ++++-
 6 files changed, 35 insertions(+), 10 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 30cc2c5..f52776e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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
diff --git a/glom/libglom/python_embed/pygdavalue_conversions.cc 
b/glom/libglom/python_embed/pygdavalue_conversions.cc
index 2edde69..ec97eb2 100644
--- a/glom/libglom/python_embed/pygdavalue_conversions.cc
+++ b/glom/libglom/python_embed/pygdavalue_conversions.cc
@@ -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())
diff --git a/glom/main.cc b/glom/main.cc
index 658a1b4..407ceb3 100644
--- a/glom/main.cc
+++ b/glom/main.cc
@@ -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
   {
diff --git a/glom/python_embed/glom_python.cc b/glom/python_embed/glom_python.cc
index cdfecd1..a8f470f 100644
--- a/glom/python_embed/glom_python.cc
+++ b/glom/python_embed/glom_python.cc
@@ -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();
diff --git a/glom/test_pyembed.cc b/glom/test_pyembed.cc
index f2b323e..7c6d6b8 100644
--- a/glom/test_pyembed.cc
+++ b/glom/test_pyembed.cc
@@ -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();
 
 
diff --git a/glom/test_pyembed_singleline.cc b/glom/test_pyembed_singleline.cc
index 35dd7e2..bcda06e 100644
--- a/glom/test_pyembed_singleline.cc
+++ b/glom/test_pyembed_singleline.cc
@@ -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();
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]