summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Chaplin <stevech1097@yahoo.com.au>2009-08-24 20:09:32 +0800
committerSteve Chaplin <stevech1097@yahoo.com.au>2009-08-24 20:09:32 +0800
commit9d9ad8f01001df91a131cc811cf733409c813829 (patch)
treeed01875df19be67cbf760da53009d1cc9ddb2f63
parenta0b47905e765aeeda6220c2dece4d959b7b25a5f (diff)
downloadpy2cairo-9d9ad8f01001df91a131cc811cf733409c813829.tar.gz
allow None as filename arg to PDF/PS/SVGSurface
-rw-r--r--doc/reference/surfaces.rst8
-rw-r--r--src/pycairo-surface.c52
2 files changed, 45 insertions, 15 deletions
diff --git a/doc/reference/surfaces.rst b/doc/reference/surfaces.rst
index 8c6332a..f1779a4 100644
--- a/doc/reference/surfaces.rst
+++ b/doc/reference/surfaces.rst
@@ -340,8 +340,8 @@ multi-page vector surface backend.
.. class:: PDFSurface(fobj, width_in_points, height_in_points)
- :param fobj: a filename or writable file object
- :type fobj: str, file or file-like object
+ :param fobj: a filename or writable file object. None may be used to specify no output. This will generate a *PDFSurface* that may be queried and used as a source, without generating a temporary file.
+ :type fobj: None, str, file or file-like object
:param width_in_points: width of the surface, in points
(1 point == 1/72.0 inch)
:type width_in_points: float
@@ -382,7 +382,7 @@ is a multi-page vector surface backend.
.. class:: PSSurface(fobj, width_in_points, height_in_points)
- :param fobj: a filename or writable file object
+ :param fobj: a filename or writable file object. None may be used to specify no output. This will generate a *PSSurface* that may be queried and used as a source, without generating a temporary file.
:type fobj: str, file or file-like object
:param width_in_points: width of the surface, in points
(1 point == 1/72.0 inch)
@@ -585,7 +585,7 @@ multi-page vector surface backend
.. class:: SVGSurface(fobj, width_in_points, height_in_points)
- :param fobj: a filename or writable file object
+ :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: str, file or file-like object
:param width_in_points: width of the surface, in points (1 point == 1/72.0 inch)
:type width_in_points: float
diff --git a/src/pycairo-surface.c b/src/pycairo-surface.c
index 65af7f5..b4a4a2d 100644
--- a/src/pycairo-surface.c
+++ b/src/pycairo-surface.c
@@ -706,7 +706,13 @@ pdf_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
&file, &width_in_points, &height_in_points))
return NULL;
- if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
+ if (file == Py_None) {
+ Py_BEGIN_ALLOW_THREADS
+ sfc = cairo_pdf_surface_create (NULL,
+ width_in_points, height_in_points);
+ Py_END_ALLOW_THREADS
+ return PycairoSurface_FromSurface (sfc, NULL);
+ }else if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
/* string (filename) argument */
Py_BEGIN_ALLOW_THREADS
sfc = cairo_pdf_surface_create (PyString_AsString(file),
@@ -719,8 +725,12 @@ pdf_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
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)");
+"PDFSurface argument 1 must be\n"
+" None, or\n"
+" a filename (str), or\n"
+" a file object, or\n"
+" an object that has a \"write\" method (like StringIO)."
+ );
return NULL;
}
Py_DECREF(writer);
@@ -812,11 +822,17 @@ ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
&file, &width_in_points, &height_in_points))
return NULL;
- if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
+ if (file == Py_None) {
+ Py_BEGIN_ALLOW_THREADS
+ sfc = cairo_ps_surface_create (NULL,
+ width_in_points, height_in_points);
+ Py_END_ALLOW_THREADS
+ return PycairoSurface_FromSurface (sfc, NULL);
+ }else if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
/* string (filename) argument */
Py_BEGIN_ALLOW_THREADS
sfc = cairo_ps_surface_create (PyString_AsString(file),
- width_in_points, height_in_points);
+ width_in_points, height_in_points);
Py_END_ALLOW_THREADS
return PycairoSurface_FromSurface (sfc, NULL);
}
@@ -825,8 +841,12 @@ ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
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)");
+"PSSurface argument 1 must be\n"
+" None, or\n"
+" a filename (str), or\n"
+" a file object, or\n"
+" an object that has a \"write\" method (like StringIO)."
+ );
return NULL;
}
Py_DECREF(writer);
@@ -1004,11 +1024,17 @@ svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
&file, &width_in_points, &height_in_points))
return NULL;
- if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
+ if (file == Py_None) {
+ Py_BEGIN_ALLOW_THREADS
+ sfc = cairo_svg_surface_create (NULL,
+ width_in_points, height_in_points);
+ Py_END_ALLOW_THREADS
+ return PycairoSurface_FromSurface (sfc, NULL);
+ }else if (PyObject_TypeCheck (file, &PyBaseString_Type)) {
/* string (filename) argument */
Py_BEGIN_ALLOW_THREADS
sfc = cairo_svg_surface_create (PyString_AsString(file),
- width_in_points, height_in_points);
+ width_in_points, height_in_points);
Py_END_ALLOW_THREADS
return PycairoSurface_FromSurface (sfc, NULL);
}
@@ -1017,8 +1043,12 @@ svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
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)");
+"SVGSurface argument 1 must be\n"
+" None, or\n"
+" a filename (str), or\n"
+" a file object, or\n"
+" an object that has a \"write\" method (like StringIO)."
+ );
return NULL;
}
Py_DECREF(writer);