[cairo-commit] pycairo/cairo pycairo-context.c, 1.91, 1.92 pycairo-font.c, 1.40, 1.41

Steve Chaplin commit at pdx.freedesktop.org
Sun Jun 21 04:53:03 PDT 2009


Committed by: stevech1097

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

Modified Files:
	pycairo-context.c pycairo-font.c 
Log Message:
'SC'

Index: pycairo-context.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-context.c,v
retrieving revision 1.91
retrieving revision 1.92
diff -u -d -r1.91 -r1.92
--- pycairo-context.c	21 Jun 2009 06:14:12 -0000	1.91
+++ pycairo-context.c	21 Jun 2009 11:53:01 -0000	1.92
@@ -37,31 +37,6 @@
 #include "pycairo-private.h"
 
 
-/* Take a PyBaseString (str or unicode) object and return a pointer to the
- * UTF-8 encoded C string.
- * Note: in Python 3.x a string is a unicode object
- */
-char *
-__PyBaseString_AsUTF8 (PyObject *o)
-{
-    if (PyString_Check(o)) {
-	/* A plain ASCII string is also a valid UTF-8 string */
-	return PyString_AsString(o);
-
-    } else if (PyUnicode_Check(o)) {
-	PyObject *u = PyUnicode_AsUTF8String(o);
-	if (u != NULL) {
-	    char *utf8 = PyString_AsString(u);
-	    Py_DECREF(u); // error: deallocate object too early ?
-	                  // so copy C string, but then need to free later,
-	                  // or unroll into the calling functions - is
-                          // useful when move to Python 3.x
-	    return utf8;
-	}
-    }
-    return NULL;
-}
-
 /* PycairoContext_FromContext
  * Create a new PycairoContext from a cairo_t
  * ctx  - a cairo_t to 'wrap' into a Python object.
@@ -891,8 +866,9 @@
 pycairo_select_font_face (PycairoContext *o, PyObject *args)
 {
     PyObject *obj;
-    const char *family;
-    cairo_font_slant_t slant = CAIRO_FONT_SLANT_NORMAL;
+    PyObject *pyUTF8 = NULL;
+    const char *utf8family = NULL;
+    cairo_font_slant_t slant   = CAIRO_FONT_SLANT_NORMAL;
     cairo_font_weight_t weight = CAIRO_FONT_WEIGHT_NORMAL;
 
     if (!PyArg_ParseTuple(args, "O!|ii:Context.select_font_face",
@@ -900,11 +876,23 @@
 	return NULL;
 
     /* accept str and unicode family, auto convert to utf8 as required */
-    family = __PyBaseString_AsUTF8 (obj);
-    if (family == NULL)
+    if (PyString_Check(obj)) {
+	/* A plain ASCII string is also a valid UTF-8 string */
+	utf8family = PyString_AS_STRING(obj);
+    } else if (PyUnicode_Check(obj)) {
+	pyUTF8 = PyUnicode_AsUTF8String(obj);
+	if (pyUTF8 != NULL) {
+	    utf8family = PyString_AS_STRING(pyUTF8);
+	}
+    } else {
+	PyErr_SetString(PyExc_TypeError,
+	    "Context.select_font_face: family must be str or unicode");
+    }
+    if (utf8family == NULL)
 	return NULL;
 
-    cairo_select_font_face (o->ctx, family, slant, weight);
+    cairo_select_font_face (o->ctx, utf8family, slant, weight);
+    Py_XDECREF(pyUTF8);
     RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
     Py_RETURN_NONE;
 }
@@ -1226,18 +1214,29 @@
 static PyObject *
 pycairo_show_text (PycairoContext *o, PyObject *obj)
 {
+    PyObject *pyUTF8 = NULL;
+    const char *utf8 = NULL;
+
     /* accept str and unicode text, auto convert to utf8 as required */
-    const char *utf8 = __PyBaseString_AsUTF8 (obj);
-    if (utf8==NULL) {
+    if (PyString_Check(obj)) {
+	/* A plain ASCII string is also a valid UTF-8 string */
+	utf8 = PyString_AS_STRING(obj);
+    } else if (PyUnicode_Check(obj)) {
+	pyUTF8 = PyUnicode_AsUTF8String(obj);
+	if (pyUTF8 != NULL) {
+	    utf8 = PyString_AS_STRING(pyUTF8);
+	}
+    } else {
 	PyErr_SetString(PyExc_TypeError,
-			"Context.show_text() argument must be a string or "
-			"unicode object");
-	return NULL;
+	    "Context.show_text: text must be str or unicode");
     }
+    if (utf8 == NULL)
+	return NULL;
 
     Py_BEGIN_ALLOW_THREADS
     cairo_show_text (o->ctx, utf8);
     Py_END_ALLOW_THREADS
+    Py_XDECREF(pyUTF8);
     RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
     Py_RETURN_NONE;
 }
@@ -1274,17 +1273,28 @@
 static PyObject *
 pycairo_text_extents (PycairoContext *o, PyObject *obj)
 {
-    /* accept str and unicode text, auto convert to utf8 as required */
     cairo_text_extents_t extents;
-    const char *utf8 = __PyBaseString_AsUTF8 (obj);
-    if (utf8==NULL) {
+    PyObject *pyUTF8 = NULL;
+    const char *utf8 = NULL;
+
+    /* accept str and unicode text, auto convert to utf8 as required */
+    if (PyString_Check(obj)) {
+	/* A plain ASCII string is also a valid UTF-8 string */
+	utf8 = PyString_AS_STRING(obj);
+    } else if (PyUnicode_Check(obj)) {
+	pyUTF8 = PyUnicode_AsUTF8String(obj);
+	if (pyUTF8 != NULL) {
+	    utf8 = PyString_AS_STRING(pyUTF8);
+	}
+    } else {
 	PyErr_SetString(PyExc_TypeError,
-			"Context.text_extents() argument must be a string or "
-			"unicode object");
-	return NULL;
+	    "Context.text_extents: text must be str or unicode");
     }
+    if (utf8 == NULL)
+	return NULL;
 
     cairo_text_extents (o->ctx, utf8, &extents);
+    Py_XDECREF(pyUTF8);
     RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
     return Py_BuildValue("(dddddd)", extents.x_bearing, extents.y_bearing,
 			 extents.width, extents.height, extents.x_advance,
@@ -1294,16 +1304,27 @@
 static PyObject *
 pycairo_text_path (PycairoContext *o, PyObject *obj)
 {
+    PyObject *pyUTF8 = NULL;
+    const char *utf8 = NULL;
+
     /* accept str and unicode text, auto convert to utf8 as required */
-    const char *utf8 = __PyBaseString_AsUTF8 (obj);
-    if (utf8==NULL) {
+    if (PyString_Check(obj)) {
+	/* A plain ASCII string is also a valid UTF-8 string */
+	utf8 = PyString_AS_STRING(obj);
+    } else if (PyUnicode_Check(obj)) {
+	pyUTF8 = PyUnicode_AsUTF8String(obj);
+	if (pyUTF8 != NULL) {
+	    utf8 = PyString_AS_STRING(pyUTF8);
+	}
+    } else {
 	PyErr_SetString(PyExc_TypeError,
-			"Context.text_path() argument must be a string or "
-			"unicode object");
-	return NULL;
+	    "Context.text_path: text must be str or unicode");
     }
+    if (utf8 == NULL)
+	return NULL;
 
     cairo_text_path (o->ctx, utf8);
+    Py_XDECREF(pyUTF8);
     RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
     Py_RETURN_NONE;
 }

Index: pycairo-font.c
===================================================================
RCS file: /cvs/cairo/pycairo/cairo/pycairo-font.c,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -d -r1.40 -r1.41
--- pycairo-font.c	12 Feb 2009 09:04:29 -0000	1.40
+++ pycairo-font.c	21 Jun 2009 11:53:01 -0000	1.41
@@ -155,16 +155,36 @@
 static PyObject *
 toy_font_face_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    char *family;
-    cairo_font_slant_t slant = CAIRO_FONT_SLANT_NORMAL;
+    PyObject *obj;
+    PyObject *pyUTF8 = NULL;
+    const char *utf8family = NULL;
+    cairo_font_slant_t slant   = CAIRO_FONT_SLANT_NORMAL;
     cairo_font_weight_t weight = CAIRO_FONT_WEIGHT_NORMAL;
 
-    if (!PyArg_ParseTuple(args, "s|II:ToyFont.__new__", &family, &slant,
-			  &weight))
+    if (!PyArg_ParseTuple(args, "O!|ii:ToyFontFace.__new__",
+			  &PyBaseString_Type, &obj, &slant, &weight))
 	return NULL;
 
-    return PycairoFontFace_FromFontFace (
-             cairo_toy_font_face_create (family, slant, weight));
+    /* accept str and unicode family, auto convert to utf8 as required */
+    if (PyString_Check(obj)) {
+	/* A plain ASCII string is also a valid UTF-8 string */
+	utf8family = PyString_AS_STRING(obj);
+    } else if (PyUnicode_Check(obj)) {
+	pyUTF8 = PyUnicode_AsUTF8String(obj);
+	if (pyUTF8 != NULL) {
+	    utf8family = PyString_AS_STRING(pyUTF8);
+	}
+    } else {
+	PyErr_SetString(PyExc_TypeError,
+	    "ToyFontFace.__new__: family must be str or unicode");
+    }
+    if (utf8family == NULL)
+	return NULL;
+
+    PyObject *o = PycairoFontFace_FromFontFace (
+		cairo_toy_font_face_create (utf8family, slant, weight));
+    Py_XDECREF(pyUTF8);
+    return o;
 }
 
 static PyObject *
@@ -326,15 +346,27 @@
 scaled_font_text_extents (PycairoScaledFont *o, PyObject *obj)
 {
     cairo_text_extents_t extents;
-    const char *utf8 = __PyBaseString_AsUTF8 (obj);
-    if (utf8 == NULL) {
+    PyObject *pyUTF8 = NULL;
+    const char *utf8 = NULL;
+
+    /* accept str and unicode text, auto convert to utf8 as required */
+    if (PyString_Check(obj)) {
+	/* A plain ASCII string is also a valid UTF-8 string */
+	utf8 = PyString_AS_STRING(obj);
+    } else if (PyUnicode_Check(obj)) {
+	pyUTF8 = PyUnicode_AsUTF8String(obj);
+	if (pyUTF8 != NULL) {
+	    utf8 = PyString_AS_STRING(pyUTF8);
+	}
+    } else {
 	PyErr_SetString(PyExc_TypeError,
-		"ScaledFont.text_extents() argument must be a string or "
-		"unicode object");
-	return NULL;
+	    "ScaledFont.text_extents: text must be str or unicode");
     }
+    if (utf8 == NULL)
+	return NULL;
 
     cairo_scaled_font_text_extents (o->scaled_font, utf8, &extents);
+    Py_XDECREF(pyUTF8);
     RETURN_NULL_IF_CAIRO_SCALED_FONT_ERROR(o->scaled_font);
     return Py_BuildValue("(dddddd)", extents.x_bearing, extents.y_bearing,
 			 extents.width, extents.height, extents.x_advance,



More information about the cairo-commit mailing list