[redland-dev] Using Python file objects

John C Barstow jbowtie at amathaine.com
Wed Sep 14 00:27:53 BST 2005


I've been working with the Python bindings quite a lot and one of the 
frustrations I have is that I can't pass file-like objects to the 
Redland API.  Enough so that I've taken a look at the problem.

SWIG 1.1 (and 1.3) allow Python objects to be mapped to FILE* pointers. 
The typemap looks like this:

%typemap (python, in) FILE* {
  if(!PyFile_Check($source)) {
    PyErr_SetString(PyExc_TypeError, "Need a file!");
    return NULL;
  }
  $target = PyFile_AsFile($source);
}

So all we really need to do is to expose an API (in the parser and the 
serializer, last I looked) that takes a FILE* as a parameter. Really, 
there's no reason we couldn't refactor the existing code to delegate to 
such a function - the existing code ultimately opens a file anyway.

Doing this would open up a lot of good opportunties in Python - 
including Gnome-VFS and WebDAV support, without having to make any 
substantial changes or introduce new dependencies.

Given the limited scope of the changes, I think this would be an easy 
win. Presumably other such typemaps exist (or could be built following a 
similar model) for other bindings as well.  Unfortunately I'm not well 
placed to actually produce a patch at the moment; however will look at 
doing so in the not very distant future if someone doesn't beat me to 
it.

-- 
John C Barstow



More information about the redland-dev mailing list