[cairo-commit] pycairo/cairo cairomodule.c, 1.52, 1.53 pycairo-surface.c, 1.72, 1.73

Steve Chaplin commit at pdx.freedesktop.org
Sat May 27 19:03:08 PDT 2006


Committed by: stevech1097

Update of /cvs/cairo/pycairo/cairo
In directory kemper:/tmp/cvs-serv13230/cairo

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

Index: cairomodule.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/cairomodule.c,v
retrieving revision 1.52
retrieving revision 1.53
diff -u -d -r1.52 -r1.53
--- cairomodule.c	1 May 2006 02:20:15 -0000	1.52
+++ cairomodule.c	28 May 2006 02:03:07 -0000	1.53
@@ -42,6 +42,9 @@
 int
 Pycairo_Check_Status (cairo_status_t status)
 {
+    if (PyErr_Occurred() != NULL)
+	return 1;
+
     switch (status) {
     case CAIRO_STATUS_SUCCESS:
 	return 0;

Index: pycairo-surface.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-surface.c,v
retrieving revision 1.72
retrieving revision 1.73
diff -u -d -r1.72 -r1.73
--- pycairo-surface.c	26 May 2006 15:32:14 -0000	1.72
+++ pycairo-surface.c	28 May 2006 02:03:07 -0000	1.73
@@ -49,7 +49,7 @@
  * Create a new PycairoSurface from a cairo_surface_t
  * surface - a cairo_surface_t to 'wrap' into a Python object.
  *           it is unreferenced if the PycairoSurface creation fails, or if
-             the cairo_surface_t has an error status
+ *           the cairo_surface_t has an error status
  * base - the base object used to create the context, or NULL.
  *        it is referenced to keep it alive while the cairo_surface_t is
  *        being used
@@ -113,6 +113,25 @@
     return o;
 }
 
+/* for use with
+ * cairo_surface_write_to_png_stream()
+ * cairo_pdf/ps/svg_surface_create_for_stream()
+ */
+static cairo_status_t
+_write_func (void *closure, const unsigned char *data, unsigned int length)
+{
+    PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(s#)",
+					 data, length);
+    if (res == NULL) {
+	/* an exception has occurred, it will be picked up later by
+	 * Pycairo_Check_Status()
+	 */
+	return CAIRO_STATUS_WRITE_ERROR;
+    }
+    Py_DECREF(res);
+    return CAIRO_STATUS_SUCCESS;
+}
+
 static void
 surface_dealloc (PycairoSurface *o)
 {
@@ -148,7 +167,6 @@
     if (!PyArg_ParseTuple (args, "iii:Surface.create_similar",
 			   &content, &width, &height))
 	return NULL;
-
     return PycairoSurface_FromSurface (
 	     cairo_surface_create_similar (o->surface, content, width, height),
 	     NULL);
@@ -225,24 +243,6 @@
 }
 
 #ifdef CAIRO_HAS_PNG_FUNCTIONS
-static cairo_status_t
-_write_func (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;
-}
-
-static PyObject *
-_err_closed (void)
-{
-    PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
-    return NULL;
-}
-
 /* METH_O */
 static PyObject *
 surface_write_to_png (PycairoSurface *o, PyObject *file)
@@ -260,26 +260,13 @@
 	    Py_XDECREF(writer);
 	    PyErr_SetString(PyExc_TypeError,
 "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)");
+"object, or a file-like object which has a \"write\" method (like StringIO)");
 	    return NULL;
 	}
 	Py_DECREF(writer);
-
-	/* check object has 'closed' attribute and it is False */
-	PyObject* closed = PyObject_GetAttrString (file, "closed");
-	if (closed == NULL) {
-	    PyErr_SetString(PyExc_ValueError,
-			    "object does not have a closed attribute");
-	    return NULL;
-	} else if (closed == Py_True) {
-	    Py_DECREF(closed);
-	    return _err_closed();
-	}
-	Py_DECREF(closed);
 	status = cairo_surface_write_to_png_stream (o->surface, _write_func,
 						    file);
     }
-
     if (Pycairo_Check_Status (status))
 	return NULL;
     Py_RETURN_NONE;
@@ -364,24 +351,13 @@
 {
     cairo_format_t format;
     int width, height;
-    cairo_surface_t *surface;
-    PyObject *o;
 
     if (!PyArg_ParseTuple (args, "iii:ImageSurface.__new__",
 			   &format, &width, &height))
 	return NULL;
-
-    o = type->tp_alloc(type, 0);
-    if (o != NULL) {
-	surface = cairo_image_surface_create (format, width, height);
-	if (Pycairo_Check_Status (cairo_surface_status (surface))) {
-	    cairo_surface_destroy (surface);
-	    Py_DECREF(o);
-	    return NULL;
-	}
-	((PycairoImageSurface *)o)->surface = surface;
-    }
-    return o;
+    return PycairoSurface_FromSurface (
+	       cairo_image_surface_create (format, width, height),
+	       NULL);
 }
 
 #ifdef HAVE_NUMPY
@@ -718,27 +694,36 @@
 static PyObject *
 pdf_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    const char *filename;
     double width_in_points, height_in_points;
-    cairo_surface_t *surface;
-    PyObject *o;
+    PyObject *file;
 
-    if (!PyArg_ParseTuple(args, "sdd:PDFSurface.__new__",
-			  &filename, &width_in_points, &height_in_points))
+    if (!PyArg_ParseTuple(args, "Odd:PDFSurface.__new__",
+			  &file, &width_in_points, &height_in_points))
 	return NULL;
 
-    o = type->tp_alloc(type, 0);
-    if (o != NULL) {
-	surface = cairo_pdf_surface_create (filename, width_in_points,
-					    height_in_points);
-	if (Pycairo_Check_Status (cairo_surface_status (surface))) {
-	    cairo_surface_destroy (surface);
-	    Py_DECREF(o);
+    if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
+	/* string (filename) argument */
+	return PycairoSurface_FromSurface (
+                  cairo_pdf_surface_create (PyString_AsString(file),
+                                     width_in_points, height_in_points),
+	       NULL);
+
+    } else {  /* file or file-like object argument */
+	PyObject* writer = PyObject_GetAttrString (file, "write");
+	if (writer == NULL || !PyCallable_Check (writer)) {
+	    Py_XDECREF(writer);
+	    PyErr_SetString(PyExc_TypeError,
+"PDFSurface argument 1 must be a filename (str), file object, or an object "
+"that has a \"write\" method (like StringIO)");
 	    return NULL;
 	}
-	((PycairoPDFSurface *)o)->surface = surface;
+	Py_DECREF(writer);
+
+	return PycairoSurface_FromSurface (
+	           cairo_pdf_surface_create_for_stream (_write_func,
+		       file, width_in_points, height_in_points),
+	       file);
     }
-    return o;
 }
 
 static PyObject *
@@ -825,27 +810,36 @@
 static PyObject *
 ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    const char *filename;
     double width_in_points, height_in_points;
-    cairo_surface_t *surface;
-    PyObject *o;
+    PyObject *file;
 
-    if (!PyArg_ParseTuple(args, "sdd:PSSurface.__new__",
-			  &filename, &width_in_points, &height_in_points))
+    if (!PyArg_ParseTuple(args, "Odd:PSSurface.__new__",
+			  &file, &width_in_points, &height_in_points))
 	return NULL;
 
-    o = type->tp_alloc(type, 0);
-    if (o != NULL) {
-	surface = cairo_ps_surface_create (filename, width_in_points,
-					   height_in_points);
-	if (Pycairo_Check_Status (cairo_surface_status (surface))) {
-	    cairo_surface_destroy (surface);
-	    Py_DECREF(o);
+    if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
+	/* string (filename) argument */
+	return PycairoSurface_FromSurface (
+                  cairo_ps_surface_create (PyString_AsString(file),
+                                     width_in_points, height_in_points),
+	       NULL);
+
+    } else {  /* file or file-like object argument */
+	PyObject* writer = PyObject_GetAttrString (file, "write");
+	if (writer == NULL || !PyCallable_Check (writer)) {
+	    Py_XDECREF(writer);
+	    PyErr_SetString(PyExc_TypeError,
+"PSSurface argument 1 must be a filename (str), file object, or an object "
+"that has a \"write\" method (like StringIO)");
 	    return NULL;
 	}
-	((PycairoPSSurface *)o)->surface = surface;
+	Py_DECREF(writer);
+
+	return PycairoSurface_FromSurface (
+	           cairo_ps_surface_create_for_stream (_write_func,
+		       file, width_in_points, height_in_points),
+	       file);
     }
-    return o;
 }
 
 static PyObject *
@@ -967,27 +961,36 @@
 static PyObject *
 svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    const char *filename;
     double width_in_points, height_in_points;
-    cairo_surface_t *surface;
-    PyObject *o;
+    PyObject *file;
 
-    if (!PyArg_ParseTuple(args, "sdd:SVGSurface.__new__",
-			  &filename, &width_in_points, &height_in_points))
+    if (!PyArg_ParseTuple(args, "Odd:SVGSurface.__new__",
+			  &file, &width_in_points, &height_in_points))
 	return NULL;
 
-    o = type->tp_alloc(type, 0);
-    if (o != NULL) {
-	surface = cairo_svg_surface_create (filename, width_in_points,
-					    height_in_points);
-	if (Pycairo_Check_Status (cairo_surface_status (surface))) {
-	    cairo_surface_destroy (surface);
-	    Py_DECREF(o);
+    if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
+	/* string (filename) argument */
+	return PycairoSurface_FromSurface (
+                  cairo_svg_surface_create (PyString_AsString(file),
+                                     width_in_points, height_in_points),
+	       NULL);
+
+    } else {  /* file or file-like object argument */
+	PyObject* writer = PyObject_GetAttrString (file, "write");
+	if (writer == NULL || !PyCallable_Check (writer)) {
+	    Py_XDECREF(writer);
+	    PyErr_SetString(PyExc_TypeError,
+"SVGSurface argument 1 must be a filename (str), file object, or an object "
+"that has a \"write\" method (like StringIO)");
 	    return NULL;
 	}
-	((PycairoSVGSurface *)o)->surface = surface;
+	Py_DECREF(writer);
+
+	return PycairoSurface_FromSurface (
+	           cairo_svg_surface_create_for_stream (_write_func,
+		       file, width_in_points, height_in_points),
+	       file);
     }
-    return o;
 }
 
 static PyObject *
@@ -1066,23 +1069,11 @@
 win32_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
     int hdc;
-    cairo_surface_t *surface;
-    PyObject *o;
 
-    if (!PyArg_ParseTuple(args, "i:Win32Surface.__new__",  &hdc))
+    if (!PyArg_ParseTuple(args, "i:Win32Surface.__new__", &hdc))
 	return NULL;
-
-    o = type->tp_alloc(type, 0);
-    if (o != NULL) {
-	surface = cairo_win32_surface_create ((HDC)hdc);
-	if (Pycairo_Check_Status (cairo_surface_status (surface))) {
-	    cairo_surface_destroy (surface);
-	    Py_DECREF(o);
-	    return NULL;
-	}
-	((PycairoWin32Surface *)o)->surface = surface;
-    }
-    return o;
+    return PycairoSurface_FromSurface (
+	       cairo_win32_surface_create ((HDC)hdc), NULL);
 }
 
 static PyMethodDef win32_surface_methods[] = {



More information about the cairo-commit mailing list