Hi folks,
I'm trying to interface Python to Lisp using the official embedded API. One aspect of this is that you can setup callback functions to pass data from Python to Lisp.
Here's an example of a module (defined in C) containing such functions, mapping a Python function name to the pointer:
static PyMethodDef ZZMethods[] = { {"copy_webpage",emb_copy_webpage,METH_VARARGS, "copy_webpage"}, {NULL, NULL, 0, NULL}};
This structure is passed to Python with this command:
Py_InitModule("ltzz", ZZMethods);
It is an array of this structure:
struct PyMethodDef { char *ml_name; PyCFunction ml_meth; int ml_flags; char *ml_doc; }; typedef struct PyMethodDef PyMethodDef;
Enclosed is the Lisp code that is my attempt at expressing this structure, but when I try and run the function testlispy, it crashes out with this message:
[6]> (testlispy) If you can see this, Python is loaded and working
#<FOREIGN-ADDRESS #x0022B030> #<FOREIGN-ADDRESS #x0022B030> #<FOREIGN-ADDRESS #x0022B040> #<FOREIGN-ADDRESS #x0022B050> C:\Python22\Lib\types.py:52: Warning: 'yield' will become a reserved keyword in the future warning: Python C API version mismatch for module Methods: This Python has API version 1011, module Methods has version 1003.
*** - handle_fault error2 ! address = 0x1 not in [0x19d70000,0x19ed27e8) ! SIGSEGV cannot be cured. Fault address = 0x1. Permanently allocated: 92032 bytes. Currently in use: 2346812 bytes. Free space: 426556 bytes.
Am I on the right lines, and if not, where have I gone wrong? I think I've misunderstood the documentation regarding arrays of structures, but because Clisp has no proper debugging environment, the only way to test it out is to make a DLL and pass it a structure. Therefore, it's easier to post to cffi-devel.
By the way, this project is for the Lisp community to use Python library code in their Lisp programs, which would be useful to me at least, so I'm not asking for help with a commercial project.
Thanks for any help,
Jeremy.
Jeremy Smith wrote:
Hi folks,
I'm trying to interface Python to Lisp using the official embedded API. One aspect of this is that you can setup callback functions to pass data from Python to Lisp.
I've got this figured out, but I could still do with some help on the array thing.
I want an array of structs. Not pointers to structs, but actual structs. So with an array of 4 16-byte structs, it should be 64 bytes.
I've got around the problem by using a struct which has 16 entries (4 * the 4-entry struct), but that's not desirable because it doesn't scale to more module entries without increasing the struct by 4 more entries.
Could someone please enlighten me on how to do this?
Thanks,
Jeremy.
Jeremy Smith jeremy@decompiler.org writes:
I want an array of structs. Not pointers to structs, but actual structs. So with an array of 4 16-byte structs, it should be 64 bytes.
I've got around the problem by using a struct which has 16 entries (4 * the 4-entry struct), but that's not desirable because it doesn't scale to more module entries without increasing the struct by 4 more entries.
I think this is what you want:
(defcstruct foo (x :int) (y :int) (z :int))
(with-foreign-object (foo-array 'foo 4) ...)
FOO-ARRAY will be a pointer to storage for 4 FOO structures.
Macroexpanding this WITH-FOREIGN-OBJECT:
(WITH-FOREIGN-POINTER (FOO-ARRAY 48) ...)
James