pygtk r2968 - in trunk: . gtk tests



Author: paulp
Date: Sun Apr 27 18:57:46 2008
New Revision: 2968
URL: http://svn.gnome.org/viewvc/pygtk?rev=2968&view=rev

Log:
2008-04-27  Paul Pogonyshev  <pogonyshev gmx net>

	* gtk/gdkcolor.override (_wrap_gdk_color_new): Add constructor
	variant similar to what gtk.gdk.color_parse() done.  Raise with
	useful message in case of wrong arguments (bug #527718).

	* tests/test_conversion.py (Tests.testColorCreation): Test the new
	constructor with string argument.


Modified:
   trunk/ChangeLog
   trunk/gtk/gdkcolor.override
   trunk/tests/test_conversion.py

Modified: trunk/gtk/gdkcolor.override
==============================================================================
--- trunk/gtk/gdkcolor.override	(original)
+++ trunk/gtk/gdkcolor.override	Sun Apr 27 18:57:46 2008
@@ -30,20 +30,42 @@
                     PyObject *args,
                     PyObject *kwargs)
 {
-    static char *kwlist[] = {"red", "green", "blue", "pixel", NULL };
+    static char *kwlist1[] = {"red", "green", "blue", "pixel", NULL };
+    static char *kwlist2[] = { "spec", NULL };
     int red = 0, green = 0, blue = 0;
     unsigned int pixel = 0;
+    const char *spec;
     GdkColor colour;
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiik:gdk.Color", kwlist,
-                                     &red, &green, &blue, &pixel)) {
-        return -1;
+    if (PyArg_ParseTupleAndKeywords(args, kwargs, "|iiik:gdk.Color", kwlist1,
+                                    &red, &green, &blue, &pixel)) {
+        colour.red = red;
+        colour.green = green;
+        colour.blue = blue;
+        colour.pixel = pixel;
+        goto success;
+    }
+
+    PyErr_Clear();
+    
+    if (PyArg_ParseTupleAndKeywords(args, kwargs, "s:color_parse|gdk.Color", kwlist2,
+                                    &spec)) {
+        if (!gdk_color_parse(spec, &colour)) {
+            PyErr_SetString(PyExc_ValueError,
+                            "unable to parse colour specification");
+            return -1;
+        }
+
+        goto success;
     }
-    colour.red = red;
-    colour.green = green;
-    colour.blue = blue;
-    colour.pixel = pixel;
-    
+
+    PyErr_Clear();
+    PyErr_SetString(PyExc_TypeError, "Usage:\n"
+                    "  gtk.gdk.Color(red, green, blue, pixel)  [all are optional]\n"
+                    "  gtk.gdk.Color(spec)                     [see gtk.gdk.color_parse()]");
+    return -1;
+
+ success:
     self->boxed =  g_boxed_copy(GDK_TYPE_COLOR, &colour);
     self->free_on_dealloc = TRUE;
     self->gtype = GDK_TYPE_COLOR;

Modified: trunk/tests/test_conversion.py
==============================================================================
--- trunk/tests/test_conversion.py	(original)
+++ trunk/tests/test_conversion.py	Sun Apr 27 18:57:46 2008
@@ -69,6 +69,18 @@
         c = gtk.gdk.Color(pixel=0xffffffffL)
         self.assertEqual(c.pixel, 0xffffffffL)
 
+        c = gtk.gdk.Color('red')
+        self.assertEqual(c.red, 65535)
+        self.assertEqual(c.green, 0)
+        self.assertEqual(c.blue, 0)
+
+        c = gtk.gdk.Color('#ff0000')
+        self.assertEqual(c.red, 65535)
+        self.assertEqual(c.green, 0)
+        self.assertEqual(c.blue, 0)
+
+        self.assertRaises(TypeError, lambda: gtk.gdk.Color([]))
+
     def testUIntArg(self):
         child = gtk.DrawingArea()
         table = gtk.Table(2, 2, False)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]