Passing a float from python to C (oo-style)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Passing a float from python to C (oo-style)

Sepp Meier
Hi all

I want to pass a float from C to python and from
python to c with getters and setters in oo-style, but
it doesn't work.

(The same construct with an Integer works)

Every suggestion is appreciated.


/*test.c*/-------------------------------------------

struct Test{
  int integer;
  float fpoint;
};

struct Test* newTest(){
  struct Test* test = (struct
Test*)malloc(sizeof(struct Test));
  return test;
}

int getInt(struct Test* t){
  return t->integer;
}

float getFloat(struct Test* t){
  return t->fpoint;
}

void setInt(struct Test* t, int i){
  t->integer = i;
}

void setFloat(struct Test* t, float f){
  t->fpoint = f;
}

/*test.i*/-------------------------------------------
//test.i

%module test
%{
#include <python/Python.h>
%}

%inline
%{
  struct Test{
    int integer;
    float fpoint;
  };
%}

%extend Test {
  void setInt(int i){setInt(self, i);}

  void setFloat(float f){setFloat(self, f);}

  int getInt(){return getInt(self);}

  float getFloat(){return setInt(self);}
};

struct Test* newTest();


python output----------------------------------------
>>> import test
>>> t = test.newTest()
>>> t.getInt()
134985952
>>> t.setInt(123)
>>> t.getInt()
123
>>> t.integer
123
>>> t.setFloat(1.4)
>>> t.getFloat()
1.0
>>> t.fpoint
3.6893488147419103e+19
>>> t.fpoint = 234.234
>>> t.getFloat()
1.0
>>> t.fpoint
234.23399353027344
>>>    


       

       
               
___________________________________________________________
Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Passing a float from python to C (oo-style)

Steve Juranich-2
On 11/10/05, Sepp Meier <[hidden email]> wrote:
> Hi all
>
> I want to pass a float from C to python and from
> python to c with getters and setters in oo-style, but
> it doesn't work.
>
> (The same construct with an Integer works)
>
> Every suggestion is appreciated.

Hmm....

I couldn't quite figure it out, but it looks like the value of "f" is
getting mangled between the inlined function in test.i and the actual
function in test.c.  I can't really look at it any more at the moment,
though.

Also, you don't need the #include <python/Python.h> in your .i file,
swig takes care of that for you.  Actually it #include's Python.h, and
you'll need to set up your include path appropriately.

If I get a chance I'll look at this a little more later.

--
Steve Juranich
Tucson, AZ
USA


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Passing a float from python to C (oo-style)

Joseph M. Koning
In reply to this post by Sepp Meier
Sepp Meier wrote:

>Hi all
>
>/*test.c*/-------------------------------------------
>
>struct Test{
>  int integer;
>  float fpoint;
>};
>
>struct Test* newTest(){
>  struct Test* test = (struct
>Test*)malloc(sizeof(struct Test));
>  return test;
>}
>
>int getInt(struct Test* t){
>  return t->integer;
>}
>
>float getFloat(struct Test* t){
>  return t->fpoint;
>}
>
>void setInt(struct Test* t, int i){
>  t->integer = i;
>}
>
>void setFloat(struct Test* t, float f){
>  t->fpoint = f;
>}
>
>/*test.i*/-------------------------------------------
>//test.i
>
>%module test
>%{
>#include <python/Python.h>
>%}
>
>%inline
>%{
>  struct Test{
>    int integer;
>    float fpoint;
>  };
>%}
>
>%extend Test {
>  void setInt(int i){setInt(self, i);}
>
>  void setFloat(float f){setFloat(self, f);}
>
>  int getInt(){return getInt(self);}
>
>  float getFloat(){return setInt(self);}
>};
>
>struct Test* newTest();
>
>  
>

Is this a cut and paste from your interface file or did you type it into
the email?
This line:
float getFloat(){return setInt(self);}
Should read:
float getFloat(){return getFloat(self);}
correct?

Joe



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...