[pygobject/pygobject-3-4] Fix Gdk.Atom to have a proper str() and repr()



commit 1a460f420f8ced65d24f24977fc58186962b4ec8
Author: Martin Pitt <martinpitt gnome org>
Date:   Tue Nov 13 12:56:11 2012 +0100

    Fix Gdk.Atom to have a proper str() and repr()
    
    Gdk.Atom is not proper GType'd class, so we cannot override the whole class.
    Just override its __str__() and __repr__() methods so that printing atoms shows
    something sensible. For nameless/invalid atoms, fall back to the old
    <void at 0xdeadbeef> output to help with debugging.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=678620

 gi/overrides/Gdk.py |   21 +++++++++++++++++++++
 tests/test_atoms.py |   12 ++++++++++++
 2 files changed, 33 insertions(+), 0 deletions(-)
---
diff --git a/gi/overrides/Gdk.py b/gi/overrides/Gdk.py
index 20ef910..0571c40 100644
--- a/gi/overrides/Gdk.py
+++ b/gi/overrides/Gdk.py
@@ -345,6 +345,27 @@ def color_parse(color):
         return None
     return color
 
+
+# Note, we cannot override the entire class as Gdk.Atom has no gtype, so just
+# hack some individual methods
+def _gdk_atom_str(atom):
+    n = atom.name()
+    if n:
+        return n
+    return Gdk.Atom.__str__(n)
+
+
+def _gdk_atom_repr(atom):
+    n = atom.name()
+    if n:
+        return 'Gdk.Atom<%s>' % n
+    return Gdk.Atom.__str__(n)
+
+
+Gdk.Atom.__str__ = _gdk_atom_str
+Gdk.Atom.__repr__ = _gdk_atom_repr
+
+
 # constants
 if Gdk._version >= '3.0':
     SELECTION_PRIMARY = Gdk.atom_intern('PRIMARY', True)
diff --git a/tests/test_atoms.py b/tests/test_atoms.py
index a59d15a..449fcda 100644
--- a/tests/test_atoms.py
+++ b/tests/test_atoms.py
@@ -13,6 +13,18 @@ class TestGdkAtom(unittest.TestCase):
         atom = Gdk.Atom.intern('my_string', False)
         self.assertEqual(atom.name(), 'my_string')
 
+    def test_str(self):
+        atom = Gdk.Atom.intern('my_string', False)
+        self.assertEqual(str(atom), 'my_string')
+
+        self.assertEqual(str(Gdk.SELECTION_CLIPBOARD), 'CLIPBOARD')
+
+    def test_repr(self):
+        atom = Gdk.Atom.intern('my_string', False)
+        self.assertEqual(repr(atom), 'Gdk.Atom<my_string>')
+
+        self.assertEqual(repr(Gdk.SELECTION_CLIPBOARD), 'Gdk.Atom<CLIPBOARD>')
+
     def test_in_single(self):
         a_selection = Gdk.Atom.intern('test_clipboard', False)
         clipboard = Gtk.Clipboard.get(a_selection)



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