On Tue, Sep 1, 2009 at 3:19 PM, Frederik<[email protected]> wrote:
> ecir hana wrote:
>
> Yes, except that PyMethodDef is a type, not a function. I have
> translated this example to Vala (see attachment). I have put the
> bindings into a separate 'python.vapi' file. You can compile with:

Thank you! This is very useful for me. It already does more than I wanted.

I tried to modify your code so I can call Vala function with string
argument from within Python (attached bellow).

However, I'm slowly getting lost:
- during compiling, valac says:
pythondemo3.vala.c:43: warning: passing arg 2 of `Py_InitModule4'
discards qualifiers from pointer target type
Is this what you said below about 'emb_methods' being constant?
(it might just be the case that I have weird python installation, though...)
- looking in your .vapi, what does it mean "..."? Variable number of arguments?
- and what does it mean, that class Object is empty? Doesn't it
matter? Or better yet, what should be inside?
- arg_parse_tuple(), when converting Python strings to C strings
expects char pointer, is it ok to write &s in Vala?
http://docs.python.org/extending/extending.html#extracting-parameters-in-extension-functions
http://docs.python.org/c-api/arg.htm (the very first format string "s")

PS: I'm sorry if I bother you guys....

> $ valac pythondemo.vala -X -lpython2.6 --pkg python --vapidir .
>
> The C compiler emits a warning about 'emb_methods' being constant, but
> Vala currently allows short array initialization with structs only for
> const arrays.
using Python;

static Python.Object? emb_numargs (Python.Object? self, Python.Object? args) {
	var s = "";
	if (!Python.arg_parse_tuple (args, "s", &s)) {
        return null;
	}
	stdout.printf ("Says: %s", s);
	return Python.build_value ("");
}

const MethodDef[] emb_methods = {
	{ "puts", emb_numargs, MethodFlags.VARARGS,
	  "Prints Hello World!." },
	{ null, null, 0, null }
};

int main (string[] args) {
	Python.initialize ();

	Python.init_module ("emb", emb_methods);

	Python.run_simple_string ("""

import emb
emb.puts('Hello World!\n')

""");

	Python.finalize ();

	return 0;
}

_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to