[PyQt] Random crashes in QWebView

Kay D dtc.kay at gmail.com
Fri Apr 2 23:04:54 BST 2010


Hi Everyone

I've created a python wrapper for qt browser, but if I run it many times,
it'll crash in few cases with segfaults like following:
[ 9939.720253] python[13090] general protection ip:7f43c853c96e
sp:7fffbe6d0de0 error:0 in QtCore.so[7f43c8406000+188000]
What's wrong with the code? Timers? Javascript? Something else? Or it's just
unstable Qt browser?

Ubuntu 9.10 (karmic); PyQt version 4.6-1; Qt 4.5.3really4.5.2-0ubuntu1
---------------------------------------------------- wrapper
---------------------------------------------------------------
import logging
from PyQt4.QtCore import SIGNAL, QObject, QUrl, QTimer, QString
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView, QWebPage, QWebSettings

class WebKitPage(QWebPage):
    def __init__(self, user_agent):
        QWebPage.__init__(self)
        self.user_agent = user_agent
    def userAgentForUrl(self, url):
        return self.user_agent
    def javaScriptConsoleMessage(self, message, line, source):
        logging.info("WebKitPage > js line %s at %s, message %s", line,
source, message)

class WebKitBrowser:
    def __init__(self, parsers):
        self.app = QApplication([])
        self.url = self.timer = None
        self.parsers = parsers
        self.page = WebKitPage("Test user agent")
        self.browser = QWebView()
        QObject.connect(self.browser, SIGNAL("loadFinished(bool)"),
self.load_finished)
        self.browser.get_js_str = self.get_js_str
        self.browser.show()
        self.browser.resize(800,600)
        self.browser.setPage(self.page)
        self.browser.settings().setAttribute(QWebSettings.AutoLoadImages,
False)

    def get_js_str(self, frame, js, arg = None):
        jsqs = QString(js)
        if arg != None: jsqs = jsqs.arg(arg)
        return unicode(frame.evaluateJavaScript(jsqs).toString())

    def timer_shot(self):
        self.timer_stop()
        self.browser.stop()

    def timer_start(self, timeout):
        self.timer = QTimer()
        QObject.connect(self.timer, SIGNAL("timeout()"), self.timer_shot)
        self.timer.start(timeout * 1000)

    def timer_stop(self):
        if self.timer:
            self.timer.stop()
            self.timer = None

    def load_finished(self, stat):
        logging.info("WebKitBrowser > Load finished")
        self.timer_stop()
        self.status = stat and "success" or "timeout"
        for parser in self.parsers:
            parser.run(self.browser)
        self.real_url =
self.browser.get_js_str(self.browser.page().mainFrame(), "location.href")
        logging.info("WebKitBrowser > Status %s, Real url %s", self.status,
self.real_url)
        self.app.exit()

    def get(self, url, timeout = 60):
        logging.info("WebKitBrowser > Loading %s", url)
        self.browser.stop()
        self.status = "loading"
        self.timer_start(timeout)
        self.browser.load(QUrl(url))
        self.app.exec_()
        return self.status

class LinkParser:
    def extract_links(self, frame):
        frame.evaluateJavaScript("var anchors =
document.getElementsByTagName('a');")
        count, ok = frame.evaluateJavaScript("anchors.length;").toInt()
        for i in range(0, count):
            logging.info("LinkParser > %s", self.browser.get_js_str(frame,
"anchors[%1].href", i))

    def run(self, browser):
        self.browser = browser
        self.extract_links(self.browser.page().mainFrame())
        for frame in self.browser.page().mainFrame().childFrames():
            self.extract_links(frame)

if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    b = WebKitBrowser([ LinkParser() ])
    b.get("http://google.com/")
    b.get("http://youtube.com/", 1)
    b.get("http://google.com/")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100403/4d56b47e/attachment-0001.html>


More information about the PyQt mailing list