Question

I want to have a shared library written in Vala which gets called by a Python application.

So i created this Vala library with two objects with one function each.
The only difference is that Bar takes an argument in the constructor, whereas Foo does not.

using GLib;

namespace VLibrary {
    public class Foo : GLib.Object {
        public Foo() {
            stdout.printf("VALA:\tcreating object...");
        }

        public void printThis(string x) {
            stdout.printf("print from vala: " +x +"\n");
        }
    }

    public class Bar : GLib.Object {
        public Bar(string parameter) {
            stdout.printf("vala object created (with parameter)");
        }

        public void printThis(string x) {
            stdout.printf("print from vala: "+x+"\n");
        }
    }
}

And compiled it with using valac to a shared (.so) library.
Valac also generated a .vapi and a .gir file.
I generated a .typelib file from the .gir file.

Then I wrote a small Python application which is supposed to use this library.
Before executing I had to set two environment variables to let python know where to find the typelib and library file.
export LD_LIBRARY_PATH=.
export GI_TYPELIB_PATH=.

#!/usr/bin/env python

from gi.repository import VLibrary



# Works, but doesnt call the constructor
foo1 = VLibrary.Foo()
# Works
foo1.printThis("FOO !")



# Works, but doesnt call the constructor
bar1 = VLibrary.Bar()
# Works
bar1.printThis("BAR !")



# TypeError: GObject.__init__() takes exactly 0 arguments (1 given)
text = 'hello world'
bar2 = VLibrary.Bar(text)
bar3 = VLibrary.Bar('hello world')

Creating an object of type Foo (no parameter in constructor) works but the print statement in the Foo constructor (Vala code) isnt executed.

When I want to create an object of type Bar I have to omit the string in the constructor, otherwise Python complains about the constructor not taking an argument (even though it should take one!)

Other than this both objects work as they should. Calling the objects method (both objects) with an argument works and prints everything correctly.

Can somebody tell me what I have done wrong ?
It seems impossible for me to call any type of Vala constructor from Python.
The objects get created but no constructor code is called.

Was it helpful?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top