[pyicu-dev] Memory Leak?

David Bolen db3l at fitlinxx.com
Fri Mar 10 17:15:51 PST 2006


(from my last response)

> About the only thing that I would find nice is if PyICU could continue
> to support building for Python 2.3, since a number of our systems are
> still using it.  (...)

As it turns out, this isn't necessarily too terrible.  I hadn't looked
closely at the code, but just assumed that it was using the CAPI entry
point it was querying (which don't exist until 2.4), but it isn't.

I've attached a small patch that seems to work for me.  It's clearly a
kludge on the datetime front, but since Python 2.3 had the datetime
type structure private, I can't think of any other good way to detect
the type except by its name, and since the code only activates below
Python 2.4, and the 2.3 names are unlikely to change, it feels like a
reasonable compromise to permit datetime input parameters.

Originally I couldn't figure out why the PyDateTime_CAPI was being
imported since it's never used, but then found out that Python 2.4
crashes without it.  I had first replaced it with a straight module
import for 2.3, but then determined that even that wasn't necessary 
in the 2.3 case, so I just kept it in solely for 2.4+.

-- David

Index: common.cpp
===================================================================
--- common.cpp	(revision 53)
+++ common.cpp	(working copy)
@@ -277,18 +277,38 @@
 }
 
 
+#if PY_VERSION_HEX < 0x02040000
+    /* Replace some _CheckExact macros for Python < 2.4 since the actual
+     * datetime types are private until then.  This is ugly, but allows
+     * support for datetime objects in Python 2.3.
+     */
+    #include <string.h>
+
+    #undef PyDateTime_CheckExact
+    #define PyDateTime_CheckExact(op) \
+       (!strcmp((op)->ob_type->tp_name, "datetime.datetime"))
+
+    #undef PyDelta_CheckExact
+    #define PyDelta_CheckExact(op) \
+       (!strcmp((op)->ob_type->tp_name, "datetime.timedelta"))
+#endif
+
+
 EXPORT UDate PyObject_AsUDate(PyObject *object)
 {
     if (PyFloat_CheckExact(object))
         return (UDate) (PyFloat_AsDouble(object) * 1000.0);
     else
     {
+#if PY_VERSION_HEX > 0x02040000
         static PyDateTime_CAPI *PyDateTimeAPI = NULL;
-        static PyObject *mktime = NULL;
 
         if (PyDateTimeAPI == NULL)
             PyDateTimeAPI = (PyDateTime_CAPI *)
                 PyCObject_Import("datetime", "datetime_CAPI");
+#endif
+        
+        static PyObject *mktime = NULL;
         if (mktime == NULL)
         {
             PyObject *time = PyImport_ImportModule("time");


More information about the pyicu-dev mailing list