vala 库的 Python 绑定

发布于 2024-09-05 00:36:03 字数 3325 浏览 4 评论 0原文

我正在尝试使用以下 IBM 教程< /a> 作为参考。

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

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 code

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 符号已定义?我错过了什么链接?

I am trying to create python bindings to a vala library using the following IBM tutorial as a reference.

My initial directory has the following two files:

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
%%

and try to build the python module source test_wrap.c using the following code

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

However, the last command fails with an error

$ ./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

Is this a bug in pygobject, or is something wrong with my setup? What is the best way to call code written in vala from python?

EDIT:
Removing the extra line fixed the current problem, but now as I proceed to build the python module, I am facing another problem. Adding the following C file to the existing two in the directory:

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");
  }
}

and building with the following script

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()'

results in an error:

$ ./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

Where is the init_pygobject symbol defined? What have I missed linking to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛放△進行李 2024-09-12 00:36:04

您可以使用 GObject Introspection。

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

https://github.com/antono/ vala-对象

You can go with GObject Introspection.

This repository contains samples how to auto-bind vala libraries to other languages:

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

甜味超标? 2024-09-12 00:36:04

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

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

Very bad situation! Writing bindings for pygtk is quite an hell, fortunately they are switching to gobject introspection that will make the things easier..

Anyway it seems that there is an extra newline in the test.override file, try removing that and it should work (at least I've tested it)

风和你 2024-09-12 00:36:04

貌似这段代码也上线了
查理的第二个博客 2008

test_module.c 需要包含

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

通过这一更改,它可以在 python 中构建并运行:

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

Looks like this code is also on
Charlie's Second Blog 2008

test_module.c needs to include <pygobject.h>:

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

With that change it builds and runs in python with:

>>> import test
>>> t = test.Test()
>>> t.sum(1,2)
3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文