From: <emi...@en...> - 2006-08-28 22:34:54
|
Thanks for a push in the right direction William. I seem to be running into some issues with the conversion of parameters when calling the ga_genesis_dobule(...) function shown below in my first email. when in gdb i see the first 3 parameters come across properly, and then the 4th is a None passed from Python. The call to SWIG_ConvertPtr identifies the parameter as py_None and sets *ptr to 0 and returns. The generated code then attempts to dereference 0 and seg faults. I have corrected this by adding a simple if( argp4 !=3D NULL ) arg4 =3D *argp4; = else arg4 =3D NULL; inside the generated call. This fixes the issue but when I receive my first actual function pointer I'm informed TypeError: Expected a pointer I dont like editing generated code so i'm inclined to think that there is an error in my interface file for SWIG that has required me to make these alterations to the _wrap.c file. I've attached a copy of my interface fil= e and my python script for a better idea of what i've done (not long, but i am probably doing something foolish that i dont see) Eamon Millman > emi...@en... wrote: >> Hello, >> >> I've got some questions about callbacks to wrapped C code from withi= n >> python. My understanding is that the callbacks used must be defined >> within the wrapped C code and can be done in two ways: >> >> >> 1. >> %constant void myFunc(int p1, int p2); >> >> 2. >> %callback("%s_cb") >> void myFunc(int p1, int p2); >> %nocallback >> >> 1) does not allow direct calling of the function (which is fine with m= e) >> 2) creates a callable and a callback form of the function (myFunc, >> myFunc_cb) >> >> >> Where my question comes in is that since I cannot use a python functio= n >> as >> a callback with the pre-generated SWIG wrapper I need to define my >> callback inside my wrapper interface as below to use it from within >> Python. The problem i'm having is that if i define the function i want >> to >> use as a helper inside my interface file I dont seem to be able to als= o >> flag it as a callback... am I doing something wrong? >> >> >> (my helper function i want to be a callback) >> %inline >> %{ >> boolean fitness_func(population* pop, entity* ent) { >> /* code goes here */ >> } >> %} >> >> (my callbacks defined in the C API) >> %constant boolean ga_seed_double_random(population *pop, entity *adam)= ; >> >> >> >> (I am making the call within Python as follows, the majority of >> parameters >> in this call are callbacks defined in the C API. fitness_func is the >> exception to this) >> >> import _gaul >> # some init code here >> pop =3D _gaul.ga_genesis_double( 200, 1, 2, None, None, None, None, >> _gaul.fitness_func, _gaul.ga_seed_double_random_unit_gaussian, None, >> _gaul.ga_select_one_aggressive, _gaul.ga_select_two_aggressive, >> _gaul.ga_mutate_double_singlepoint_drift, >> _gaul.ga_crossover_double_allele_mixing, None, None ) >> >> all i get is a Segmentation fault and no other information. My guess i= s >> that it's my helper function that's not flagged as a callback in the >> interface for SWIG, but i could be wrong. >> >> On a side note are there methods to debug the wrapped C module so i ca= n >> tell where the segfault is occuring? >> > > No. Use a debugger like you'd debug any program, for example gdb: > > gdb --args python myscript.so > > then type 'run' at the gdb prompt and then 'where' to give you the stac= k > trace when it seg faults. > > William > |