← Back to team overview

kicad-developers team mailing list archive

Re: Python binding of enums

 

:-P :)

/*
----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 2.0.4
 *
 * This file is not intended to be easily readable and contains a number of
 * coding conventions designed to improve portability and efficiency. Do
not make
 * changes to this file unless you know what you are doing--modify the SWIG
 * interface file instead.
 *
-----------------------------------------------------------------------------
*/


After the joke,


This is one piece of C++ code that checks for PCB_FILE_T parameter of
LoadBoard

WIGINTERN PyObject *_wrap_LoadBoard__SWIG_0(PyObject *SWIGUNUSEDPARM(self),
PyObject *args) {
  PyObject *resultobj = 0;
  wxString *arg1 = 0 ;
  IO_MGR::PCB_FILE_T arg2 ;
  bool temp1 = false ;
  int val2 ;
  int ecode2 = 0 ;
  PyObject * obj0 = 0 ;
  PyObject * obj1 = 0 ;
  BOARD *result = 0 ;

  if (!PyArg_ParseTuple(args,(char *)"OO:LoadBoard",&obj0,&obj1)) SWIG_fail;
  {
    arg1 = newWxStringFromPy(obj0);
    if (arg1 == NULL) SWIG_fail;
    temp1 = true;
  }
 * ecode2 = SWIG_AsVal_int(obj1, &val2);*
  if (!SWIG_IsOK(ecode2)) {
    *SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "LoadBoard"
"', argument " "2"" of type '" "IO_MGR::PCB_FILE_T""'");*
  }
  arg2 = static_cast< IO_MGR::PCB_FILE_T >(val2);
  {
    try{
      result = (BOARD *)LoadBoard(*arg1,arg2);
    }
    catch( IO_ERROR e )
    {
      char ExceptionError[256];
      sprintf(ExceptionError, "%s\n", TO_UTF8(e.errorText) );
      PyErr_SetString(PyExc_IOError,ExceptionError);
      return NULL;
    }
    catch( ... )
    {
      SWIG_fail;
    }



As you said, it only checks that it's an "int", and it does an staic cast
to PCB_FILE_T,


It won't be a problem in most situations (where user is dependent from the
ENUMS to know what he's calling),
but for layer numbers, layer colors, etc, it can be a problem, since user
can start tweaking the parameters, so
better do a compare, and throw an exception,

if you tell me what kind of exception you can throw, I could set it up to
convert the exception back to python-land.

Do we have some kind of exception for wrong parameters? It could make sense
now, but not for all situations, only
scripting-accesible functions, where enums could be used also as numbers
(layer number, color number, etc..)



2012/8/9 Miguel Angel Ajo Pelayo <miguelangel@xxxxxxx>:
> 2012/8/9 Lorenzo Marcantonio <l.marcantonio@xxxxxxxxxxxx>:
>> I'm preparing the plot option class for script binding and I looked into
>> the generated swig stuff (horrible stuff indeed!).
>
> Haha, that .py and .cpp is not for humans, but for python and g++ :-)
>
>> What concernes me are
>> enums... in its own typelessness it seems that python handles them as
>> int. Also it seems to me that no domain checking is done by SWIG (unless
>> I missed some magical function)
>
> I'm not sure if it's checked in the C++ part of the wrapper, (cannot
> tell you, never checked it),
> but they do a ton of run-time checks for types, throwing python
> exceptions for bad situations.
>
> But for enums, I don't know, I'm checking and I tell you in some minutes.
>
>>
>> Am I right? Then I'd have to handle domain validation at least in the
>> accessor for the members. Example: colors; they go for 0 to 23 and are
>> in an enum so they are moderately checked at least by the compiler (I
>> have a ColorFromInt which asserts this fact);
>> after binding SetColor(100) would be accepted by python and trigger
>> a segfault on access (that's not good in my book).
>
> We should avoid that situation.
>
>>
>> It's all of this correct?
>>
>   I'm checking :)
>>
>> --
>> Lorenzo Marcantonio
>> Logos Srl
>>
>> _______________________________________________
>> Mailing list: https://launchpad.net/~kicad-developers
>> Post to     : kicad-developers@xxxxxxxxxxxxxxxxxxx
>> Unsubscribe : https://launchpad.net/~kicad-developers
>> More help   : https://help.launchpad.net/ListHelp
>
>
>
> --
>
> Miguel Angel Ajo Pelayo
> http://www.nbee.es
> +34 636 52 25 69
> skype: ajoajoajo



-- 

Miguel Angel Ajo Pelayo
http://www.nbee.es
+34 636 52 25 69
skype: ajoajoajo

Follow ups

References