[cairo-commit] pycairo/cairo pycairo-surface.c,1.70,1.71
Steve Chaplin
commit at pdx.freedesktop.org
Thu May 25 21:56:02 PDT 2006
Committed by: stevech1097
Update of /cvs/cairo/pycairo/cairo
In directory kemper:/tmp/cvs-serv18050/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.70
retrieving revision 1.71
diff -u -d -r1.70 -r1.71
--- pycairo-surface.c 17 May 2006 02:25:57 -0000 1.70
+++ pycairo-surface.c 26 May 2006 04:55:58 -0000 1.71
@@ -238,19 +238,28 @@
{
PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(s#)",
data, length);
- if (res == NULL)
+ if (res == NULL) {
+ printf ("res==NUL error\n\n");
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)
{
FILE *fp = NULL;
cairo_status_t status;
- unsigned int mode = 0;
if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
fp = fopen (PyString_AsString(file), "wb");
@@ -258,35 +267,49 @@
PyErr_SetString(PyExc_IOError, "unable to open file for writing");
return NULL;
}
- mode = 1;
+ status = cairo_surface_write_to_png_stream (o->surface, _write_func1,
+ fp);
+ fclose (fp);
+
} 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);
+
+ /* check file.closed is False */
+ PyObject* closed = PyObject_GetAttrString (file, "closed");
+ if (closed == Py_True) {
+ Py_DECREF(closed);
+ return err_closed();
}
- }
- if (mode == 1) {
+ Py_DECREF(closed);
status = cairo_surface_write_to_png_stream (o->surface, _write_func1,
fp);
- } else if (mode == 2) {
+
+ } else {
+ PyObject* writer = PyObject_GetAttrString (file, "write");
+ if (writer == NULL || !PyCallable_Check (writer)) {
+ 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)");
+ 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_func2,
file);
- } else {
- 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)");
- return NULL;
}
- if (PyObject_TypeCheck (file, &PyBaseString_Type))
- fclose (fp);
-
if (Pycairo_Check_Status (status))
return NULL;
Py_RETURN_NONE;
More information about the cairo-commit
mailing list