【问题标题】:PyXPCOM component not loading in XULRunnerPyXPCOM 组件未在 XULRunner 中加载
【发布时间】:2011-07-28 02:48:32
【问题描述】:

我计划创建需要与 Python 交互的基于 XULRunner 的应用程序。计划是使用 PyXPCOM。目前,我正在自学使用 PyXPCOM 并通过 Creating a Python XPCOM component 中的示例组件 developermnet,但无法使其正常工作。

我使用的是 Ubuntu 11.04,我的步骤是:

  1. 创建了一个应用程序目录并将我的 XULRUnner 5.x 二进制分发版复制到其中的 xulrunner 子目录中

  2. 按照Building PyXPCOM成功构建PyXPCOM

  3. 按照PyXPCOM源README.txt文件中的安装说明,将目录obj/dist/bin的全部内容复制到我的xulrunner子目录中,并在xulrunner/chrome.manifest文件中添加以下行:

    manifest components/pyxpcom.manifest
    
  4. 创建了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);
    };
    
  5. 通过在我的components 子目录中执行以下命令创建 xpt 文件:

    [xul-sdk-path]/xpidl -m typelib -w -v -I [xul-sdk-path]/idl/ nsIPySimple.idl
    
  6. 在我的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,
    ]
    
  7. 通过在我的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}
    
  8. 创建了调用 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


    【解决方案1】:

    错误消息表明createInstance() 调用导致错误。好消息:这意味着createInstance() 之前的一切都成功了(PyXPCOM 正在工作并且组件已正确加载/注册)。 http://code.google.com/p/pythonext/source/browse/samples/pyshell/components/pyShell.py 表示 _com_interfaces_ 需要是一个列表,所以这可能是这里的问题。如果未正确指定支持的接口,则创建实例失败是有道理的。

    【讨论】:

    • 它仍然抛出同样的异常。但是在与@ActiveState 的好人一起解决之后,我发现问题出在 xulrunner-stub 上。当我使用存根运行我的应用程序时,它会引发异常,但如果我直接使用 xulrunner 运行它,一切正常!
    • 但是感谢您指出这一点,我没有注意到这是一个列表。也许是因为网站上的自动颜色编码。在您指出之前,[] 不够清晰,我看不到。
    • 终于弄清楚了为什么 xulrunner-stub 会抛出异常,而 xulrunner 本身却没有。 xulrunner-stub 没有加载所需的库:libxpcom.so 和 libpyxpcom.so。当我在dependentlibs.list 文件中列出两个文件时,xulrunner-stub 和 xulrunner 都按预期工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多