From c682c4df83a29bd9c2fd7f7c92c19f1c42de8b61 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 19 Dec 2012 09:54:06 +0100 Subject: [PATCH] Revert "SVGSurface: Change from bytes-mode to text-mode." This reverts parts of commit 9576b3d77034d91f456a5b199a9fc9cc2fde3c08, and adjusts the documentation. --- doc/reference/surfaces.rst | 2 +- src/surface.c | 38 +++++++++----------------------------- test/surface_create_for_stream.py | 2 +- 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/doc/reference/surfaces.rst b/doc/reference/surfaces.rst index ef01c8e..c820966 100644 --- a/doc/reference/surfaces.rst +++ b/doc/reference/surfaces.rst @@ -714,7 +714,7 @@ multi-page vector surface backend :param fobj: a filename or writable file object. None may be used to specify no output. This will generate a *SVGSurface* that may be queried and used as a source, without generating a temporary file. - :type fobj: None, filename (str), file or a file-like text-mode object + :type fobj: None, filename (str), file or a file-like bytes-mode object :param width_in_points: width of the surface, in points (1 point == 1/72.0 inch) :type width_in_points: float :param height_in_points: height of the surface, in points (1 point == 1/72.0 inch) diff --git a/src/surface.c b/src/surface.c index 204af68..9080d79 100644 --- a/src/surface.c +++ b/src/surface.c @@ -121,10 +121,10 @@ PycairoSurface_FromSurface (cairo_surface_t *surface, PyObject *base) { /* for use with * cairo_surface_write_to_png_stream() - * cairo_pdf/ps_surface_create_for_stream() + * cairo_pdf/ps/svg_surface_create_for_stream() */ static cairo_status_t -_write_bytes_func (void *closure, const unsigned char *data, unsigned int length) { +_write_func (void *closure, const unsigned char *data, unsigned int length) { PyGILState_STATE gstate = PyGILState_Ensure(); PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(y#)", data, (Py_ssize_t)length); @@ -140,26 +140,6 @@ _write_bytes_func (void *closure, const unsigned char *data, unsigned int length return CAIRO_STATUS_SUCCESS; } -/* for use with - * cairo_svg_surface_create_for_stream() - */ -static cairo_status_t -_write_str_func (void *closure, const unsigned char *data, unsigned int length) { - PyGILState_STATE gstate = PyGILState_Ensure(); - PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(s#)", - data, (Py_ssize_t)length); - if (res == NULL) { - /* an exception has occurred, it will be picked up later by - * Pycairo_Check_Status() - */ - PyGILState_Release(gstate); - return CAIRO_STATUS_WRITE_ERROR; - } - Py_DECREF(res); - PyGILState_Release(gstate); - return CAIRO_STATUS_SUCCESS; -} - static void surface_dealloc (PycairoSurface *o) { if (o->surface) { @@ -346,7 +326,7 @@ surface_write_to_png (PycairoSurface *o, PyObject *args) { } Py_DECREF(writer); Py_BEGIN_ALLOW_THREADS; - status = cairo_surface_write_to_png_stream (o->surface, _write_bytes_func, + status = cairo_surface_write_to_png_stream (o->surface, _write_func, file); Py_END_ALLOW_THREADS; } @@ -498,7 +478,7 @@ image_surface_create_for_data (PyTypeObject *type, PyObject *args) { #ifdef CAIRO_HAS_PNG_FUNCTIONS static cairo_status_t -_read_bytes_func (void *closure, unsigned char *data, unsigned int length) { +_read_func (void *closure, unsigned char *data, unsigned int length) { char *buffer; Py_ssize_t str_length; cairo_status_t status = CAIRO_STATUS_READ_ERROR; @@ -560,7 +540,7 @@ image_surface_create_from_png (PyTypeObject *type, PyObject *args) { Py_DECREF(reader); Py_BEGIN_ALLOW_THREADS; - is = cairo_image_surface_create_from_png_stream (_read_bytes_func, file); + is = cairo_image_surface_create_from_png_stream (_read_func, file); Py_END_ALLOW_THREADS; return PycairoSurface_FromSurface (is, NULL); } @@ -751,7 +731,7 @@ pdf_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { Py_DECREF(writer); Py_BEGIN_ALLOW_THREADS; - sfc = cairo_pdf_surface_create_for_stream (_write_bytes_func, obj, + sfc = cairo_pdf_surface_create_for_stream (_write_func, obj, width_in_points, height_in_points); Py_END_ALLOW_THREADS; return PycairoSurface_FromSurface (sfc, obj); @@ -903,7 +883,7 @@ ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { Py_DECREF(writer); Py_BEGIN_ALLOW_THREADS; - sfc = cairo_ps_surface_create_for_stream (_write_bytes_func, obj, + sfc = cairo_ps_surface_create_for_stream (_write_func, obj, width_in_points, height_in_points); Py_END_ALLOW_THREADS; return PycairoSurface_FromSurface (sfc, obj); @@ -1186,14 +1166,14 @@ svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { " None, or\n" " a filename (str), or\n" " a file object, or\n" -" a file-like text-mode object which has a \"write\" method." +" a file-like bytes-mode object which has a \"write\" method." ); return NULL; } Py_DECREF(writer); Py_BEGIN_ALLOW_THREADS; - sfc = cairo_svg_surface_create_for_stream (_write_str_func, obj, + sfc = cairo_svg_surface_create_for_stream (_write_func, obj, width_in_points, height_in_points); Py_END_ALLOW_THREADS; return PycairoSurface_FromSurface (sfc, obj); diff --git a/test/surface_create_for_stream.py b/test/surface_create_for_stream.py index b6ad225..90d46c9 100755 --- a/test/surface_create_for_stream.py +++ b/test/surface_create_for_stream.py @@ -30,7 +30,7 @@ class C(object): WIDTH, HEIGHT = 256, 256 # a selection of possible args to surface.write_to_png() -fo = sys.stdout # only compatible with str/text objects - SVG +fo = sys.stdout.buffer #fo = C() #fo = '/tmp/f.pdf' -- 1.8.0.2