It'll crash at random point 6 out of 100 times on my Ubuntu desktop. It's not supposed to crash, so I'm asking what I'm doing wrong.<br><br><div class="gmail_quote">On Sat, Apr 3, 2010 at 3:05 AM, patx <span dir="ltr"><<a href="mailto:patx44@gmail.com">patx44@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">not really helping just a question to the author:<br><br>it crashes right away? is it supposed to or?<br>
<br><div class="gmail_quote"><div><div></div><div class="h5">On Fri, Apr 2, 2010 at 6:04 PM, Kay D <span dir="ltr"><<a href="mailto:dtc.kay@gmail.com" target="_blank">dtc.kay@gmail.com</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="h5">Hi Everyone<br><br>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:<br>
[ 9939.720253] python[13090] general protection ip:7f43c853c96e sp:7fffbe6d0de0 error:0 in QtCore.so[7f43c8406000+188000]<br>
What's wrong with the code? Timers? Javascript? Something else? Or it's just unstable Qt browser?<br><br>Ubuntu 9.10 (karmic); PyQt version 4.6-1; Qt 4.5.3really4.5.2-0ubuntu1<br>---------------------------------------------------- wrapper ---------------------------------------------------------------<br>
import logging<br>from PyQt4.QtCore import SIGNAL, QObject, QUrl, QTimer, QString<br>from PyQt4.QtGui import QApplication<br>from PyQt4.QtWebKit import QWebView, QWebPage, QWebSettings<br><br>class WebKitPage(QWebPage):<br>
def __init__(self, user_agent):<br> QWebPage.__init__(self)<br> self.user_agent = user_agent<br> def userAgentForUrl(self, url):<br> return self.user_agent<br> def javaScriptConsoleMessage(self, message, line, source):<br>
<a href="http://logging.info" target="_blank">logging.info</a>("WebKitPage > js line %s at %s, message %s", line, source, message)<br><br>class WebKitBrowser:<br> def __init__(self, parsers):<br> self.app = QApplication([])<br>
self.url = self.timer = None<br> self.parsers = parsers<br> self.page = WebKitPage("Test user agent")<br> self.browser = QWebView()<br> QObject.connect(self.browser, SIGNAL("loadFinished(bool)"), self.load_finished)<br>
self.browser.get_js_str = self.get_js_str<br> self.browser.show()<br> self.browser.resize(800,600)<br> self.browser.setPage(self.page)<br> self.browser.settings().setAttribute(QWebSettings.AutoLoadImages, False)<br>
<br> def get_js_str(self, frame, js, arg = None):<br> jsqs = QString(js)<br> if arg != None: jsqs = jsqs.arg(arg)<br> return unicode(frame.evaluateJavaScript(jsqs).toString())<br><br> def timer_shot(self):<br>
self.timer_stop()<br> self.browser.stop()<br><br> def timer_start(self, timeout):<br> self.timer = QTimer()<br> QObject.connect(self.timer, SIGNAL("timeout()"), self.timer_shot)<br>
self.timer.start(timeout * 1000)<br><br> def timer_stop(self):<br> if self.timer:<br> self.timer.stop()<br> self.timer = None<br><br> def load_finished(self, stat):<br> <a href="http://logging.info" target="_blank">logging.info</a>("WebKitBrowser > Load finished")<br>
self.timer_stop()<br> self.status = stat and "success" or "timeout"<br> for parser in self.parsers:<br> parser.run(self.browser)<br> self.real_url = self.browser.get_js_str(self.browser.page().mainFrame(), "location.href")<br>
<a href="http://logging.info" target="_blank">logging.info</a>("WebKitBrowser > Status %s, Real url %s", self.status, self.real_url)<br> self.app.exit()<br><br> def get(self, url, timeout = 60):<br>
<a href="http://logging.info" target="_blank">logging.info</a>("WebKitBrowser > Loading %s", url)<br>
self.browser.stop()<br> self.status = "loading"<br> self.timer_start(timeout)<br> self.browser.load(QUrl(url))<br> self.app.exec_()<br> return self.status<br><br>class LinkParser:<br>
def extract_links(self, frame):<br> frame.evaluateJavaScript("var anchors = document.getElementsByTagName('a');")<br> count, ok = frame.evaluateJavaScript("anchors.length;").toInt()<br>
for i in range(0, count):<br> <a href="http://logging.info" target="_blank">logging.info</a>("LinkParser > %s", self.browser.get_js_str(frame, "anchors[%1].href", i))<br><br> def run(self, browser):<br>
self.browser = browser<br> self.extract_links(self.browser.page().mainFrame())<br> for frame in self.browser.page().mainFrame().childFrames():<br> self.extract_links(frame)<br><br>if __name__ == "__main__":<br>
logging.basicConfig(level=logging.DEBUG)<br> b = WebKitBrowser([ LinkParser() ])<br> b.get("<a href="http://google.com/" target="_blank">http://google.com/</a>")<br> b.get("<a href="http://youtube.com/" target="_blank">http://youtube.com/</a>", 1)<br>
b.get("<a href="http://google.com/" target="_blank">http://google.com/</a>")<br><br>
<br></div></div><div class="im">_______________________________________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com" target="_blank">PyQt@riverbankcomputing.com</a><br>
<a href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt" target="_blank">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br></div></blockquote></div><font color="#888888"><br><br clear="all"><br>-- <br>
patx - <a href="http://patx.me/" target="_blank">http://patx.me/</a><br>
</font></blockquote></div><br>