[pygobject] Do not change constructor-only "type" Window property



commit de4aa426002eeb09a060f8fd70bd6cb25a17766a
Author: Martin Pitt <martinpitt gnome org>
Date:   Mon Jun 25 15:06:47 2012 +0200

    Do not change constructor-only "type" Window property
    
    When reading a Gtk.Window subclass from a GtkBuilder object, the object's
    properties are already set at __init__ time. Do not try to set it again, to
    avoid a warning.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=678510

 gi/overrides/Gtk.py     |    8 +++++++-
 tests/test_overrides.py |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 1960297..ff4de24 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -396,7 +396,13 @@ __all__.append('Builder')
 
 class Window(Gtk.Window):
     def __init__(self, type=Gtk.WindowType.TOPLEVEL, **kwds):
-        Gtk.Window.__init__(self, type=type, **kwds)
+        # type is a construct-only property; if it is already set (e. g. by
+        # GtkBuilder), do not try to set it again and just ignore it
+        try:
+            self.get_property('type')
+            Gtk.Window.__init__(self, **kwds)
+        except TypeError:
+            Gtk.Window.__init__(self, type=type, **kwds)
 
 Window = override(Window)
 __all__.append('Window')
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 63eaeb8..2968290 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -758,6 +758,42 @@ class TestGtk(unittest.TestCase):
         self.assertEqual(signal_checker.sentinel, 4)
         self.assertEqual(signal_checker.after_sentinel, 2)
 
+    def test_window(self):
+        # standard Window
+        w = Gtk.Window()
+        self.assertEqual(w.get_property('type'), Gtk.WindowType.TOPLEVEL)
+
+        # type works as keyword argument
+        w = Gtk.Window(type=Gtk.WindowType.POPUP)
+        self.assertEqual(w.get_property('type'), Gtk.WindowType.POPUP)
+
+        # pygtk compatible positional argument
+        w = Gtk.Window(Gtk.WindowType.POPUP)
+        self.assertEqual(w.get_property('type'), Gtk.WindowType.POPUP)
+
+        class TestWindow(Gtk.Window):
+            __gtype_name__ = "TestWindow"
+
+        # works from builder
+        builder = Gtk.Builder()
+        builder.add_from_string('''
+<interface>
+  <object class="GtkWindow" id="win">
+    <property name="type">popup</property>
+  </object>
+  <object class="TestWindow" id="testwin">
+  </object>
+  <object class="TestWindow" id="testpop">
+    <property name="type">popup</property>
+  </object>
+</interface>''')
+        self.assertEqual(builder.get_object('win').get_property('type'),
+                         Gtk.WindowType.POPUP)
+        self.assertEqual(builder.get_object('testwin').get_property('type'),
+                         Gtk.WindowType.TOPLEVEL)
+        self.assertEqual(builder.get_object('testpop').get_property('type'),
+                         Gtk.WindowType.POPUP)
+
     def test_dialogs(self):
         self.assertEqual(Gtk.Dialog, overrides.Gtk.Dialog)
         self.assertEqual(Gtk.AboutDialog, overrides.Gtk.AboutDialog)



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