Thanks Karl!
 
That worked perfectly. I'm surprised that noone had written anything about this anywhere on the net. Well, now they have! :-)
 
Cheers
JP
 
On Sun, Feb 26, 2012, at 09:44 PM, Karl Wette wrote:
Hi John,

You could modify your "in" typemap by moving the declaration of "myerr" inside the typemap:

%typemap(in,numinputs=0, noblock=1) int *err {
   int myerr = 0;
   $1 = &myerr;
};

So long as there's only one "int *err" argument in each function, this should be fine. You can then use "myerr" directly without the argument number.

Cheers,
Karl


On 26 February 2012 21:18, John Pye <john@curioussymbols.com> wrote:
Hi all

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 a, int b, int *err){
   if(a < 0)*err = 1;
   if(b < 0)*err = 2;
   return a+b;
}

char *err_string(int err){
   switch(err){
   case 1:return "first argument was less than 0";
   case 2:return "second argument was less than 0";
   default:return "unknown error";
   }
}
%}

%typemap(in,numinputs=0) int *err (int myerr = 0){
   $1 = &myerr;
};

%exception{
   $action
   if(myerr3 != 0){
       PyErr_SetString(PyExc_RuntimeError,err_string(myerr3));
       return NULL;
   }
};

int add(int a, int b, int *err);

This behaves as it should, eg with

import myswig
print "add(1,1) = "
print myswig.add(1,1)
# prints '2'

print "add(1,-1) = "
print myswig.add(1,-1)
# raises an exception

# we never get here...
print "here we are"

but I can't really use this solution, because if I have another function
like

int add(int a, int b, int c, int *err)

then my myerr3 kludge will break down.

What's the better way to solve this problem, without changing the
calling convention of the C code? I couldn't find any clues in the
documentation.

I've also cross-posted this question here:
http://stackoverflow.com/questions/9456723

Cheers
JP
--
John Pye
john@curioussymbols.com


------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Swig-user mailing list
Swig-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
 
--
John Pye
john@curioussymbols.com