From: Nir Izraeli <nirizr@gm...>  20130724 18:52:14

BEGIN PGP SIGNED MESSAGE Hash: SHA1 Hi list, Sorry for the stupid question but I tried googling and couldn't find anything useful... I'm wrapping a function with %typemap(argout), and I need to convert a C array to a Python list. The issue I'm having is with handling errors. the wrapped function might not initialize (or allocate) the list if there's errors in the function, and instead return a negative value. I know I can access $result to get the converted python object, but what I actually want is to use the actual C int returned by the function. Thanks,  Nir. BEGIN PGP SIGNATURE Version: GnuPG v1.4.13 (Darwin) Comment: Using GnuPG with Thunderbird  http://www.enigmail.net/ iQIcBAEBAgAGBQJR8CJNAAoJENC3iWP8AkloaO8P/jMIsR+haNREFTpGe/0jEvUJ 3WFLn8gMYTYuwA95riC3f/D16nS80t9q0ZMn/q/62AuHBvljKFgUI7Z6UxBxbbng QQ6aKl0JwUmR/20IKq7xF3yL06ZRvBr+EQl+kzDlhUd3ifZ+NgoqyBKomPLufGHg 63175CMYk2GSNx4rpptunS/m2dCwjGO+Zy2Y35PUscGKozMLpxYmisJOa3mjPih0 l56/JHqKeZfBxK0E54mZCmL6ffBtPOCTMRCbXwuxDSuBbRlJ5g65wmozibrNFqrV qbflt9GB8kglRknoGOF+tCdDkTPaXUbY6haXIjB6u4l7zgi8MU3BSwCtLeaOexdM tV/PTWKoIbjzju1SqUwwWuYmkGCd2y1qfc1ewjZ2ge13wHQX4zY2zAZOBB685YlH YJHYxg8dLUFCf9p0Rff9Gpu7P06FeqTJ80Sx8GWfx2hZdjVygyJvGkXtHbRTSt+f zGC3aifss/2k/TLYFeHm5vZJxjBw6YTWX1IBRtdlBUvU7aAumFLkgm6pa7dLqY9N wmKnHAuQF7sXee+UYF3xJKIRKSlCne1itHfpJ87qh0WDrwT2b9WVIu1Rmym5m7Fo 3uKumaTSuVfftzLt6ZPfFnS4xoV5fiJGOBqPvsRkfEIKHcFnTWhvYiZ3v8gRkOd2 fUfmQ6GURJVn7QOg+MKK =phN0 END PGP SIGNATURE 
From: Nick Johnston <nickjoh@sh...>  20130802 00:36:49

Hi there, I am trying to wrap a library which uses the Armadillo linear algebra library in order to store / operate on matrices. The function that I am trying to wrap at the moment, is as follows (Note: that "arma::mat" is a typedef for "arma::Mat<double>") : void func ( arma::mat &firstInput, arma::mat &secondInput, arma::mat &firstOutput \ arma::Mat< size_t > &secondOutput, ... etc. [options in int / bool form] ); So I have written a SWIG interface file that wraps it as: extern void func (same arguments); And I have included "numpy.i" which is an interface file for NumPy, which is a library in Python that is comparable to Armadillo in C++. The first issue that I have run into is a seg fault (which is caused by the wrapped library attempting to access the values that are passed in from python/numpy). The python code: import example import numpy as np x = np.ndarray(shape=(2,2), dtype=float, order='F') y = np.ndarray(shape=(2,2), dtype=float, order='F') S = np.ndarray(shape=(2,2), dtype=float, order='F') T = np.ndarray(shape=(2,2), dtype='uintp', order='F') (Yes I am allocating the memory for the arrays inside S and T for now. I will deal with this later.) (And yes I did use 'float'  this is the same size as a 'double' in C++ / 'uintp' is the same as 'size_t') The parameter order='F' implies that the matrix will be generated with Fortran ordering: S,T = example.func(x,y,...) And here like the second problem: this tells me that I have entered the wrong number of arguments (although S and T should be passed in as arguments for argout). But then if I do this: example.func(x,y,S,T,...) This issue goes away, and I get the seg fault that I described previously. Below are my typemaps. Note that the constructor for arma::Mat< type > that I am using is as follows: arma::Mat< type > matrix (auxiliary memory *, int size of 1st dimension, int size of 2nd dimension, \ bool copy auxiliary memory, bool strict) "copy auxiliary memory" is false as I don't to make any copies, as large matrices are being passed around. "strict" is true; strict specifies whether or not the Armadillo matrix is allowed to reallocate if the matrix dimensions are increases Here is my 'in' typemap: //////////////////////////////////////////////////////////////////// %typemap(in) arma::Mat< type > *INPUT { arma::Mat< type > x( (type*)(((PyArrayObject *)$input)>data), 2, 2, false, true ); $1 = &x; } //////////////////////////////////////////////////////////////////// And, 'argout': //////////////////////////////////////////////////////////////////// %typemap(in) arma::Mat< type > *OUTPUT { arma::Mat< type > x( (type*)(((PyArrayObject *)$input)>data), 2, 2, false, true ); $1 = &x; } %typemap(argout) arma::Mat< type > *OUTPUT { $result = $input; } //////////////////////////////////////////////////////////////////// (The above typemaps are inside a SWIG macro that is used for double and size_t). Now I know that this is a rather incomplete 'argout' typemap, it is rather stubby. But I would expect it to wrap those arguments so that S,T = func() instead of func(,S,T) is correct. Here are my %apply statements: %apply(arma::Mat< double > *INPUT) { (arma::mat &firstInput) }; %apply(arma::Mat< double > *INPUT) { (arma::mat &secondInput) }; %apply(arma::Mat< double > *OUTPUT) { (arma::mat &firstOutput) }; %apply(arma::Mat< size_t > *OUTPUT) { (arma::Mat< size_t > &secondOutput) }; (I recognize that I am applying *VAR to &numberVar, I have tried &VAR, and it does not compile) If anyone can provide an insight, that would be great. I have spent an enormous amount of time trying to debug this, and I still don't know why this isn't working. Thanks, Nick  