Vala库的Python绑定
我正在尝试为一个 Vala 库创建 Python 绑定,参考了这个 IBM 的教程。
我最开始的目录里有以下两个文件:
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 的一个 bug,还是我设置上有什么问题?从 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
符号在哪里定义的?我漏掉了什么链接?
3 个回答
1
看起来这段代码也出现在了Charlie的第二个博客 2008上。
test_module.c
需要包含 <pygobject.h>
:
#include <Python.h>
#include <pygobject.h>
做了这个修改后,它可以在Python中成功构建并运行:
>>> import test
>>> t = test.Test()
>>> t.sum(1,2)
3
1
情况很糟糕!为pygtk写绑定真是太麻烦了,不过幸运的是,他们正在转向gobject introspection,这样会简单一些。
不过,似乎在test.override文件里多了一个空行,试着把它删掉,应该就能正常工作了(至少我测试过是这样)
4