[Moved] How to write a cache class from QAbstractNetworkCache with python
-
I tried and get Segmentation fault, I write this with c++, and it works fine. Anybody know what happened there? Thanks for any help.
I am using Debian sid, qt4 4.7.3, libqtwebkit 2.1.0~2011week13-1 , python-qt4 4.8.3-2+b1 , python 2.6.6-14
Here is my python code:
@
import sys, os
try:
from hashlib import md5, sha1
except:
import md5, sha1
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl, QIODevice, QFile
from PyQt4.QtNetwork import QAbstractNetworkCache, QNetworkCacheMetaData
from PyQt4.QtWebKit import QWebPage, QWebViewdef l(msg):
print >> sys.stderr, msgclass MyCache1(QAbstractNetworkCache):
def init(self, basedir, uid=None):
QAbstractNetworkCache.init(self)
self.basedir = basedir
self.uid = uiddef get_filename(self, url): m = md5(url.toString()).hexdigest() f = os.path.join(self.basedir, m) return f + ".meta", f + ".data" def cacheSize(self): l("cacheSize") return 100 * 1024 * 1024 # fake def clear(self): l("clear") return 0 def data(self, url): l("get data" + url.toString()) mfn, dfn = self.get_filename(url) try: df = QFile(dfn) df.open(QIODevice.ReadOnly) return df except: l("get_data rturn 0") return 0 def metaData(self, url): l("metaData " + url.toString()) return QNetworkCacheMetaData() def insert(self, device): l("insert") device.close() def prepare(self, metaData): l("get url: " + metaData.url().toString()) mfn, dfn = self.get_filename(metaData.url()) l("open file " + dfn) df = QFile(dfn) df.open(QFile.ReadWrite) l("file opened " + dfn) #df.write("I am here") return df def remove(self, url): l("remove") return True def updateMetaData(self, metaData): l("updateMetaData") return
app = QApplication(sys.argv)
v = QWebView()
c = MyCache1("/home/networkcache")v.page().networkAccessManager().setCache(c)
v.load(QUrl("http://google.com/"))
v.show()app.exec_()
@After doing some debugging, I notice the problem is after network/access/qnetworkreplyimpl.cpp:521, after this point, the cacheSaveDevice is a QIODevice * (this is return from python code "prepare"), but can not call openMode() method, whenever you call it, it will get Segment Fault, but you can access cacheSaveDevice->openMode, I still don't know how to fix it.
-
OK, answer by myself:
@
def prepare(self, metaData):
l("get url: " + metaData.url().toString())
mfn, dfn = self.get_filename(metaData.url())
l("open file " + dfn)
df = QFile(dfn)
df.open(QFile.ReadWrite)
l("file opened " + dfn)
self.save_df = df
return df
@