【问题标题】:Python 3 / PyGObject: Attempting to pack GTK.Box() is causing segfaultPython 3 / PyGObject:尝试打包 GTK.Box() 导致段错误
【发布时间】:2015-09-09 17:50:12
【问题描述】:

在 Python 3 中运行以下代码会给我一个段错误。这是一个错误,还是我做了一些我不应该做的事情?

我已经使用 DEBUG 打印语句(在 TabbedWebkitBrowser 类中)标记了发生段错误的位置:

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)

我在 gdb 中运行程序并在段错误后进行了回溯。这是我得到的:

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 ()

有什么想法吗?

【问题讨论】:

    标签: python user-interface segmentation-fault gtk pygobject


    【解决方案1】:

    您忘记调用 TabbedWebKitBrowser 类的父类的 __init__ 方法。所以这应该有效:

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

    【讨论】:

    • 啊,谢谢!就是这样。您是否知道为什么这样做会导致段错误而不是引发某种异常/错误?
    • 对不起,我不知道。可能是因为 gtk+ 是用 C 编写的,并且只有 python 绑定并且绑定缺少一些检查。
    • 这是有道理的。我可能会继续将它作为一个小错误提交,因为我猜这些检查可能应该存在,而不是导致段错误。
    猜你喜欢
    • 1970-01-01
    • 2016-07-01
    • 2015-05-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2016-07-19
    相关资源
    最近更新 更多