← Back to team overview

kicad-developers team mailing list archive

Re: Python binding of enums

 

On 08/09/2012 03:33 AM, Miguel Angel Ajo Pelayo wrote:
> :-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..)


Ouch.

Tread carefully.

The value proposition (and acceptance contract on the part of C++ developers to tolerate
SWIG) was that all the magic can be done in the SWIG interface files.

If that contract needs to be renegotiated, then lets begin the process of renegotiation,
rather than starting immediately into a breach of contract.    I for one want the ugliness
out of the C++ code.

Warning, warning, warning ...




Follow ups

References