WebKit2.JavascriptResult.get_value() throws exception “TypeError: Couldn't find foreign struct converter for 'JavaScriptCore.Value'”.

跟風遠走 提交于 2019-12-11 13:52:47

问题


I am writing a python3 + webkit2 + gtk3 based GUI. Everything works fine, but when I tried to use WebKit2.WebView.run_javascript_finish() in a callback function, getting the return (a WebKit2.JavascriptResult objetc), e extrat the value with WebKit2.JavascriptResult.get_value(), I received a "TypeError: Couldn't find foreign struct converter for 'JavaScriptCore.Value'".

I have a "from gi.repository import JavaScriptCore" on the imports, and I dont know why this error is occuring.

ps: Sorry about my terrible english.


回答1:


Unfortunately, using the JavaScriptCore library isn't supported in Python.

The definitions in gi.repository.JavaScriptCore only include opaque definitions for JavaScriptCore.Value and JavaScriptCore.GlobalContext in order to provide type information for functions that use those types; as you can verify by looking in /usr/share/gir-1.0/JavaScriptCore-4.0.gir. But as you've discovered, you can't actually call those functions because there's no information on how to convert those types to native Python objects.

I recommend writing a function in C which takes in your WebKit2.JavaScriptResult and fetches whatever information you need from it, and then exporting that function in its own private GIR library. So you would not use any JavaScriptCore types in your Python code. This is what it might look like:

from gi.repository import MyAppPrivate
# ...
def on_javascript_finish(obj, res):
    result = obj.run_javascript_finish(res)
    my_real_results = MyAppPrivate.process(result)


来源:https://stackoverflow.com/questions/37553701/webkit2-javascriptresult-get-value-throws-exception-typeerror-couldnt-find

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!