[Xapian-discuss] Python bindings...

Olly Betts olly at survex.com
Sat Dec 10 08:05:53 GMT 2005


On Thu, Sep 01, 2005 at 11:20:35AM +0100, James Aylett wrote:
> On Wed, Aug 31, 2005 at 07:40:01PM +0100, Olly Betts wrote:
> 
> > I was hoping James would have a chance to look at it, since he seems to
> > understand the swig python stuff best.
> > 
> > James: If you're too busy, or had a look and didn't get anywhere, let me
> > kmow abnd I'll see what I can do...
> 
> Previously I was usually able to get this sort of thing working with a
> bit of judicious juggling of things, but I'm having difficulty this
> time.

I've just have another long argument with SWIG, but this time I won!  

I can now run queries with simplesearch.py.

The secret seems to be that the typecheck typemap needs a precedence
parameter to actually work (I set it to 500, somewhat arbitrarily).
You mentioned there was a built-in one, so I suspect the problem is
that ours was being overridden by it.  I also found out how to eliminate
the new/delete pair which avoids a memory leak if an exception is
thrown.

Patch against 0.9.2 attached.

I've not checked this in yet - I want to add a regression test (since
it seems this used to work but stopped due to changes in SWIG, and we
definitely want to know if this stops working again) and also make sure
that the related vector<Query> typemap works too.

Cheers,
    Olly
-------------- next part --------------
Index: python/util.i
===================================================================
--- python/util.i	(.../svn+userv:///xapian/tags/0.9.2/xapian-bindings)	(revision 6401)
+++ python/util.i	(working copy)
@@ -140,8 +127,3 @@
 
-%typemap(python, freearg) const vector<string>* {
-    delete $1;
-}
-
-%typemap(python, typecheck) const std::vector<std::string>* {
-    $1 = 1;
+%typemap(python, typecheck, precedence=500) vector<string> & {
     if (!PyList_Check($input)) {
@@ -149,2 +131,3 @@
     } else {
+	$1 = 1;
 	int numitems = PyList_Size($input);
@@ -160,23 +143,20 @@
 
-%typemap(python, in) const std::vector<std::string>* {
+%typemap(python, in) vector<string> & (vector<string> v) {
     if (!PyList_Check($input)) {
-        PyErr_SetString(PyExc_TypeError, "expected list");
-        return NULL;
+	PyErr_SetString(PyExc_TypeError, "expected list");
+	return NULL;
     }
     int numitems = PyList_Size($input);
-    vector<string> *v = new vector<string>();
     v.reserve(numitems);
     for (int i=0; i<numitems; ++i) {
-        PyObject *obj = PyList_GetItem($input, i);
-	if (PyString_Check(obj)) {
-	    int len = PyString_Size(obj);
-	    char *err = PyString_AsString(obj);
-	    v->push_back(string(err, len));
-	} else {
-	    PyErr_SetString(PyExc_TypeError,
-			    "expected list of strings");
+	PyObject *obj = PyList_GetItem($input, i);
+	if (!PyString_Check(obj)) {
+	    PyErr_SetString(PyExc_TypeError, "expected list of strings");
 	    return NULL;
 	}
+	int len = PyString_Size(obj);
+	const char *err = PyString_AsString(obj);
+	v.push_back(string(err, len));
     }
-    $1 = v;
+    $1 = &v;
 }
Index: xapian.i
===================================================================
--- xapian.i	(.../svn+userv:///xapian/tags/0.9.2/xapian-bindings)	(revision 6390)
+++ xapian.i	(working copy)
@@ -787,6 +787,4 @@
 	     *  specified operator */
-	    Query(Query::op op, vector<string> * subqs,
-		  termcount parameter = 0) {
-		return new Xapian::Query(op, subqs->begin(), subqs->end(),
-					 parameter);
+	    Query(Query::op op, vector<string> & subqs, termcount param = 0) {
+		return new Xapian::Query(op, subqs.begin(), subqs.end(), param);
 	    }


More information about the Xapian-discuss mailing list