pygtk r3014 - in trunk: . docs/reference gtk tests



Author: paulp
Date: Sat Aug  2 16:08:41 2008
New Revision: 3014
URL: http://svn.gnome.org/viewvc/pygtk?rev=3014&view=rev

Log:
2008-08-02  Paul Pogonyshev  <pogonyshev gmx net>

	Bug 527212 â types with well-defined equality semantics are not
	properly comparable

	* gtk/gdk.override (_wrap_pygdk_region_tp_richcompare): New
	function.

	* gtk/gdkcolor.override (_wrap_gdk_color_tp_richcompare): New
	function.

	* gtk/gdkrectangle.override (_wrap_gdk_rectangle_tp_richcompare):
	New function.

	* tests/test_conversion.py (testColorCreation): Move to thematic
	test file.

	* tests/Makefile.am:
	* tests/test_color.py:
	* tests/test_rectangle.py: Two new test files.

2008-08-02  Paul Pogonyshev  <pogonyshev gmx net>

	Bug 527212 â types with well-defined equality semantics are not
	properly comparable

	* pygtk-gdkcolor.xml: Document new constructor option.  Document
	proper comparison as of PyGTK 2.14.

	* pygtk-gdkregion.xml: Document proper comparison as of PyGTK
	2.14.

	* pygtk-gdkrectangle.xml: Document proper comparison as of PyGTK
	2.14.


Added:
   trunk/tests/test_color.py
   trunk/tests/test_rectangle.py
Modified:
   trunk/ChangeLog
   trunk/docs/reference/ChangeLog
   trunk/docs/reference/pygtk-gdkcolor.xml
   trunk/docs/reference/pygtk-gdkrectangle.xml
   trunk/docs/reference/pygtk-gdkregion.xml
   trunk/gtk/gdk.override
   trunk/gtk/gdkcolor.override
   trunk/gtk/gdkrectangle.override
   trunk/tests/Makefile.am
   trunk/tests/test_conversion.py

Modified: trunk/docs/reference/pygtk-gdkcolor.xml
==============================================================================
--- trunk/docs/reference/pygtk-gdkcolor.xml	(original)
+++ trunk/docs/reference/pygtk-gdkcolor.xml	Sat Aug  2 16:08:41 2008
@@ -109,6 +109,14 @@
 the <link linkend="class-gtkstyle"><classname>gtk.Style</classname></link>
 object.</para>
 
+    <para>
+      Starting with PyGTK 2.14 <classname>gtk.gdk.Color</classname> objects are properly
+      comparable.  By Python rules, colors (being mutable) are now unhashable.  If you
+      need to use them as dictionary keys, use string representation instead.  You can
+      convert string representation to <classname>gtk.gdk.Color</classname> objects using
+      the constructor.
+    </para>
+
   </refsect1>
 
   <refsect1 id="constructor-gdkcolor">
@@ -128,6 +136,11 @@
 	<methodparam><parameter
 		       role="keyword">pixel</parameter>
 	  <initializer>0</initializer></methodparam>
+    </constructorsynopsis></programlisting>
+    <programlisting><constructorsynopsis language="python">
+	<methodname>gtk.gdk.Color</methodname>
+	<methodparam><parameter
+		       role="keyword">spec</parameter></methodparam>
       </constructorsynopsis></programlisting>
     <variablelist>
       <varlistentry>
@@ -151,6 +164,10 @@
 colormap</simpara></listitem>
       </varlistentry>
       <varlistentry>
+	<term><parameter role="keyword">spec</parameter>&nbsp;:</term>
+	<listitem><simpara>String containing color specification</simpara></listitem>
+      </varlistentry>
+      <varlistentry>
 	<term><emphasis>Returns</emphasis>&nbsp;:</term>
 	<listitem><simpara>a new <link
 linkend="class-gdkcolor"><classname>gtk.gdk.Color</classname></link>
@@ -158,6 +175,10 @@
       </varlistentry>
     </variablelist>
 
+    <note>
+      <para>Second form of the constructor is available in PyGTK 2.14 and above.</para>
+    </note>
+
     <para>Creates a new <link
 linkend="class-gdkcolor"><classname>gtk.gdk.Color</classname></link> object
 with the color component values specified by <parameter>red</parameter>,
@@ -166,6 +187,9 @@
 value of <parameter>pixel</parameter> will be overwritten when the color is
 allocated.</para>
 
+    <para>Second form of the constructor is analogous to
+      <link linkend="function-gdk--color-parse"><function>gtk.gdk.color_parse</function></link>.</para>
+
   </refsect1>
   
     <refsect1>

Modified: trunk/docs/reference/pygtk-gdkrectangle.xml
==============================================================================
--- trunk/docs/reference/pygtk-gdkrectangle.xml	(original)
+++ trunk/docs/reference/pygtk-gdkrectangle.xml	Sat Aug  2 16:08:41 2008
@@ -101,6 +101,18 @@
 "x" and "y" attributes and the size, by the "width" and "height"
 attributes.</para>
 
+    <para>
+      Starting with PyGTK 2.14 <classname>gtk.gdk.Rectangle</classname> objects are
+      properly comparable.  By Python rules, rectangles (being mutable) are now
+      unhashable.  If you need to use them as dictionary keys, convert rectangle objects
+      to tuples first.  You can convert such tuples back
+      to <classname>gtk.gdk.Rectangle</classname> using the following code:
+    </para>
+
+    <programlisting>
+  rectangle = gtk.gdk.Rectangle(*tuple)
+    </programlisting>
+
   </refsect1>
 
   <refsect1 id="constructor-gdkrectangle">

Modified: trunk/docs/reference/pygtk-gdkregion.xml
==============================================================================
--- trunk/docs/reference/pygtk-gdkregion.xml	(original)
+++ trunk/docs/reference/pygtk-gdkregion.xml	Sat Aug  2 16:08:41 2008
@@ -125,6 +125,12 @@
     linkend="method-gdkgc--set-clip-region"><methodname>gtk.gdk.GC.set_clip_region</methodname>()</link>
     method).</para>
 
+    <para>
+      Starting with PyGTK 2.14 <classname>gtk.gdk.Region</classname> objects are
+      properly comparable.  By Python rules, regions (being mutable) are now
+      unhashable.
+    </para>
+
   </refsect1>
 
   <refsect1 id="constructor-gdkregion">
@@ -228,6 +234,11 @@
       region specified by <parameter>other</parameter> is equal to this
       region.</para>
 
+      <note>
+        <para>Since PyGTK 2.14 Python comparison operator (<literal>==</literal>) can be
+	  used for the same result.</para>
+      </note>
+
     </refsect2>
 
     <refsect2 id="method-gdkregion--point-in">

Modified: trunk/gtk/gdk.override
==============================================================================
--- trunk/gtk/gdk.override	(original)
+++ trunk/gtk/gdk.override	Sat Aug  2 16:08:41 2008
@@ -534,6 +534,37 @@
     return py_rects;
 }
 %%
+override-slot GdkRegion.tp_richcompare
+static PyObject *
+_wrap_pygdk_region_tp_richcompare(PyObject *self, PyObject *other, int op)
+{
+    PyObject *result;
+
+    if (PyObject_TypeCheck(self, &PyGdkRegion_Type)
+        && PyObject_TypeCheck(other, &PyGdkRegion_Type)) {
+        GdkRegion *region1 = pyg_boxed_get(self, GdkRegion);
+        GdkRegion *region2 = pyg_boxed_get(other, GdkRegion);
+
+        switch (op) {
+        case Py_EQ:
+            result = (gdk_region_equal(region1, region2)
+                      ? Py_True : Py_False);
+            break;
+        case Py_NE:
+            result = (!gdk_region_equal(region1, region2)
+                      ? Py_True : Py_False);
+            break;
+        default:
+            result = Py_NotImplemented;
+        }
+    }
+    else
+        result = Py_NotImplemented;
+
+    Py_INCREF(result);
+    return result;
+}
+%%
 override-attr GdkDevice.axes
 static PyObject *
 _wrap_gdk_device__get_axes(PyGObject *self, void *closure)

Modified: trunk/gtk/gdkcolor.override
==============================================================================
--- trunk/gtk/gdkcolor.override	(original)
+++ trunk/gtk/gdkcolor.override	Sat Aug  2 16:08:41 2008
@@ -190,6 +190,37 @@
     return _wrap_gdk_colormap_alloc_color(self, args, kwargs);
 }
 %%
+override-slot GdkColor.tp_richcompare
+static PyObject *
+_wrap_gdk_color_tp_richcompare(PyObject *self, PyObject *other, int op)
+{
+    PyObject *result;
+
+    if (PyObject_TypeCheck(self, &PyGdkColor_Type)
+        && PyObject_TypeCheck(other, &PyGdkColor_Type)) {
+        GdkColor *color1 = pyg_boxed_get(self, GdkColor);
+        GdkColor *color2 = pyg_boxed_get(other, GdkColor);
+
+        switch (op) {
+        case Py_EQ:
+            result = (gdk_color_equal(color1, color2)
+                      ? Py_True : Py_False);
+            break;
+        case Py_NE:
+            result = (!gdk_color_equal(color1, color2)
+                      ? Py_True : Py_False);
+            break;
+        default:
+            result = Py_NotImplemented;
+        }
+    }
+    else
+        result = Py_NotImplemented;
+
+    Py_INCREF(result);
+    return result;
+}
+%%
 override gdk_colormap_query_color kwargs
 static PyObject *
 _wrap_gdk_colormap_query_color(PyGObject *self, PyObject *args,

Modified: trunk/gtk/gdkrectangle.override
==============================================================================
--- trunk/gtk/gdkrectangle.override	(original)
+++ trunk/gtk/gdkrectangle.override	Sat Aug  2 16:08:41 2008
@@ -214,3 +214,36 @@
                             
     return pyg_boxed_new(GDK_TYPE_RECTANGLE, &dest, TRUE, TRUE);
 }
+%%
+override-slot GdkRectangle.tp_richcompare
+static PyObject *
+_wrap_gdk_rectangle_tp_richcompare(PyObject *self, PyObject *other, int op)
+{
+    PyObject *result;
+
+    if (PyObject_TypeCheck(self, &PyGdkRectangle_Type)
+        && PyObject_TypeCheck(other, &PyGdkRectangle_Type)) {
+        GdkRectangle *rect1 = pyg_boxed_get(self, GdkRectangle);
+        GdkRectangle *rect2 = pyg_boxed_get(other, GdkRectangle);
+
+        switch (op) {
+        case Py_EQ:
+            result = (rect1->x == rect2->x && rect1->y == rect2->y
+                      && rect1->width == rect2->width && rect1->height == rect2->height
+                      ? Py_True : Py_False);
+            break;
+        case Py_NE:
+            result = (rect1->x != rect2->x || rect1->y != rect2->y
+                      || rect1->width != rect2->width || rect1->height != rect2->height
+                      ? Py_True : Py_False);
+            break;
+        default:
+            result = Py_NotImplemented;
+        }
+    }
+    else
+        result = Py_NotImplemented;
+
+    Py_INCREF(result);
+    return result;
+}

Modified: trunk/tests/Makefile.am
==============================================================================
--- trunk/tests/Makefile.am	(original)
+++ trunk/tests/Makefile.am	Sat Aug  2 16:08:41 2008
@@ -1,24 +1,26 @@
 EXTRA_DIST = $(tests) common.py runtests.py testmodule.py leak.glade
 
-tests =  \
-	test_actiongroup.py \
-	test_api.py \
-	test_bin.py \
-	test_button.py \
-	test_container.py
-	test_conversion.py \
-	test_dialog.py \
-	test_enum.py \
-	test_gdk.py \
-	test_glade.py \
-	test_liststore.py
-	test_pango.py \
-	test_plug.py \
-	test_radiobutton.py \
-	test_style.py \
-	test_textview.py \
-	test_treeview.py \
-	test_filechooserdialog.py
+tests =						\
+	test_actiongroup.py			\
+	test_api.py				\
+	test_bin.py				\
+	test_button.py				\
+	test_color.py				\
+	test_container.py			\
+	test_conversion.py			\
+	test_dialog.py				\
+	test_enum.py				\
+	test_filechooserdialog.py		\
+	test_gdk.py				\
+	test_glade.py				\
+	test_liststore.py			\
+	test_pango.py				\
+	test_plug.py				\
+	test_radiobutton.py			\
+	test_rectangle.py			\
+	test_style.py				\
+	test_textview.py			\
+	test_treeview.py
 
 GTK_PY_FILES = __init__.py _lazyutils.py compat.py deprecation.py keysyms.py
 

Added: trunk/tests/test_color.py
==============================================================================
--- (empty file)
+++ trunk/tests/test_color.py	Sat Aug  2 16:08:41 2008
@@ -0,0 +1,60 @@
+# -*- Mode: Python -*-
+
+import unittest
+
+from common import gtk
+
+
+class Tests(unittest.TestCase):
+
+    def test_constructor(self):
+        """ Test GdkColor creation """
+
+        c = gtk.gdk.Color(1, 2, 3)
+        self.assertEqual(c.red, 1)
+        self.assertEqual(c.green, 2)
+        self.assertEqual(c.blue, 3)
+
+        c = gtk.gdk.Color(pixel=0xffff)
+        self.assertEqual(c.pixel, 0xffff)
+
+        c = gtk.gdk.Color(pixel=0xffffL)
+        self.assertEqual(c.pixel, 0xffff)
+
+        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 test_equal(self):
+        self.assertEqual(gtk.gdk.Color(0, 0, 0), gtk.gdk.Color(0, 0, 0))
+        self.assertEqual(gtk.gdk.Color(100, 200, 300), gtk.gdk.Color(100, 200, 300))
+        self.assertEqual(gtk.gdk.Color('#abc'), gtk.gdk.Color('#aabbcc'))
+        self.assertEqual(gtk.gdk.Color('#100020003000'), gtk.gdk.Color(0x1000, 0x2000, 0x3000))
+
+    def test_not_equal(self):
+        self.assertNotEqual(gtk.gdk.Color('red'), gtk.gdk.Color('blue'))
+        self.assertNotEqual(gtk.gdk.Color(1, 0, 0), gtk.gdk.Color(0, 0, 0))
+        self.assertNotEqual(gtk.gdk.Color(0, 1, 0), gtk.gdk.Color(0, 0, 0))
+        self.assertNotEqual(gtk.gdk.Color(0, 0, 1), gtk.gdk.Color(0, 0, 0))
+
+    def test_non_hashable(self):
+        self.assertRaises(TypeError, lambda: hash(gtk.gdk.Color()))
+
+        def dict_key():
+            {} [gtk.gdk.Color()] = 'must raise'
+        self.assertRaises(TypeError, dict_key)
+
+
+if __name__ == '__main__':
+    unittest.main()

Modified: trunk/tests/test_conversion.py
==============================================================================
--- trunk/tests/test_conversion.py	(original)
+++ trunk/tests/test_conversion.py	Sat Aug  2 16:08:41 2008
@@ -52,35 +52,6 @@
             self.assertEqual(entry.get_property('invisible_char'),
                              valid_value, valid_value)
 
-    def testColorCreation(self):
-        """ Test GdkColor creation """
-
-        c = gtk.gdk.Color(1, 2, 3)
-        self.assertEqual(c.red, 1)
-        self.assertEqual(c.green, 2)
-        self.assertEqual(c.blue, 3)
-
-        c = gtk.gdk.Color(pixel=0xffff)
-        self.assertEqual(c.pixel, 0xffff)
-
-        c = gtk.gdk.Color(pixel=0xffffL)
-        self.assertEqual(c.pixel, 0xffff)
-
-        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)

Added: trunk/tests/test_rectangle.py
==============================================================================
--- (empty file)
+++ trunk/tests/test_rectangle.py	Sat Aug  2 16:08:41 2008
@@ -0,0 +1,28 @@
+# -*- Mode: Python -*-
+
+import unittest
+
+from common import gtk
+
+
+class Tests(unittest.TestCase):
+
+    def test_equal(self):
+        self.assertEqual(gtk.gdk.Rectangle(0, 0, 1, 1), gtk.gdk.Rectangle(0, 0, 1, 1))
+
+    def test_not_equal(self):
+        self.assertNotEqual(gtk.gdk.Rectangle(1, 0, 10, 10), gtk.gdk.Rectangle(0, 0, 10, 10))
+        self.assertNotEqual(gtk.gdk.Rectangle(0, 1, 10, 10), gtk.gdk.Rectangle(0, 0, 10, 10))
+        self.assertNotEqual(gtk.gdk.Rectangle(0, 0, 11, 10), gtk.gdk.Rectangle(0, 0, 10, 10))
+        self.assertNotEqual(gtk.gdk.Rectangle(0, 0, 10, 11), gtk.gdk.Rectangle(0, 0, 10, 10))
+
+    def test_non_hashable(self):
+        self.assertRaises(TypeError, lambda: hash(gtk.gdk.Rectangle()))
+
+        def dict_key():
+            {} [gtk.gdk.Rectangle()] = 'must raise'
+        self.assertRaises(TypeError, dict_key)
+
+
+if __name__ == '__main__':
+    unittest.main()



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