swig

SWIG+c+Python: Passing and receiving c arrays

℡╲_俬逩灬. 提交于 2019-12-06 15:54:01
I am trying to reuse some old c code with SWIG and Python. Right now I am quite confused. The errors I get can be demonstrated on a small example: bsp.h: extern void add(int a[], int b[], int c[]); bsp.c: #include "bsp.h" void add(int a[], int b[], int c[]) { c[0] = a[0] + b[0]; c[1] = a[1] + b[1]; } bsp.i %module bsp %{ #include "bsp.h"; %} %include "bsp.h"; setup.py: #!/usr/bin/env python from distutils.core import setup, Extension bsp_module = Extension('_bsp', sources = ['bsp_wrap.c', 'bsp.c'] ) setup(name = 'bsp', ext_modules = [bsp_module], py_modules = ["bsp"] ) The example Python file

%typemap and %exception for error codes from C functions for SWIG, Python

拜拜、爱过 提交于 2019-12-06 14:39:49
I've got some C code that I want to expose to Python. It has a calling convention like this: int add(int a, int b, int *err) where the return value would be (a+b) or whatever, but if something went wrong, then I would get an error code in *err. I want to wrap this function so that it behaves like this, from the Python perspective: def add(a,b): if something_bad: raise RuntimeError("something bad") return a+b This should be easy, right? But I'm not finding it so. Here is something that I have that works, but look out for the myerr3 kludge: %module myswig %feature("autodoc","1"); %{ int add(int

how to investigate python2 segfault on imp.load_module

妖精的绣舞 提交于 2019-12-06 11:55:36
I am trying to install and use dolfin on Arch Linux, with Python 2.7.3. What is the best way to find out what is causing segmentation faults such as these? $ python2 -c "import dolfin; print dolfin.__version__" [3] 6491 segmentation fault (core dumped) python2 -c "import dolfin; print dolfin.__version__" I have tried inspecting the core file through gdb: $ sudo systemd-coredumpctl gdb 6491 but they are always truncated: Reading symbols from /usr/bin/python2.7...(no debugging symbols found)...done. BFD: Warning: /var/tmp/coredump-wSi8DV is truncated: expected core file size >= 39694336, found:

SWIGTYPE_p_p_ : a Pointer on a Pointer on a Structure ( From C/C++ to Java )

穿精又带淫゛_ 提交于 2019-12-06 11:15:44
I'm using Swig to generate Java classes. I need to deal with a pointer on a pointer on a structure . I have this structure : struct Model { uint32_t serial; } And I have a function in the C/C++ that needs to be wrapped : void getModel( Model ** model ){ // instructions... } I get as a result this Java class : public class SWIGTYPE_p_p_Model { private long swigCPtr; protected SWIGTYPE_p_p_Model(long cPtr, boolean futureUse) { swigCPtr = cPtr; } protected SWIGTYPE_p_p_Model() { swigCPtr = 0; } protected static long getCPtr(SWIGTYPE_p_p_Model obj) { return (obj == null) ? 0 : obj.swigCPtr; } } So

How C/C++ global variables are implemented in python?

删除回忆录丶 提交于 2019-12-06 10:30:55
问题 While i am reading through SWIG Documentation i came through these lines.. C/C++ global variables are fully supported by SWIG. However, the underlying mechanism is somewhat different than you might expect due to the way that Python assignment works. When you type the following in Python a = 3.4 "a" becomes a name for an object containing the value 3.4. If you later type b = a then "a" and "b" are both names for the object containing the value 3.4. Thus, there is only one object containing 3.4

Wrapping a Lua object for use in C++ with SWIG

淺唱寂寞╮ 提交于 2019-12-06 10:26:11
问题 Currently I know how to have C++ objects instantiated and passed around in Lua using SWIG bindings, what I need is the reverse. I am using Lua & C++ & SWIG. I have interfaces in C++ and objects in lua, that implement methods which do the same job and have the same structure. I would like to be able to instantiate these objects in lua yet pass them around in C++ using pointers to that interface which they resemble. As such I can imagine creating a c++ implementation of the interface which

Proxying C#->C++ class wrappers using SWIG

不羁岁月 提交于 2019-12-06 09:55:11
Say I have following C++ code: /* File : example.h*/ typedef void (__stdcall *CppCallback)(int code, const char* message); class CppClass { public: CppClass() {}; void call(CppCallback callback) { callback(1234, "Hello from C++"); } }; And then I have C# counterpart: /* File : example.cs */ using System; using System.Text; public delegate void CSharpCallback(int code, string param); public class App { static void Main() { CppClass cppClass = new CppClass(); cppClass.call((i, s) => { Console.WriteLine("Code " + i + " and message '" + s + "'"); }); } } And then I have SWIG .i file gluing them

Polymorphism across C++ and Ruby using SWIG

假装没事ソ 提交于 2019-12-06 09:19:27
问题 I use SWIG to wrap a Ruby script around a C++ library. In Ruby, I can inherit from a C++ class, but I cannot pass the resulting pointer to a C++ function in a polymorphic way. Here is a concrete example. The SWIG interface file defines base class Animal with virtual function sound(): [animals.i] %module(directors="1") animals %{ #include "animals.h" %} // Apply the 'director' feature to a virtual function, // so that we can override it in Ruby. %feature("director") Animal::sound; class Animal

SWIG & C/C++ Python API connected - SEGFAULT

故事扮演 提交于 2019-12-06 08:18:49
my task is to create dual program. At the beginning I start C program that calls throught C/C++ API of Python some Python method. The called method after that call a function that is created with SWIG. I show you my sample also with backtrace from gdb after I am given Segmentation fault. main.c: #include <Python.h> #include <stdio.h> #include "utils.h" int main(int argc, char** argv) { printf("Calling from C !\n"); increment(); int i; for(i = 0; i < 11; ++i) { Py_Initialize(); PyObject *pname = PyString_FromString("py_function"); PyObject *module = PyImport_Import(pname); PyObject *dict =

How to use SWIG Generated C structures in Java as input to C functions via SWIG/JNI

岁酱吖の 提交于 2019-12-06 08:01:28
问题 I have a SWIG interface file that exposes some C functions (via JNI) to my Java application and these C structures are used as input into the C function (via SWIG/JNI). SWIG generates the structure as a Java class, but I'm unsure how to set the structures properties as the setters take a SWIG generated type. I need to set the structures properties before passing it as input into the C function from my Java class. example_location_id_t_ is the class I need to pass, but the setters for Id and