[glom/glom-1-18] Initialize pygobject, to fix the use of the PyRecord API.



commit f50b191643b894de5af1e5b8ba56206e78f90fad
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Apr 7 13:46:22 2011 +0200

    Initialize pygobject, to fix the use of the PyRecord API.
    
    * configure.ac: Require the latest pygobject with gi.repository support.
    * glom/libglom/init.cc: Include pygobject.h so we can call pygobject_init()
    here.
    * glom/libglom/python_embed/py_glom_record.cc: Define NO_IMPORT_PYGOBJECT
    so we can still include pygobject.h again here. pygobject.h uses a nasty
    variable declared in the header.
    * tests/python/test_python_execute_func_with_record.cc: Fix the python
    example code.
    However, this shows a problem with getting bool results from python,
    apparently because boost::python::extract<>::check() checks that the type
    is compatible rather than checking that it's the exact type.

 ChangeLog                                          |   16 ++++++++++++++++
 glom/libglom/init.cc                               |    6 ++++++
 glom/libglom/python_embed/py_glom_record.cc        |   16 ++++++++++++----
 .../libglom/python_embed/pygdavalue_conversions.cc |    2 ++
 4 files changed, 36 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index e19ad40..244d3e0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,21 @@
 2011-04-07  Murray Cumming  <murrayc murrayc com>
 
+	Initialize pygobject, to fix the use of the PyRecord API.
+
+	* configure.ac: Require the latest pygobject with gi.repository support.
+	* glom/libglom/init.cc: Include pygobject.h so we can call pygobject_init()
+	here.
+	* glom/libglom/python_embed/py_glom_record.cc: Define NO_IMPORT_PYGOBJECT
+	so we can still include pygobject.h again here. pygobject.h uses a nasty
+	variable declared in the header.
+	* tests/python/test_python_execute_func_with_record.cc: Fix the python
+	example code.
+	However, this shows a problem with getting bool results from python,
+	apparently because boost::python::extract<>::check() checks that the type
+	is compatible rather than checking that it's the exact type.
+
+2011-04-07  Murray Cumming  <murrayc murrayc com>
+
 	Moved the python tests to a sub-directory.
 
 	* tests/test_load_python_library.cc:
diff --git a/glom/libglom/init.cc b/glom/libglom/init.cc
index 4275dba..5399165 100644
--- a/glom/libglom/init.cc
+++ b/glom/libglom/init.cc
@@ -28,6 +28,8 @@
 #include <libgdamm.h>
 #include <iostream>
 
+#include <pygobject.h>
+
 //TODO: Remove this redefine when Python fixes the compiler error in their macro:
 // http://bugs.python.org/issue7463
 // Note that this sets a local copy of PyDateTimeAPI (in Python's datetime.h
@@ -61,6 +63,10 @@ void libglom_init()
     //See https://bugzilla.gnome.org/show_bug.cgi?id=644702
     PyErr_Print();
   }
+
+  //Initialize PyGObject, so that functions such as pygobject_new() work
+  //instead of crashing.
+  pygobject_init(2, 28, 0);
 }
 
 void libglom_deinit()
diff --git a/glom/libglom/python_embed/py_glom_record.cc b/glom/libglom/python_embed/py_glom_record.cc
index 6f4e6f0..7a5a9c1 100644
--- a/glom/libglom/python_embed/py_glom_record.cc
+++ b/glom/libglom/python_embed/py_glom_record.cc
@@ -21,7 +21,7 @@
 //We need to include this before anything else, to avoid redefinitions:
 //#include <Python.h>
 
-//#define NO_IMPORT_PYGOBJECT //To avoid a multiple definition in pygtk.
+#define NO_IMPORT_PYGOBJECT //To avoid a multiple definition in pygtk.
 #include <pygobject.h> //For the PyGObject and PyGBoxed struct definitions.
 
 #include <libglom/python_embed/py_glom_record.h>
@@ -64,10 +64,18 @@ boost::python::object PyGlomRecord::get_connection()
 
   if(m_connection)
   {
+
     //Ask pygobject to create a PyObject* that wraps our GObject,
     //presumably using something from pygda:
+    if(!_PyGObject_API)
+    {
+      std::cerr << "pyggobject does not seem to be initialized properly." << std::endl;
+      return result;
+    }
+
     PyObject* cobject = pygobject_new( G_OBJECT(m_connection->gobj()) );
-    result = boost::python::object( boost::python::borrowed(cobject) );
+    if(cobject)
+      result = boost::python::object( boost::python::borrowed(cobject) );
   }
 
   return result;
@@ -180,7 +188,7 @@ void PyGlomRecord::setitem(const boost::python::object& key, const boost::python
   params->add_holder(field->get_holder(field_value));
   params->add_holder(m_key_field->get_holder(m_key_field_value));
 
-  Glib::RefPtr<Gnome::Gda::SqlBuilder> builder = 
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> builder =
     Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_UPDATE);
   builder->set_table(m_table_name);
   builder->add_field_value_as_value(field->get_name(), field_value);
@@ -188,7 +196,7 @@ void PyGlomRecord::setitem(const boost::python::object& key, const boost::python
     builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
       builder->add_field_id(m_key_field->get_name(), m_table_name),
       builder->add_expr(m_key_field_value)));
- 
+
   bool updated = false;
   try
   {
diff --git a/glom/libglom/python_embed/pygdavalue_conversions.cc b/glom/libglom/python_embed/pygdavalue_conversions.cc
index d8dd0d1..93b1730 100644
--- a/glom/libglom/python_embed/pygdavalue_conversions.cc
+++ b/glom/libglom/python_embed/pygdavalue_conversions.cc
@@ -41,6 +41,8 @@ glom_pygda_value_from_pyobject(GValue* boxed, const boost::python::object& input
       return true;
     }
 
+    //TODO: This also succeeds if the Python type is a bool,
+    //but we don't want to do that.
     boost::python::extract<int> extractor_int(input);
     if(extractor_int.check())
     {



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