Quantcast

Enums as unsigned int in Python

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

Enums as unsigned int in Python

andreas.held
Hi

I guess my original mail got lost in the translation between mail lists, so here I try again:
Sorry if this has been asked before, but I could not find anything in the archive.
In my C++ source code, which I am wrapping for Python, I have a normal enum declaration:
enum Fl_Color {
FL_WHITE = 255;
}

and I have several C style functions that return this enum, e.g.
Fl_Color get_color(uchar g) { return  (((g <<8 ) | g) << 8 |g);}

The problem is that the enum gets wrapped in those functions as an int ,
by means of SWIG_From_int. Is there a way I can tell SWIG to treat it as unsigned?

BTW, I am using swig-1.3.26

Thanks for your help.

Andreas Held
http://pyfltk.sourceforge.net




-------------------------------------------------------
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: Enums as unsigned int in Python

Marcelo Matus
I read your mail before and I was thinking in your problem.

As you see, it is not a problem with managing enums as int or unsigned int.
it is a problem with your method not returning an unsigned value, ie, in
the
C++ side you will have the same problem, just try

unsigned int c = (unsigned int) get_color(g);

if you are lucky, you will get something in the (0-255) range, but if not,
c will be a very very large value (around UINT_MAX - 255).

So, I will not tell you how to map enum as unsigned int, which is very easy
by the way :), but I will recommend you to change your method to something
safer as:

Fl_Color get_color(uchar g) { return  (uchar) (((g <<8 ) | g) << 8 |g);}


then you will return an unsigned value, and C++, swig and python will
live happy ever
after.

Marcelo

[hidden email] wrote:

>Hi
>
>I guess my original mail got lost in the translation between mail lists, so here I try again:
>Sorry if this has been asked before, but I could not find anything in the archive.
>In my C++ source code, which I am wrapping for Python, I have a normal enum declaration:
>enum Fl_Color {
>FL_WHITE = 255;
>}
>
>and I have several C style functions that return this enum, e.g.
>Fl_Color get_color(uchar g) { return  (((g <<8 ) | g) << 8 |g);}
>
>The problem is that the enum gets wrapped in those functions as an int ,
>by means of SWIG_From_int. Is there a way I can tell SWIG to treat it as unsigned?
>
>BTW, I am using swig-1.3.26
>
>Thanks for your help.
>
>Andreas Held
>http://pyfltk.sourceforge.net
>
>
>
>
>-------------------------------------------------------
>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
>
>  
>



-------------------------------------------------------
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: Enums as unsigned int in Python

Marcelo Matus
In reply to this post by andreas.held
and I forgot, in your case, unless you really want to name all the colors
from 0-255, enums seems like strange type to use, maybe you need something
like

   typedef uchar Fl_Color;
   const Fl_Color FL_WHITE = 255;

then

  Fl_Color get_color(uchar g) { return  (((g <<8 ) | g) << 8 |g);}


will work as you expect in C/C++ and the python side, where Fl_Color
really is
always between 0-255.

Marcelo


[hidden email] wrote:

>Hi
>
>I guess my original mail got lost in the translation between mail lists, so here I try again:
>Sorry if this has been asked before, but I could not find anything in the archive.
>In my C++ source code, which I am wrapping for Python, I have a normal enum declaration:
>enum Fl_Color {
>FL_WHITE = 255;
>}
>
>and I have several C style functions that return this enum, e.g.
>Fl_Color get_color(uchar g) { return  (((g <<8 ) | g) << 8 |g);}
>
>The problem is that the enum gets wrapped in those functions as an int ,
>by means of SWIG_From_int. Is there a way I can tell SWIG to treat it as unsigned?
>
>BTW, I am using swig-1.3.26
>
>Thanks for your help.
>
>Andreas Held
>http://pyfltk.sourceforge.net
>
>
>
>
>-------------------------------------------------------
>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
>
>  
>



-------------------------------------------------------
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: Re: Enums as unsigned int in Python

andreas.held
In reply to this post by andreas.held
Hi Marcelo

Thank you for your comments. Sometimes we do not have full control over the code we wrap. Actually, I did something along the lines you suggested, by redeclaring the offending functions with a different return type. I just wondered whether there would be a more elegant way of doing this. Alas, it does not seem so.

Regards

Andreas Held

>and I forgot, in your case, unless you really want to name all the colors
>from 0-255, enums seems like strange type to use, maybe you need something
>like
>
>   typedef uchar Fl_Color;
>   const Fl_Color FL_WHITE = 255;
>
>then
>
>  Fl_Color get_color(uchar g) { return  (((g <<8 ) | g) << 8 |g);}
>
>
>will work as you expect in C/C++ and the python side, where Fl_Color
>really is
>always between 0-255.
>
>Marcelo
>
>
>[hidden email] wrote:
>
>>Hi
>>
>>I guess my original mail got lost in the translation between mail lists, so here I try again:
>>Sorry if this has been asked before, but I could not find anything in the archive.
>>In my C++ source code, which I am wrapping for Python, I have a normal enum declaration:
>>enum Fl_Color {
>>FL_WHITE = 255;
>>}
>>
>>and I have several C style functions that return this enum, e.g.
>>Fl_Color get_color(uchar g) { return  (((g <<8 ) | g) << 8 |g);}
>>
>>The problem is that the enum gets wrapped in those functions as an int ,
>>by means of SWIG_From_int. Is there a way I can tell SWIG to treat it as unsigned?
>>
>>BTW, I am using swig-1.3.26
>>
>>Thanks for your help.
>>
>>Andreas Held
>>http://pyfltk.sourceforge.net
>>
>>
>>
>>
>>-------------------------------------------------------
>>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
>>
>>  
>>
>
>
>


-------------------------------------------------------
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: Enums as unsigned int in Python

Marcelo Matus
Ok, now that you have no choice, here is the secret.
Add the following lines to your interface:

%apply unsigned char {enum Fl_Color};
%apply const unsigned char& {const enum Fl_Color& };


Note that you need to use unsigned char instead of unsigned int,
and then you will avoid the overflow problem.

Marcelo


[hidden email] wrote:

>Hi Marcelo
>
>Thank you for your comments. Sometimes we do not have full control over the code we wrap. Actually, I did something along the lines you suggested, by redeclaring the offending functions with a different return type. I just wondered whether there would be a more elegant way of doing this. Alas, it does not seem so.
>
>Regards
>
>Andreas Held
>
>  
>
>>and I forgot, in your case, unless you really want to name all the colors
>>    
>>
>>from 0-255, enums seems like strange type to use, maybe you need something
>  
>
>>like
>>
>>  typedef uchar Fl_Color;
>>  const Fl_Color FL_WHITE = 255;
>>
>>then
>>
>> Fl_Color get_color(uchar g) { return  (((g <<8 ) | g) << 8 |g);}
>>
>>
>>will work as you expect in C/C++ and the python side, where Fl_Color
>>really is
>>always between 0-255.
>>
>>Marcelo
>>
>>
>>[hidden email] wrote:
>>
>>    
>>
>>>Hi
>>>
>>>I guess my original mail got lost in the translation between mail lists, so here I try again:
>>>Sorry if this has been asked before, but I could not find anything in the archive.
>>>In my C++ source code, which I am wrapping for Python, I have a normal enum declaration:
>>>enum Fl_Color {
>>>FL_WHITE = 255;
>>>}
>>>
>>>and I have several C style functions that return this enum, e.g.
>>>Fl_Color get_color(uchar g) { return  (((g <<8 ) | g) << 8 |g);}
>>>
>>>The problem is that the enum gets wrapped in those functions as an int ,
>>>by means of SWIG_From_int. Is there a way I can tell SWIG to treat it as unsigned?
>>>
>>>BTW, I am using swig-1.3.26
>>>
>>>Thanks for your help.
>>>
>>>Andreas Held
>>>http://pyfltk.sourceforge.net
>>>
>>>
>>>
>>>
>>>-------------------------------------------------------
>>>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
>>>
>>>
>>>
>>>      
>>>
>>
>>    
>>
>
>  
>



-------------------------------------------------------
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...