[glom] Python field calculation: Fix a crash.



commit 2c15d6b2af5208d3c2b1f56dd229853fbc204237
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Apr 15 21:18:13 2010 +0200

    Python field calculation: Fix a crash.
    
    	* glom/libglom/python_embed/pygdavalue_conversions.cc:
    	glom_pygda_value_as_boost_pyobject(): Add a PyDateTimeAPI call, as already
    	done in glom_pygda_value_from_pyobject(), to prevent a crash (and valgrind
    	warning about 0 dereference) when using this (silly, wrong) field calculation,
    	though I can't reproduce it in a unit test:
    	import datetime
      return (datetime.date.today() - record["date_of_birth"]).days / 365

 ChangeLog                                          |   12 ++++++++++++
 .../libglom/python_embed/pygdavalue_conversions.cc |   17 +++++++++++++++++
 2 files changed, 29 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index f54846f..aa53595 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2010-04-15  Murray Cumming  <murrayc murrayc com>
 
+  Python field calculation: Fix a crash.
+  
+	* glom/libglom/python_embed/pygdavalue_conversions.cc: 
+	glom_pygda_value_as_boost_pyobject(): Add a PyDateTimeAPI call, as already 
+	done in glom_pygda_value_from_pyobject(), to prevent a crash (and valgrind 
+	warning about 0 dereference) when using this (silly, wrong) field calculation, 
+	though I can't reproduce it in a unit test:
+	import datetime
+  return (datetime.date.today() - record["date_of_birth"]).days / 365
+
+2010-04-15  Murray Cumming  <murrayc murrayc com>
+
   Python module: Improve API documentation.
   
 	* glom/python_embed/python_module/py_glom_module.cc: Added some options and 
diff --git a/glom/libglom/python_embed/pygdavalue_conversions.cc b/glom/libglom/python_embed/pygdavalue_conversions.cc
index 9078fa4..4368ea9 100644
--- a/glom/libglom/python_embed/pygdavalue_conversions.cc
+++ b/glom/libglom/python_embed/pygdavalue_conversions.cc
@@ -133,6 +133,23 @@ boost::python::object glom_pygda_value_as_boost_pyobject(const Glib::ValueBase&
     const GType value_type = G_VALUE_TYPE(boxed);
     boost::python::object ret;
 
+#if PY_VERSION_HEX >= 0x02040000
+    if((value_type == G_TYPE_DATE) || 
+       (value_type == GDA_TYPE_TIME) ||
+       (value_type == GDA_TYPE_TIMESTAMP))
+    {
+      // We shouldn't need to call PyDateTime_IMPORT again,
+      // after already doing it in libglom_init(),
+      // but PyDate_FromDate() crashes (with valgrind warnings) if we don't.
+      //
+      // Causes a C++ compiler warning, so we use its definition directly.
+      // See http://bugs.python.org/issue7463.
+      // PyDateTime_IMPORT; //A macro, needed to use PyDate_Check(), PyDateTime_Check(), etc.
+      PyDateTimeAPI = (PyDateTime_CAPI*) PyCObject_Import((char*)"datetime", (char*)"datetime_CAPI");
+      g_assert(PyDateTimeAPI); //This should have been set by the PyDateTime_IMPORT macro
+    }
+#endif
+
     if (value_type == G_TYPE_INT64) {
         ret = boost::python::object(g_value_get_int64(boxed));
     } else if (value_type == G_TYPE_UINT64) {



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