ecir hana wrote:
> On Tue, Sep 1, 2009 at 1:14 PM, Jan Hudec<[email protected]> wrote:
>> The OP does not seem to be. He says "like the example for Lua" and that shows
>> running lua code from vala.
>
> Yes. And also calling Vala code from Lua. Or Python.
>
> Just to check if I'm on the right track:
> - just to run some python from Vala I can use Frederik's code from above
> - and to call Vala from Python, I have to, according:
> http://docs.python.org/extending/embedding.html#extending-embedded-python
> add to Frederik's code the definition of "Py_InitModule()", which in
> turn needs "PyMethodDef()" and "PyObject()" and if I don't have to
> return anything, that's all because PyMethodDef() could return NULL.
>
> Is this correct, so far?
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:
$ 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.
Best regards,
Frederik
using Python;
static int numargs = 0;
static Python.Object? emb_numargs (Python.Object? self, Python.Object? args) {
if (!Python.arg_parse_tuple (args, ":numargs")) {
return null;
}
return Python.build_value ("i", numargs);
}
const MethodDef[] emb_methods = {
{ "numargs", emb_numargs, MethodFlags.VARARGS,
"Return the number of arguments received by the process." },
{ null, null, 0, null }
};
int main (string[] args) {
Python.initialize ();
numargs = args.length;
Python.init_module ("emb", emb_methods);
Python.run_simple_string ("""
import emb
print "Number of arguments", emb.numargs()
""");
Python.finalize ();
return 0;
}
[CCode (cheader_filename = "python2.6/Python.h")]
namespace Python {
[CCode (cname = "Py_Initialize")]
public static void initialize ();
[CCode (cname = "Py_Finalize")]
public static void finalize ();
[CCode (cname = "Py_InitModule")]
public static void init_module (string name, [CCode (array_length = false, array_null_terminated = true)] MethodDef[] methods);
[CCode (cname = "Py_BuildValue")]
public static Python.Object build_value (string format, ...);
[CCode (cname = "PyRun_SimpleString")]
public static void run_simple_string (string code);
[CCode (cname = "PyArg_ParseTuple")]
public static bool arg_parse_tuple (Python.Object arg, string format, ...);
[SimpleType]
[CCode (cname = "PyMethodDef")]
public struct MethodDef {
[CCode (cname = "ml_name")]
public string name;
[CCode (cname = "ml_meth")]
public CFunction meth;
[CCode (cname = "ml_flags")]
public int flags;
[CCode (cname = "ml_doc")]
public string doc;
}
[CCode (cname = "PyCFunction", has_target = false)]
public delegate Python.Object? CFunction (Python.Object? self, Python.Object? args);
[Flags]
[CCode (cprefix = "METH_", cname = "int")]
public enum MethodFlags {
OLDARGS,
VARARGS,
KEYWORDS,
NOARGS,
O,
CLASS,
STATIC,
COEXIST
}
[Compact]
[CCode (cname = "PyObject")]
public class Object {
// TODO
}
}
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list