[redland-dev] Using Python file objects

Dave Beckett dave.beckett at bristol.ac.uk
Wed Sep 14 11:58:05 BST 2005


On Wed, 2005-09-14 at 11:28 +0100, Dave Beckett wrote:
> The simplest code is like this:

I was wrong, there is simpler code as there is an API call:
  librdf_serializer_serialize_model_to_file_handle(librdf_serializer*
serializer, FILE *handle, librdf_uri* base_uri, librdf_model* model);

so you can write direct to a FILE* however as I said previously about
FILE* being hard to export between bindings, it's not exposed in the
public SWIG file - although you can add it yourself easily.  That makes
writing a helper function in the python SWIG to serialize a model to a
FILE* rather easier.  

Something like the following although the calling and return signature
would have to change if it was written as a native python method rather
than one that SWIG wrapped for you.

int
librdf_python_serialize_model_to_pyfile(librdf_serializer* serializer,
PyObject *source, librdf_uri* base_uri, librdf_model* model)
{
  FILE* handle;

  if(!PyFile_Check(source)) {
    PyErr_SetString(PyExc_TypeError, "Need a file!");
    return 1;
  }
  handle=PyFile_AsFile(source);
  return librdf_serializer_serialize_model_to_file_handle(serializer,
     handle, base_uri, model);
}

Dave




More information about the redland-dev mailing list