I believe it is common practice (at least it is for me) to be able to wrap function calls.
For example, as an example, a minimilistic wrapping function looks like:
def wrap(fn, *args, **kwargs):
return fn(*args, **kwargs)
And you could call an arbitrary method via
wrap(qt_method, 1, 2, foo='bar')
which would be equivalent to directly calling
qt_method(1,2, foo='bar')
This generally works for me. However, I've come across a case where it doesn't.
QWebView.load()
doesn't seem to like having an empty dictionary expanded into its call signature. E.g. wrap(my_webview.load, QUrl('http://istonyabbottstillprimeminister.com'))
fails with the exception:
TypeError: QWebView.load(QUrl): argument 1 has unexpected type 'QUrl'
.
Below is a minimilistic working example that demonstrates things that work, and don't. I have yet to find another Qt method that fails to wrap like this.
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtWebKit import QWebView
qapplication = QtGui.QApplication(sys.argv)
webview = QWebView()
url = QtCore.QUrl('http://istonyabbottstillprimeminister.com')
# basic wrapping function
def wraps(fn, *args, **kwargs):
return fn(*args, **kwargs)
args = ()
kwargs = {}
wraps(webview.load, url) # Doesn't work
webview.load(url, **kwargs) # Doesn't work
webview.load(url) # works
webview.url(*args, **kwargs) # works
webview.show()
qapplication.exec_()
This problem also applies when you subclass QWebView
and override the load
method like this:
def load(self *args, **kwargs):
return QWebView.load(self, *args, **kwargs)
Of course, if you instead call QWebView.load(self, *args)
or do not use *args, **kwargs
in the methods signature, then you don't get the exception (which follows what is seen from the minimilistic working example ebove)
Any insight into this would be appreciated.