Changeset 11158

Show
Ignore:
Timestamp:
2008-09-03 06:24:47 (3 months ago)
Author:
richard
Message:

python/util.i: Fix memory leaks in query constructor typemaps.
Also, remove old, commented out, code which almost certainly had
similar memory leaks - I don't think we're going to want this
code, but if we do, rewriting it by copying the maintained and
working code for the query constructor is less likely to lead to
subtle errors.

Location:
trunk/xapian-bindings
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/xapian-bindings/ChangeLog

    r11059 r11158  
     1Wed Sep 03 05:22:09 GMT 2008  Richard Boulton <richard@lemurconsulting.com> 
     2 
     3        * python/util.i: Fix memory leaks in query constructor typemaps. 
     4          Also, remove old, commented out, code which almost certainly had 
     5          similar memory leaks - I don't think we're going to want this 
     6          code, but if we do, rewriting it by copying the maintained and 
     7          working code for the query constructor is less likely to lead to 
     8          subtle errors. 
     9 
    110Mon Aug 04 05:06:46 GMT 2008  Olly Betts <olly@survex.com> 
    211 
  • trunk/xapian-bindings/python/util.i

    r10784 r11158  
    5555        PyObject * mythis = PyObject_GetAttrString(obj, "this"); 
    5656#endif 
     57        if (!mythis) 
     58            return 0; 
     59 
    5760        Query * retval = 0; 
    58         if (!mythis || SWIG_ConvertPtr(mythis, (void **)&retval, 
    59                                        SWIGTYPE_p_Xapian__Query, 0) < 0) { 
     61        int res = SWIG_ConvertPtr(mythis, (void **)&retval, 
     62                                  SWIGTYPE_p_Xapian__Query, 0); 
     63        if (!SWIG_IsOK(res)) { 
    6064            retval = 0; 
    6165        } 
     66        Py_DECREF(mythis); 
    6267        return retval; 
    6368    } 
    64  
    65 #if 0 // Currently unused 
    66     RSet *get_py_rset(PyObject *obj) { 
    67         PyObject * mythis = PyObject_GetAttrString(obj, "this"); 
    68         Rset * retval = 0; 
    69         if (!mythis || SWIG_ConvertPtr(mythis, (void **)&retval, 
    70                                        SWIGTYPE_p_Xapian__RSet, 0) < 0) { 
    71             retval = 0; 
    72         } 
    73         return retval; 
    74     } 
    75 #endif 
    76  
    77 #if 0 // FIXME 
    78     MatchDecider *get_py_matchdecider(PyObject *obj) { 
    79         PyObject * mythis = PyObject_GetAttrString(obj, "this"); 
    80         MatchDecider * retval = 0; 
    81         if (!mythis || SWIG_ConvertPtr(mythis, (void **)&retval, 
    82                                        SWIGTYPE_p_Xapian__MatchDecider, 0) < 0) { 
    83             retval = 0; 
    84         } 
    85         return retval; 
    86     } 
    87 #endif 
    88  
    89 #if 0 
    90     int get_py_int(PyObject *obj) { 
    91         if (!PyNumber_Check(obj)) { 
    92             throw PythonProblem(); 
    93         } 
    94         return PyInt_AsLong(PyNumber_Int(obj)); 
    95     } 
    96 #endif 
    9769} 
    9870%} 
     
    10476    } else { 
    10577        $1 = 1; 
    106         int numitems = PySequence_Size($input); 
     78        PyObject * fastseq = PySequence_Fast($input, "expected sequence of strings or queries"); 
     79        if (!fastseq) { 
     80            // We've already checked that we have a sequence, so the failure of 
     81            // PySequence_Fast() is a serious error, not just a failure of 
     82            // typecheck. 
     83            SWIG_fail; 
     84        } 
     85        int numitems = PySequence_Fast_GET_SIZE(fastseq); 
    10786        for (int i = 0; i < numitems; ++i) { 
    108             PyObject *obj = PySequence_GetItem($input, i); 
     87            PyObject *obj = PySequence_Fast_GET_ITEM(fastseq, i); 
    10988            if (!PyUnicode_Check(obj) && 
    11089%#if PY_VERSION_HEX >= 0x03000000 
     
    11897            } 
    11998        } 
     99        Py_DECREF(fastseq); 
    120100    } 
    121101} 
    122102 
    123103%typemap(in) const vector<Xapian::Query> & (vector<Xapian::Query> v) { 
    124     if (!PySequence_Check($input)) { 
    125         PyErr_SetString(PyExc_TypeError, "expected list of strings or queries"); 
    126         return NULL; 
    127     } 
    128  
    129     int numitems = PySequence_Size($input); 
     104    PyObject * fastseq = PySequence_Fast($input, "expected sequence of strings or queries"); 
     105    if (!fastseq) { 
     106        SWIG_fail; 
     107    } 
     108 
     109    int numitems = PySequence_Fast_GET_SIZE(fastseq); 
    130110    v.reserve(numitems); 
    131111    for (int i = 0; i < numitems; ++i) { 
    132         PyObject *obj = PySequence_GetItem($input, i); 
     112        PyObject *obj = PySequence_Fast_GET_ITEM(fastseq, i); 
     113        PyObject *decrefme = NULL; 
    133114        if (PyUnicode_Check(obj)) { 
    134115            PyObject *strobj = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(obj), PyUnicode_GET_SIZE(obj), "ignore"); 
    135116            if (!strobj) SWIG_fail; 
    136             Py_DECREF(obj); 
    137117            obj = strobj; 
     118            decrefme = strobj; 
    138119        } 
    139120%#if PY_VERSION_HEX >= 0x03000000 
     
    157138            if (!subqp) { 
    158139                PyErr_SetString(PyExc_TypeError, "expected string or query"); 
     140                Py_XDECREF(decrefme); 
    159141                SWIG_fail; 
    160142            } 
    161143            v.push_back(*subqp); 
    162144        } 
     145        Py_XDECREF(decrefme); 
    163146    } 
    164147    $1 = &v; 
     148    Py_DECREF(fastseq); 
    165149} 
    166150