[pygtk] Color rgb values & Python 2.0

John Ehresman jpe@archaeopteryx.com
Tue, 26 Sep 2000 12:26:10 -0400 (EDT)


Hi,

Below is a patch to make colors constructed from red, green, blue triples
work under Python 2.0 (or at least 2.0b1).  The problem is that
PyArg_ParseTuple now checks to see the value of a short integer
(specified with 'h' in the format string) falls in the range of a signed
short int.  Since a unsigned short integer is what is needed, the patch
below gets the values as a long and then checks the range of values.  I
think the 'H' format character could be used under 2.0 to specify the
variable is an unsigned short int, but this solution works under both
1.5.2 and 2.0.

The patch is against 0.6.5, but should apply to 0.6.6.

Thanks,

John

------------------------------------------------------------------------
Archaeopteryx Software, Inc.                        Wing IDE for Python 
www.archaeopteryx.com                               Take Flight!

--- /home/jpe/down/pygtk-0.6.5/gtkmodule.c	Mon Mar 13 04:04:14 2000
+++ gtkmodule.c	Mon Sep 25 23:24:32 2000

@@ -5588,19 +5606,18 @@
 }
 /* gdk_color_alloc analog */
 static PyObject *_wrap_gdk_color_alloc(PyObject *self, PyObject *args) {
+    int red, blue, green;
     GdkColor gdk_color;
     GdkColormap *colormap;
     PyGtkStyle_Object *style;
     PyGtk_Object *obj;
-    if (PyArg_ParseTuple(args, "O!hhh:gdk_color_alloc", &PyGtkStyle_Type,
-			 &style, &(gdk_color.red), &(gdk_color.green),
-			 &(gdk_color.blue)))
+    if (PyArg_ParseTuple(args, "O!iii:gdk_color_alloc", &PyGtkStyle_Type,
+			 &style, &red, &green, &blue))
         colormap = PyGtkStyle_Get(style)->colormap;
     else {
         PyErr_Clear();
-	if (!PyArg_ParseTuple(args, "O!hhh:gdk_color_alloc", &PyGtk_Type,
-			      &obj, &(gdk_color.red), &(gdk_color.green),
-			      &(gdk_color.blue)))
+	if (!PyArg_ParseTuple(args, "O!iii:gdk_color_alloc", &PyGtk_Type,
+			      &obj, &red, &green, &blue))
 	    return NULL;
 	if (GTK_WIDGET_NO_WINDOW(PyGtk_Get(obj))) {
 	    PyErr_SetString(PyExc_TypeError, "widget has no window");
@@ -5608,18 +5625,35 @@
 	}
 	colormap = gdk_window_get_colormap(GTK_WIDGET(PyGtk_Get(obj))->window);
     }
+    if ( red < 0 || red > 0xFFFF || green < 0 || green > 0xFFFF
+         || blue < 0 || blue > 0xFFFF ) {
+        PyErr_SetString(PyExc_OverflowError, "Color index value not in range");
+        return NULL;
+    }
+    gdk_color.red = red;
+    gdk_color.green = green;
+    gdk_color.blue = blue;
     gdk_color_alloc(colormap, &gdk_color);
     return PyGdkColor_New(&gdk_color);
 }
 
 static PyObject *_wrap_gdk_color_new(PyObject *self, PyObject *args) {
+    int red, blue, green;
     GdkColor gdk_color;
+
     
-    if (!PyArg_ParseTuple(args, "hhhl:gdk_color_new", 
-			  &(gdk_color.red), &(gdk_color.green),
-			  &(gdk_color.blue), &(gdk_color.pixel)))
-      return NULL;
+    if (!PyArg_ParseTuple(args, "iiil:gdk_color_new", &red, &green, &blue,
+			  &(gdk_color.pixel)))
+        return NULL;
 
+    if ( red < 0 || red > 0xFFFF || green < 0 || green > 0xFFFF
+         || blue < 0 || blue > 0xFFFF ) {
+        PyErr_SetString(PyExc_OverflowError, "Color index value not in range");
+        return NULL;
+    }
+    gdk_color.red = red;
+    gdk_color.green = green;
+    gdk_color.blue = blue;
     return PyGdkColor_New(&gdk_color);
 }