J. Taylor
J. Taylor

Reputation: 4855

Python 3 / PyGObject: Attempting to pack GTK.Box() is causing segfault

Running the following code in Python 3 is giving me a segfault. Is this a bug, or am I doing something I'm not supposed to do?

I've marked the location where the segfault is occuring with a DEBUG print statement (in the TabbedWebkitBrowser class):

from gi.repository import Gtk, WebKit

class UI:
    def __init__(self, config = dict()):
        self.config = config

        self.window = MainWindow(self.config)

        self.run()


    def run(self):
        self.window.show_all()
        Gtk.main()

class MainWindow(Gtk.Window):
    def __init__(self, config = dict()):
        self.title = config['title'] or "Untitled"

        Gtk.Window.__init__(self, title = self.title)
        self.set_size_request(800, 600)    
        self.connect("delete-event", Gtk.main_quit)

        top_vbox= Gtk.Box(orientation = Gtk.Orientation.VERTICAL, spacing = 0)
        self.add(top_vbox)
        main_header = Gtk.Box()
        main_content = Gtk.HBox()
        main_footer = Gtk.Box()

        top_vbox.pack_start(main_header, expand = False,  fill = False, padding = 0)
        top_vbox.pack_start(main_content, expand = True, fill = True, padding = 0)
        top_vbox.pack_start(main_footer, expand = False, fill = False, padding = 0)

        left_sidebar = Gtk.Box()
        browser = TabbedWebKitBrowser()
        right_sidebar = Gtk.Box() 

        main_content.pack_start(left_sidebar, expand = False,fill = False, padding = 0)
        main_content.pack_start(browser, expand = True, fill = True, padding = 0)
        main_content.pack_start(right_sidebar, expand = False, fill = False, padding = 0)


class TabbedWebKitBrowser(Gtk.VBox):
    def __init__(self):

        self.navbar = Gtk.HBox()

        # This is a tabbed collection of WebKit.WebView instances 
        self.tabs = Gtk.Notebook() 

        wk = WebKit.WebView()
        self.tabs.prepend_page(wk)
        wk.load_uri('http://www.interference.cc')

        self.tabs.show_all()
        print("DEBUG: This .pack_start() is where the segfault is occuring ...")        
        self.pack_start(self.navbar,  expand = True, fill = True, padding = 0)
        print("DEBUG: Didn't make it to this point ...")
        self.pack_start(self.tabs, expand = True, fill = True, padding = 0) 

config = {'title': 'Test program ...'}
x = UI(config)

I ran the program in gdb and did a backtrace after the segfault. Here is what I got:

Starting program: /usr/bin/python3 UI.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
DEBUG: This .pack_start() is where the segfault is occuring ...
[New Thread 0x7fff97fff700 (LWP 14066)]
[New Thread 0x7fff9ce91700 (LWP 14065)]
[New Thread 0x7fff9e3d0700 (LWP 14064)]
[New Thread 0x7fffdebd3700 (LWP 14063)]
[New Thread 0x7fffed4d4700 (LWP 14062)]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff44400bd in ?? () from /usr/lib/x86_64-linux-gnu/libgtk-3.so.0
(gdb) bt
#0  0x00007ffff44400bd in ?? () from /usr/lib/x86_64-linux-gnu/libgtk-3.so.0
#1  0x00007ffff5d6ed90 in ffi_call_unix64 () from /usr/lib/x86_64-linux-gnu/libffi.so.6
#2  0x00007ffff5d6e7f8 in ffi_call () from /usr/lib/x86_64-linux-gnu/libffi.so.6
#3  0x00007ffff6732ae4 in ?? () from /usr/lib/python3/dist-packages/gi/_gi.cpython-34m-x86_64-linux-gnu.so
#4  0x00007ffff67343e8 in ?? () from /usr/lib/python3/dist-packages/gi/_gi.cpython-34m-x86_64-linux-gnu.so
#5  0x00007ffff672859e in ?? () from /usr/lib/python3/dist-packages/gi/_gi.cpython-34m-x86_64-linux-gnu.so
#6  0x0000000000504127 in PyEval_EvalFrameEx ()
#7  0x00000000004c9fb5 in ?? ()
#8  0x000000000050ea0b in ?? ()
#9  0x0000000000564024 in ?? ()
#10 0x00000000005997ea in ?? ()
#11 0x0000000000504127 in PyEval_EvalFrameEx ()
#12 0x00000000004c9fb5 in ?? ()
#13 0x000000000050ea0b in ?? ()
#14 0x0000000000564024 in ?? ()
#15 0x00000000005997ea in ?? ()
#16 0x0000000000504127 in PyEval_EvalFrameEx ()
#17 0x00000000004c9fb5 in ?? ()
#18 0x000000000050ea0b in ?? ()
#19 0x0000000000564024 in ?? ()
#20 0x00000000005997ea in ?? ()
#21 0x0000000000504127 in PyEval_EvalFrameEx ()
#22 0x00000000005a9cb5 in PyEval_EvalCodeEx ()
#23 0x00000000005e7105 in ?? ()
#24 0x00000000005e71c9 in PyRun_FileExFlags ()
#25 0x00000000005e79aa in PyRun_SimpleFileExFlags ()
#26 0x00000000005fb69d in Py_Main ()
#27 0x00000000004c2e7f in main ()

Any ideas?

Upvotes: 1

Views: 246

Answers (1)

elya5
elya5

Reputation: 2258

You forgot to call the __init__ method of the parent of the TabbedWebKitBrowser class. So this should work:

class TabbedWebkitBrowser(Gtk.VBox):
    def __init__(self):
        super().__init__()  # or Gtk.Vbox.__init__(self) if you like
        self.navbar = Gtk.HBox()
        ...

Upvotes: 2

Related Questions