pygobject3/gdk-atom-1.patch
2012-11-26 10:14:06 -06:00

75 lines
2.3 KiB
Diff

From c69fca1cd401bb6c9cf360076dc3027979a18cd9 Mon Sep 17 00:00:00 2001
From: Martin Pitt <martinpitt@gnome.org>
Date: Tue, 13 Nov 2012 12:56:11 +0100
Subject: [PATCH 1/3] 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(+)
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)
--
1.7.11.7