[cairo-commit] pycairo/cairo pycairo-surface.c,1.58,1.59

Steve Chaplin commit at pdx.freedesktop.org
Tue Oct 11 20:13:55 PDT 2005


Committed by: stevech1097

Update of /cvs/cairo/pycairo/cairo
In directory gabe:/tmp/cvs-serv21030/cairo

Modified Files:
	pycairo-surface.c 
Log Message:
'SC'

Index: pycairo-surface.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-surface.c,v
retrieving revision 1.58
retrieving revision 1.59
diff -u -d -r1.58 -r1.59
--- pycairo-surface.c	28 Sep 2005 14:35:31 -0000	1.58
+++ pycairo-surface.c	12 Oct 2005 03:13:47 -0000	1.59
@@ -192,20 +192,31 @@
 
 #ifdef CAIRO_HAS_PNG_FUNCTIONS
 static cairo_status_t
-_write_func (void *closure, const unsigned char *data,
-	     unsigned int length)
+_write_func1 (void *closure, const unsigned char *data, unsigned int length)
 {
     if (fwrite (data, 1, (size_t) length, (FILE *)closure) != length)
 	return CAIRO_STATUS_WRITE_ERROR;
     return CAIRO_STATUS_SUCCESS;
 }
 
+static cairo_status_t
+_write_func2 (void *closure, const unsigned char *data, unsigned int length)
+{
+    PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(s#)",
+					 data, length);
+    if (res == NULL)
+	return CAIRO_STATUS_WRITE_ERROR;
+    Py_DECREF(res);
+    return CAIRO_STATUS_SUCCESS;
+}
+
 /* METH_O */
 static PyObject *
 surface_write_to_png (PycairoSurface *o, PyObject *file)
 {
     FILE *fp;
     cairo_status_t status;
+    unsigned int mode = 0;
 
     if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
 	fp = fopen (PyString_AsString(file), "wb");
@@ -213,16 +224,32 @@
 	    PyErr_SetString(PyExc_IOError, "unable to open file for writing");
 	    return NULL;
 	}
+	mode = 1;
     } else if (PyObject_TypeCheck (file, &PyFile_Type)) {
 	fp = PyFile_AsFile(file);
-
+	mode = 1;
+    } else {
+	PyObject* writer = PyObject_GetAttrString (file, "write");
+	if (writer) {
+	    if (PyCallable_Check (writer))
+		mode = 2;
+	    Py_DECREF(writer);
+	}
+    }
+    if (mode == 1) {
+	status = cairo_surface_write_to_png_stream (o->surface, _write_func1,
+						    fp);
+    } else if (mode == 2) {
+	status = cairo_surface_write_to_png_stream (o->surface, _write_func2,
+						    file);
     } else {
 	PyErr_SetString(PyExc_TypeError,
-			"Surface.write_to_png takes one argument "
-			"which must be a filename (str) or file object");
+			"Surface.write_to_png takes one argument which must "
+			"be a filename (str), file object, or an object that "
+			"has a \"write\" method (like StringIO)");
 	return NULL;
     }
-    status = cairo_surface_write_to_png_stream (o->surface, _write_func, fp);
+
     if (PyObject_TypeCheck (file, &PyBaseString_Type))
     	fclose (fp);
 



More information about the cairo-commit mailing list