[pygobject/gtk-window-check-initialized: 5/5] gtk: raise in case Gtk.Window is instantiated after init failed. See #298



commit 624ea3ec615ad82d5138eb034e7c02c79608441e
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Sat Jan 26 21:31:02 2019 +0100

    gtk: raise in case Gtk.Window is instantiated after init failed. See #298
    
    gtk crashes if gtk_init() fails and we can't do much about it.
    This assumes the Gtk.Window() is the first instantiated widget and
    raises an exception in case init failed.
    
    We already had such a check but it was removed in 86a37d67455d
    for some reason (maybe because it only helps in some cases)

 gi/overrides/Gtk.py | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index ba0a71dc..b5c2dd06 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -506,11 +506,19 @@ __all__.append('Builder')
 # NOTE: This must come before any other Window/Dialog subclassing, to ensure
 # that we have a correct inheritance hierarchy.
 
+_window_init = deprecated_init(Gtk.Window.__init__,
+                               arg_names=('type',),
+                               category=PyGTKDeprecationWarning,
+                               stacklevel=3)
+
 
 class Window(Gtk.Window):
-    __init__ = deprecated_init(Gtk.Window.__init__,
-                               arg_names=('type',),
-                               category=PyGTKDeprecationWarning)
+    def __init__(self, *args, **kwargs):
+        if not initialized:
+            raise RuntimeError(
+                "Gtk couldn't be initialized. "
+                "Use Gtk.init_check() if you want to handle this case.")
+        _window_init(self, *args, **kwargs)
 
 
 Window = override(Window)
@@ -1627,7 +1635,7 @@ if Gtk._version in ("2.0", "3.0"):
     __all__.append('stock_lookup')
 
 if Gtk._version == "4.0":
-    Gtk.init_check()
+    initialized = Gtk.init_check()
 else:
     initialized, argv = Gtk.init_check(sys.argv)
     sys.argv = list(argv)


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