[pygobject] Fix Gdk.Atom to have a proper str() and repr()
- From: Martin Pitt <martinpitt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Fix Gdk.Atom to have a proper str() and repr()
- Date: Tue, 13 Nov 2012 11:57:48 +0000 (UTC)
commit 671361841de797ef62b59d1d7568fc3d431898c7
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 653b3df..df84e45 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]