vala库的Python绑定

vala库的Python绑定,python,gobject,vala,pygobject,Python,Gobject,Vala,Pygobject,我试图使用以下内容作为参考来创建到vala库的python绑定 我的初始目录包含以下两个文件: test.vala using GLib; namespace Test { public class Test : Object { public int sum(int x, int y) { return x + y; } } } 测试。覆盖 %% headers #include <Python.h> #inc

我试图使用以下内容作为参考来创建到vala库的python绑定

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

test.vala

using GLib;

namespace Test {

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

}
测试。覆盖

%%
headers
#include <Python.h> 
#include "pygobject.h"
#include "test.h"
%%
modulename test
%%
import gobject.GObject as PyGObject_Type
%%
ignore-glob
  *_get_type
%%
但是,最后一个命令失败并出现错误

$ ./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
导致错误:

$ ./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
$./build.sh
***信息***全球功能覆盖率为100.00%(1/1)
***信息***方法的覆盖率为100.00%(1/1)
***信息***没有声明的虚拟代理。
***信息***没有声明的虚拟访问器。
***信息***没有声明的接口代理。
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ImportError:./test.so:未定义符号:init_pygobject

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

非常糟糕的情况!为pygtk编写绑定相当麻烦,幸运的是,他们正在转向gobject内省,这将使事情变得更容易


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

看起来此代码也在运行

测试\u模块.c
需要包括


你可以用GObject内省

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


谢谢。这解决了当前的问题,但现在我有了一个新问题。你知道如何解决这个问题吗?对不起,不,我试着在某个地方搜索,但我没有找到任何相关的东西,它似乎与pygtk 2.16一起工作。你应该询问pygtk邮件列表。这个问题在几年前就被问到了,现在情况可能已经改变了。目前的情况如何?。i、 当前为vala代码生成Python绑定的最佳方法是什么?
#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");
  }
}
#/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
#include <Python.h>
#include <pygobject.h>
>>> import test
>>> t = test.Test()
>>> t.sum(1,2)
3