[英]Python bindings for a vala library
我正在尝试使用以下IBM教程作为参考来创建与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
符号在哪里定义? 我错过了链接到什么?
情况非常糟糕! 为pygtk编写绑定真是一个地狱,幸运的是,它们正在切换到gobject内省,这将使事情变得更容易。
无论如何,似乎在test.override文件中有多余的换行符,请尝试删除它,它应该可以工作(至少我已经对其进行了测试)
看起来这段代码也在Charlie's Second Blog 2008上
test_module.c
需要包含<pygobject.h>
:
#include <Python.h>
#include <pygobject.h>
进行此更改后,它将在python中构建并运行:
>>> import test
>>> t = test.Test()
>>> t.sum(1,2)
3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.