PyXPCOM组件在XULRunner中未加载
我打算创建一个基于XULRunner的应用程序,需要和Python进行交互。我的计划是使用PyXPCOM。目前我正在自学如何使用PyXPCOM,并且在阅读创建Python XPCOM组件中的示例,但我无法让它正常工作。
我使用的是Ubuntu 11.04,以下是我的步骤:
创建了一个应用程序目录,并将我的XULRunner 5.x的二进制文件复制到这个目录下的
xulrunner
子目录中。成功按照构建PyXPCOM的步骤构建了PyXPCOM。
按照PyXPCOM源代码中的
README.txt
文件里的安装说明,将obj/dist/bin
目录下的所有内容复制到我的xulrunner
子目录中,并在xulrunner/chrome.manifest
文件中添加了以下内容:manifest components/pyxpcom.manifest
创建了
nsIPySimple.idl
文件,并将其放在我的应用程序的components
子目录中:#include "nsISupports.idl" [scriptable, uuid(2b324e9d-a322-44a7-bd6e-0d8c83d94883)] interface nsIPySimple : nsISupports { attribute string yourName; void write( ); void change(in string aValue); };
在我的
components
子目录中执行以下命令,创建了xpt文件:[xul-sdk-path]/xpidl -m typelib -w -v -I [xul-sdk-path]/idl/ nsIPySimple.idl
在我的
components
子目录中创建了nsIPySimple.py
文件:from xpcom import components, verbose class PySimple: #PythonTestComponent _com_interfaces_ = components.interfaces.nsIPySimple _reg_clsid_ = "{607ebc50-b8ba-11e0-81d9-001cc4c794e3}" _reg_contractid_ = "@mozilla.org/PySimple;1" def __init__(self): self.yourName = "a default name" # or mName ? def __del__(self): if verbose: print "PySimple: __del__ method called - object is destructing" def write(self): print self.yourName def change(self, newName): self.yourName = newName PYXPCOM_CLASSES = [ PySimple, ]
通过在我的
chrome.manifest
文件中添加以下内容,注册了Python代码:interfaces components/nsIPySimple.xpt component {607ebc50-b8ba-11e0-81d9-001cc4c794e3} components/nsIPySimple.py contract @mozilla.org/PySimple;1 {607ebc50-b8ba-11e0-81d9-001cc4c794e3}
创建了一个Javascript函数来调用Python的方法:
function showMore() { try { testComp = Components.classes["@mozilla.org/PySimple;1"].name; alert(testComp); testComp = Components.classes["@mozilla.org/PySimple;1"]. createInstance(Components.interfaces.nsIPySimple); testComp.write(); } catch (anError) { alert(anError); } }
但是,Javascript代码抛出了以下异常:
[Exception... "Component returned failure code: 0x80570015
(NS_ERROR_XPC_CI_RETURNED_FAILURE) [nsIJSCID.createInstance]"
nsresult: "0x80570015 (NS_ERROR_XPC_CI_RETURNED_FAILURE)"
location: "JS frame :: chrome://reader/content/main.js ::
showMore :: line 5" data: no]
你知道发生了什么事或者我做错了什么吗?
谢谢你的帮助和解释!
1 个回答
这个错误信息告诉我们,createInstance()
这个调用出错了。不过好消息是,这意味着在 createInstance()
之前的所有步骤都成功了(也就是说,PyXPCOM 正常工作,组件也加载和注册正确)。根据这个链接 http://code.google.com/p/pythonext/source/browse/samples/pyshell/components/pyShell.py,_com_interfaces_
需要是一个列表,所以这可能就是问题所在。如果支持的接口没有正确指定,那创建实例失败也是可以理解的。