[pygobject] GLib.Variant: Fix repr(), add proper str()



commit e1e849d1a9af77c29ee35971db8d439bac60d573
Author: Martin Pitt <martinpitt gnome org>
Date:   Wed Jul 4 08:35:16 2012 +0200

    GLib.Variant: Fix repr(), add proper str()
    
    Fix the GLib.Variant override's repr() after commit 16280d6985. Also add a
    proper __str__() method, and tests for both.
    
    Thanks to Rul Matos for spotting this!
    
    https://bugzilla.gnome.org/show_bug.cgi?id=679336

 gi/overrides/GLib.py    |    6 +++++-
 tests/test_overrides.py |    5 +++++
 2 files changed, 10 insertions(+), 1 deletions(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index 1d15aff..c41f523 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -169,13 +169,17 @@ class Variant(GLib.Variant):
         (v, rest_format, _) = creator._create(format_string, [value])
         if rest_format:
             raise TypeError('invalid remaining format string: "%s"' % rest_format)
+        v.format_string = format_string
         return v
 
     def __del__(self):
         self.unref()
 
+    def __str__(self):
+        return self.print_(True)
+
     def __repr__(self):
-        return '<GLib.Variant(%s)>' % getattr(self, 'print')(True)
+        return "GLib.Variant('%s', %s)" % (self.format_string, self.print_(True))
 
     def __eq__(self, other):
         try:
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 2968290..f6685b2 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -464,6 +464,11 @@ class TestGLib(unittest.TestCase):
         assert_equals_bool('v', GLib.Variant('i', 0))
         assert_equals_bool('v', GLib.Variant('i', 1))
 
+    def test_variant_repr(self):
+        v = GLib.Variant('(is)', (1, 'somestring'))
+        self.assertEqual(str(v), "(1, 'somestring')")
+        self.assertEqual(repr(v), "GLib.Variant('(is)', (1, 'somestring'))")
+
 
 class TestPango(unittest.TestCase):
 



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