summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Feltman <sfeltman@src.gnome.org>2014-01-07 09:16:54 -0800
committerSimon Feltman <sfeltman@src.gnome.org>2014-01-07 09:30:37 -0800
commit9b02b29016958791dfa9d7ebfc6c2ec44ab5690d (patch)
tree8fe7e0d45bd3a96c13d67b332c1f842b7795401c
parentf6a87935596a3b59c238a5572b288f34691b53d1 (diff)
downloadpygobject-9b02b29016958791dfa9d7ebfc6c2ec44ab5690d.tar.gz
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__
-rw-r--r--gi/overrides/Gdk.py10
-rw-r--r--tests/test_atoms.py7
-rw-r--r--tests/test_overrides_gdk.py9
3 files changed, 19 insertions, 7 deletions
diff --git a/gi/overrides/Gdk.py b/gi/overrides/Gdk.py
index d9797465..9d0ba7d3 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 1561dbd4..18f8d098 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 648597ea..90e2fdef 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)