【发布时间】:2011-07-28 02:48:32
【问题描述】:
我计划创建需要与 Python 交互的基于 XULRunner 的应用程序。计划是使用 PyXPCOM。目前,我正在自学使用 PyXPCOM 并通过 Creating a Python XPCOM component 中的示例组件 developermnet,但无法使其正常工作。
我使用的是 Ubuntu 11.04,我的步骤是:
创建了一个应用程序目录并将我的 XULRUnner 5.x 二进制分发版复制到其中的
xulrunner子目录中按照Building 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.pyfrom 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} -
创建了调用 Python 方法的 Javascript 函数:
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]
知道发生了什么或我做错了什么吗?
感谢您的帮助和澄清!
【问题讨论】:
标签: python firefox xpcom xulrunner pyxpcom