[pygobject] overrides: Fix __repr__ for various Gdk structs



commit 9b02b29016958791dfa9d7ebfc6c2ec44ab5690d
Author: Simon Feltman <sfeltman src gnome org>
Date:   Tue Jan 7 09:16:54 2014 -0800

    overrides: Fix __repr__ for various Gdk structs
    
    Change __repr__ overrides for Gdk.Color, Gdk.RGBA, and Gdk.Atom to return a
    string reprentation that is valid Python given an expected environment.
    See: http://docs.python.org/2/reference/datamodel.html#object.__repr__

 gi/overrides/Gdk.py         |   10 +++++-----
 tests/test_atoms.py         |    7 +++++--
 tests/test_overrides_gdk.py |    9 +++++++++
 3 files changed, 19 insertions(+), 7 deletions(-)
---
diff --git a/gi/overrides/Gdk.py b/gi/overrides/Gdk.py
index d979746..9d0ba7d 100644
--- a/gi/overrides/Gdk.py
+++ b/gi/overrides/Gdk.py
@@ -44,7 +44,7 @@ class Color(Gdk.Color):
         return self.equal(other)
 
     def __repr__(self):
-        return '<Gdk.Color(red=%d, green=%d, blue=%d)>' % (self.red, self.green, self.blue)
+        return 'Gdk.Color(red=%d, green=%d, blue=%d)' % (self.red, self.green, self.blue)
 
     red_float = property(fget=lambda self: self.red / float(self.MAX_VALUE),
                          fset=lambda self, v: setattr(self, 'red', int(v * self.MAX_VALUE)))
@@ -84,7 +84,7 @@ if Gdk._version == '3.0':
             return self.equal(other)
 
         def __repr__(self):
-            return '<Gdk.Color(red=%f, green=%f, blue=%f, alpha=%f)>' % (self.red, self.green, self.blue, 
self.alpha)
+            return 'Gdk.RGBA(red=%f, green=%f, blue=%f, alpha=%f)' % (self.red, self.green, self.blue, 
self.alpha)
 
         def __iter__(self):
             """Iterator which allows easy conversion to tuple and list types."""
@@ -121,7 +121,7 @@ if Gdk._version == '2.0':
             self.height = height
 
         def __repr__(self):
-            return '<Gdk.Rectangle(x=%d, y=%d, width=%d, height=%d)>' % (self.x, self.y, self.height, 
self.width)
+            return 'Gdk.Rectangle(x=%d, y=%d, width=%d, height=%d)' % (self.x, self.y, self.height, 
self.width)
 
     Rectangle = override(Rectangle)
     __all__.append('Rectangle')
@@ -332,9 +332,9 @@ def _gdk_atom_str(atom):
 def _gdk_atom_repr(atom):
     n = atom.name()
     if n:
-        return 'Gdk.Atom<%s>' % n
+        return 'Gdk.Atom.intern("%s", False)' % n
     # fall back to atom index
-    return 'Gdk.Atom<%i>' % hash(atom)
+    return '<Gdk.Atom(%i)>' % hash(atom)
 
 
 Gdk.Atom.__str__ = _gdk_atom_str
diff --git a/tests/test_atoms.py b/tests/test_atoms.py
index 1561dbd..18f8d09 100644
--- a/tests/test_atoms.py
+++ b/tests/test_atoms.py
@@ -20,10 +20,13 @@ class TestGdkAtom(unittest.TestCase):
         self.assertEqual(str(Gdk.SELECTION_CLIPBOARD), 'CLIPBOARD')
 
     def test_repr(self):
+        # __repr__ should generate a string which is parsable when possible
+        # http://docs.python.org/2/reference/datamodel.html#object.__repr__
         atom = Gdk.Atom.intern('my_string', False)
-        self.assertEqual(repr(atom), 'Gdk.Atom<my_string>')
+        self.assertEqual(repr(atom), 'Gdk.Atom.intern("my_string", False)')
+        self.assertEqual(eval(repr(atom)), atom)
 
-        self.assertEqual(repr(Gdk.SELECTION_CLIPBOARD), 'Gdk.Atom<CLIPBOARD>')
+        self.assertEqual(repr(Gdk.SELECTION_CLIPBOARD), 'Gdk.Atom.intern("CLIPBOARD", False)')
 
     def test_in_single(self):
         a_selection = Gdk.Atom.intern('test_clipboard', False)
diff --git a/tests/test_overrides_gdk.py b/tests/test_overrides_gdk.py
index 648597e..90e2fde 100644
--- a/tests/test_overrides_gdk.py
+++ b/tests/test_overrides_gdk.py
@@ -148,3 +148,12 @@ class TestGdk(unittest.TestCase):
         self.assertEqual(c.green, 65535)
         self.assertEqual(c.blue, 32896)
         self.assertEqual(Gdk.color_parse('bogus'), None)
+
+    def test_color_representations(self):
+        # __repr__ should generate a string which is parsable when possible
+        # http://docs.python.org/2/reference/datamodel.html#object.__repr__
+        color = Gdk.Color(red=65535, green=32896, blue=1)
+        self.assertEqual(eval(repr(color)), color)
+
+        rgba = Gdk.RGBA(red=1.0, green=0.8, blue=0.6, alpha=0.4)
+        self.assertEqual(eval(repr(rgba)), rgba)


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