[cairo-commit] pycairo/cairo pycairo-surface.c,1.74,1.75
Steve Chaplin
commit at pdx.freedesktop.org
Wed Jun 21 21:52:33 PDT 2006
Committed by: stevech1097
Update of /cvs/cairo/pycairo/cairo
In directory kemper:/tmp/cvs-serv29371/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.74
retrieving revision 1.75
diff -u -d -r1.74 -r1.75
--- pycairo-surface.c 11 Jun 2006 07:17:26 -0000 1.74
+++ pycairo-surface.c 22 Jun 2006 04:52:31 -0000 1.75
@@ -489,15 +489,7 @@
#ifdef CAIRO_HAS_PNG_FUNCTIONS
static cairo_status_t
-_read_func1 (void *closure, unsigned char *data, unsigned int length)
-{
- if (fread (data, 1, (size_t) length, (FILE *)closure) != length)
- return CAIRO_STATUS_READ_ERROR;
- return CAIRO_STATUS_SUCCESS;
-}
-
-static cairo_status_t
-_read_func2 (void *closure, unsigned char *data, unsigned int length)
+_read_func (void *closure, unsigned char *data, unsigned int length)
{
char *str;
PyObject *pystr = PyObject_CallMethod ((PyObject *)closure, "read", "(i)",
@@ -517,45 +509,25 @@
static PyObject *
image_surface_create_from_png (PyTypeObject *type, PyObject *file)
{
- FILE *fp = NULL;
- cairo_surface_t *surface;
- unsigned int mode = 0;
-
if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
- fp = fopen (PyString_AsString(file), "rb");
- if (fp == NULL) {
- PyErr_SetString(PyExc_IOError, "unable to open file for reading");
- return NULL;
- }
- mode = 1;
- } else if (PyObject_TypeCheck (file, &PyFile_Type)) {
- fp = PyFile_AsFile(file);
- mode = 1;
- } else {
- PyObject* reader = PyObject_GetAttrString (file, "read");
- if (reader) {
- if (PyCallable_Check (reader))
- mode = 2;
- Py_DECREF(reader);
- }
+ return PycairoSurface_FromSurface (
+ cairo_image_surface_create_from_png (PyString_AsString(file)),
+ NULL);
}
- if (mode == 1) {
- surface = cairo_image_surface_create_from_png_stream (_read_func1, fp);
- } else if (mode == 2) {
- surface = cairo_image_surface_create_from_png_stream (_read_func2,
- file);
- } else {
+
+ /* file or file-like object argument */
+ PyObject* reader = PyObject_GetAttrString (file, "read");
+ if (reader == NULL || !PyCallable_Check (reader)) {
+ Py_XDECREF(reader);
PyErr_SetString(PyExc_TypeError,
- "ImageSurface.create_from_png takes one argument "
- "which must be a filename (str), file object, or an "
- "object that has a \"read\" method (like StringIO)");
+"ImageSurface.create_from_png argument must be a filename (str), file object, "
+"or an object that has a \"read\" method (like StringIO)");
return NULL;
}
+ Py_DECREF(reader);
- if (PyObject_TypeCheck (file, &PyBaseString_Type))
- fclose (fp);
-
- return PycairoSurface_FromSurface (surface, NULL);
+ return PycairoSurface_FromSurface (
+ cairo_image_surface_create_from_png_stream (_read_func, file), NULL);
}
#endif /* CAIRO_HAS_PNG_FUNCTIONS */
More information about the cairo-commit
mailing list