GNOME Bugzilla – Bug 692300
allow skipping automatic Gtk.init_check
Last modified: 2013-01-30 07:15:10 UTC
Executive summary: People want some option to allow "import Gtk" to not cause the side effect of attempting to connect to X. See https://bugzilla.redhat.com/show_bug.cgi?id=902401 Previous discussion from pygtk: https://bugzilla.gnome.org/show_bug.cgi?id=157422 https://bugzilla.gnome.org/show_bug.cgi?id=316877
I don't quite like this myself, but we can't just drop it (as suggested in the downstream bug) as that would break pretty much every Python GTK program out there. Oh the joy of backwards compatibility... I'm fine with testing an environment variable, though. That might be something like $GTK_SKIP_AUTOINIT, or we could even go as far as skipping the initialization if we don't have a $DISPLAY?
Looking for DISPLAY might be a pragmatic solution, indeed
On the other hand looking at DISPLAY would not be a cross platform way to skip init... but maybe that's not and actual problem
Paolo: Oh, you mean if you run GTK on e. g. a framebuffer or Wayland instead of X.org? Good point. I just thought about this again. What we really do need to keep for compatibility is the call to Gtk.init_check(), but we could drop the exception raising if it fails. If programs really care they could check Gtk.initialized after importing. The main drawback is that the output becomes really unintelligible if you try and start a Gtk program without a $DISPLAY. You get a neverending stream of Gtk-CRITICALS and then a segfault, but no really well-defined exception or failure mode. So it would help if we moved the "initialized" check later. I tried to add it to Gtk.main(), but that usually comes way too late. A reasonable compromise might be to move it to Gtk.Window.__init__()? That might still cause a few warnings if some widgets are built before that, but it limits the performance impact having a Python constructor override and the initialized check to Windows instead of putting it on all widgets.
Created attachment 234169 [details] [review] Do not immediately initialize Gdk and Gtk on import What do people think about this patch?
Wonder if the check could be done somewhere in the main loop, as the event loop is a (bit) more fundamental part of Gtk+ apps than GtkWindow.
> Wonder if the check could be done somewhere in the main loop That's what I tried at first indeed, but I got tons of criticals and a segfault during initializing widgets, way before the program got to the main loop.
Review of attachment 234169 [details] [review]: The patch seems fine. The important thing is we can keep compatibility without having an exception raised during module import and we avoid as many Gtk-CRITICALS as possible.
Comment on attachment 234169 [details] [review] Do not immediately initialize Gdk and Gtk on import OK, thanks for reviewing! Let's just go with that for now, and see how it goes.