[pygobject] Fix repr() of GLib.Variant



commit a52245381fab3c2aebd330cc9c5e717a93c9607d
Author: Martin Pitt <martinpitt gnome org>
Date:   Wed Jan 23 14:28:42 2013 +0100

    Fix repr() of GLib.Variant
    
    When using a standard constructor like GLib.Variant.new_*(), the object does
    not have a format_string property, and previously repr() would crash. Fall back
    to get_type_string() instead.
    
    Also drop the unintended type annotations in repr().

 gi/overrides/GLib.py         |    6 +++++-
 tests/test_overrides_glib.py |   15 ++++++++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index 6ae5dba..28a35e2 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -220,7 +220,11 @@ class Variant(GLib.Variant):
         return self.print_(True)
 
     def __repr__(self):
-        return "GLib.Variant('%s', %s)" % (self.format_string, self.print_(True))
+        if hasattr(self, 'format_string'):
+            f = self.format_string
+        else:
+            f = self.get_type_string()
+        return "GLib.Variant('%s', %s)" % (f, self.print_(False))
 
     def __eq__(self, other):
         try:
diff --git a/tests/test_overrides_glib.py b/tests/test_overrides_glib.py
index 3799454..6bad3e0 100644
--- a/tests/test_overrides_glib.py
+++ b/tests/test_overrides_glib.py
@@ -463,6 +463,19 @@ class TestGVariant(unittest.TestCase):
         assert_equals_bool('v', GLib.Variant('i', 1))
 
     def test_repr(self):
+        # with C constructor
+        v = GLib.Variant.new_uint32(42)
+        self.assertEqual(repr(v), "GLib.Variant('u', 42)")
+
+        # with override constructor
         v = GLib.Variant('(is)', (1, 'somestring'))
-        self.assertEqual(str(v), "(1, 'somestring')")
         self.assertEqual(repr(v), "GLib.Variant('(is)', (1, 'somestring'))")
+
+    def test_str(self):
+        # with C constructor
+        v = GLib.Variant.new_uint32(42)
+        self.assertEqual(str(v), 'uint32 42')
+
+        # with override constructor
+        v = GLib.Variant('(is)', (1, 'somestring'))
+        self.assertEqual(str(v), "(1, 'somestring')")



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