Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未在XULRunner中加载PyXPCOM组件_Python_Firefox_Xpcom_Xulrunner_Pyxpcom - Fatal编程技术网

Python 未在XULRunner中加载PyXPCOM组件

Python 未在XULRunner中加载PyXPCOM组件,python,firefox,xpcom,xulrunner,pyxpcom,Python,Firefox,Xpcom,Xulrunner,Pyxpcom,我计划创建需要与Python接口的基于XULRunner的应用程序。计划是使用PyXPCOM。目前,我正在自学如何使用PyXPCOM,并在中学习组件开发网的示例,但无法使其正常工作 我正在使用Ubuntu11.04,我的步骤是: 创建了一个应用程序目录,并将我的XULRUnner 5.x二进制发行版复制到XULRUnner子目录中 from xpcom import components, verbose class PySimple: #PythonTestComponent _co

我计划创建需要与Python接口的基于XULRunner的应用程序。计划是使用PyXPCOM。目前,我正在自学如何使用PyXPCOM,并在中学习组件开发网的示例,但无法使其正常工作

我正在使用Ubuntu11.04,我的步骤是:

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

  • 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,
    ]
    
  • 成功构建PyXPCOM,如下所示

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

    manifest components/pyxpcom.manifest
    
    interfaces  components/nsIPySimple.xpt
    component   {607ebc50-b8ba-11e0-81d9-001cc4c794e3} components/nsIPySimple.py
    contract    @mozilla.org/PySimple;1 {607ebc50-b8ba-11e0-81d9-001cc4c794e3}
    
  • 创建了
    nsIPySimple.idl
    文件,并将其放置在我的应用程序
    组件
    子目录中:

    #include "nsISupports.idl"
    [scriptable, uuid(2b324e9d-a322-44a7-bd6e-0d8c83d94883)]
    interface nsIPySimple : nsISupports {
        attribute string yourName;
        void write( );
        void change(in string aValue);
    };
    
    [xul-sdk-path]/xpidl -m typelib -w -v -I [xul-sdk-path]/idl/ nsIPySimple.idl
    
  • 通过在my
    components
    子目录中执行以下命令创建xpt文件:

    #include "nsISupports.idl"
    [scriptable, uuid(2b324e9d-a322-44a7-bd6e-0d8c83d94883)]
    interface nsIPySimple : nsISupports {
        attribute string yourName;
        void write( );
        void change(in string aValue);
    };
    
    [xul-sdk-path]/xpidl -m typelib -w -v -I [xul-sdk-path]/idl/ nsIPySimple.idl
    
  • 在my
    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代码:

    manifest components/pyxpcom.manifest
    
    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]
    
    知道发生了什么或我做错了什么吗


    谢谢你的帮助和澄清

    错误消息表明
    createInstance()
    调用导致错误。好消息是:这意味着前面的
    createInstance()
    都成功了(PyXPCOM正在工作,组件已正确加载/注册)。指示
    \u com\u interfaces\u
    需要是一个列表,因此这可能就是问题所在。如果未正确指定支持的接口,则创建实例失败是有意义的。

    它仍会引发相同的异常。但是在与good people@ActiveState合作后,我发现问题出在xulrunner存根上。当我使用存根运行我的应用程序时,它会抛出异常,但是如果我直接使用xulrunner运行它,一切都会正常!但是谢谢你指出这一点,我没有注意到这是一个列表。可能是因为网站上的自动颜色编码。直到您指出,[]才清楚地让我看到。终于弄明白为什么xulrunner存根会抛出异常,为什么xulrunner本身不会。xulrunner存根未加载所需的库:libxpcom.so和libpyxpcom.so。当我在dependentlibs.list文件中列出两个文件时,xulrunner存根和xulrunner都按预期工作。