|
|
|
|
|
by sadfsa
6249 days ago
|
|
Ease of programming isn't the issue, at least not when it comes to writing libraries that must interface with C code (as a robot-interfacing library is likely to). Most Schemes and Lisps can interface to C by means of a few simple declarations, while Python requires the C code to accept and return Python's internal data structures (and convert them to a form that can be used by C code), and requires it to register those functions that are to be made visible to Python. Scheme (PLT) (require scheme/foreign)
(unsafe!)
(define my-c-lib (ffi-lib "libmylib")) ;; loads libmylib.so
(define my-c-func (get-ffi-obj 'my_c_func my-c-lib (_fun _long -> _long)))
;; The C code does not need to take Scheme into account--
;; Existing C libraries can be interfaced with Scheme
;; with a trivial amount of effort.
C (to be linked against Python code-- Python can load the resulting shared library as if it was written in Python). Notice that all this code does is interface Linux's htonl function to Python: #include <Python.h>
#include <netinet/in.h>
static PyObject *
py_htonl(PyObject *self, PyObject *args) {
long my_arg;
if(!PyArgParseTuple(args, "l", &my_arg))
return NULL;
return PyBuildValue("l", htonl(my_arg));
}
static PyMethodDef methods[] = {
{"htonl", py_htonl, METH_VARARGS,
"Demonstrate Python's C interface\n"}
};
static char module_doc[] = "Demonstration";
PyMODINIT_FUNC
initfunc(void) {
PyInitModule3("mylib", methods, module_doc);
}
|
|