【问题标题】:Python bindings for a vala libraryvala 库的 Python 绑定
【发布时间】:2011-03-02 10:14:29
【问题描述】:

我正在尝试使用以下IBM tutorial 作为参考来创建与 vala 库的 python 绑定。

我的初始目录有以下两个文件:

test.vala

using GLib;

namespace Test {

   public class Test : Object {
       public int sum(int x, int y) {
           return x + y;
       }
   }

}

test.override

%%
headers
#include <Python.h> 
#include "pygobject.h"
#include "test.h"
%%
modulename test
%%
import gobject.GObject as PyGObject_Type
%%
ignore-glob
  *_get_type
%%

并尝试使用以下代码构建python模块源test_wrap.c

build.sh

#/usr/bin/env bash

valac test.vala -CH test.h
python /usr/share/pygobject/2.0/codegen/h2def.py test.h > test.defs
pygobject-codegen-2.0 -o test.override -p test test.defs > test_wrap.c

但是,最后一个命令失败并出现错误

$ ./build.sh 
Traceback (most recent call last):
  File "/usr/share/pygobject/2.0/codegen/codegen.py", line 1720, in <module>
    sys.exit(main(sys.argv))
  File "/usr/share/pygobject/2.0/codegen/codegen.py", line 1672, in main
    o = override.Overrides(arg)
  File "/usr/share/pygobject/2.0/codegen/override.py", line 52, in __init__
    self.handle_file(filename)
  File "/usr/share/pygobject/2.0/codegen/override.py", line 84, in handle_file
    self.__parse_override(buf, startline, filename)
  File "/usr/share/pygobject/2.0/codegen/override.py", line 96, in __parse_override
    command = words[0]
IndexError: list index out of range

这是 pygobject 中的错误,还是我的设置有问题?从 python 调用用 vala 编写的代码的最佳方法是什么?

编辑: 删除额外的行解决了当前的问题,但现在当我继续构建 python 模块时,我面临另一个问题。将以下C文件添加到目录中已有的两个中:

test_module.c

#include <Python.h>

void test_register_classes (PyObject *d);
extern PyMethodDef test_functions[];

DL_EXPORT(void)
inittest(void)
{
  PyObject *m, *d;
  init_pygobject();
  m = Py_InitModule("test", test_functions);
  d = PyModule_GetDict(m);
  test_register_classes(d);
  if (PyErr_Occurred ()) {
      Py_FatalError ("can't initialise module test");
  }
}

并使用以下脚本构建

build.sh

#/usr/bin/env bash

valac test.vala -CH test.h
python /usr/share/pygobject/2.0/codegen/h2def.py test.h > test.defs
pygobject-codegen-2.0 -o test.override -p test test.defs > test_wrap.c

CFLAGS="`pkg-config --cflags pygobject-2.0` -I/usr/include/python2.6/ -I."
LDFLAGS="`pkg-config --libs pygobject-2.0`"

gcc $CFLAGS -fPIC -c test.c 
gcc $CFLAGS -fPIC -c test_wrap.c 
gcc $CFLAGS -fPIC -c test_module.c
gcc $LDFLAGS -shared test.o test_wrap.o test_module.o -o test.so

python -c 'import test; exit()'

导致错误:

$ ./build.sh 
***INFO*** The coverage of global functions is 100.00% (1/1)
***INFO*** The coverage of methods is 100.00% (1/1)
***INFO*** There are no declared virtual proxies.
***INFO*** There are no declared virtual accessors.
***INFO*** There are no declared interface proxies.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: ./test.so: undefined symbol: init_pygobject

init_pygobject 符号在哪里定义?我错过了什么链接?

【问题讨论】:

  • 这个问题是几年前提出的,现在情况可能已经改变。目前情况如何?。即 CURRENTLY 为 vala 代码生成 Python 绑定的最佳方式是什么?

标签: python gobject vala pygobject


【解决方案1】:

您可以使用 GObject Introspection。

此存储库包含如何将 vala 库自动绑定到其他语言的示例:

https://github.com/antono/vala-object

【讨论】:

    【解决方案2】:

    情况非常糟糕!为 pygtk 编写绑定简直就是地狱,幸运的是,他们正在切换到 gobject 内省,这将使事情变得更容易..

    无论如何,test.override 文件中似乎有一个额外的换行符,尝试删除它,它应该可以工作(至少我已经测试过了)

    【讨论】:

    • 谢谢。这解决了当前的问题,但现在我有了一个新问题。你知道怎么解决吗?
    • 对不起,我已经尝试在某个地方搜索,但没有找到任何相关内容,似乎它适用于 pygtk 2.16,您应该向 pygtk 邮件列表询问。
    【解决方案3】:

    看起来这个代码也在 Charlie's Second Blog 2008

    test_module.c需要包含&lt;pygobject.h&gt;

    #include <Python.h>
    #include <pygobject.h>
    

    通过该更改,它可以在 python 中构建和运行:

    >>> import test
    >>> t = test.Test()
    >>> t.sum(1,2)
    3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多