Commit 31a693cb authored by Miguel Angel Ajo's avatar Miguel Angel Ajo
Browse files

Refactoring python to wxArrayString, and better exception error reporting

parent f83a200e
Loading
Loading
Loading
Loading
+2 −28
Original line number Diff line number Diff line
@@ -63,25 +63,9 @@ PyObject* PYTHON_FOOTPRINT_WIZARD::CallMethod( const char* aMethod, PyObject* aA

        if( PyErr_Occurred() )
        {
            wxString    message;
            PyObject*   t;
            PyObject*   v;
            PyObject*   b;

            PyErr_Fetch( &t, &v, &b );
            message.Printf( wxT( "calling %s()\n"
                                 "Exception: %s\n"
                                 "         : %s\n" ),
                            FROM_UTF8( aMethod ).c_str(),
                            FROM_UTF8( PyString_AsString( PyObject_Str( v ) ) ).c_str(),
                            FROM_UTF8( PyString_AsString( PyObject_Str( b ) ) ).c_str()
                            );

            wxMessageBox( message,
            wxMessageBox( PyErrStringWithTraceback(),
                          wxT( "Exception on python footprint wizard code" ),
                          wxICON_ERROR | wxOK );

            PyErr_Clear();
        }

        if( result )
@@ -140,17 +124,7 @@ wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod( const char* aMet
            return ret;
        }

        int list_size = PyList_Size( result );

        for( int n = 0; n<list_size; n++ )
        {
            PyObject*   element = PyList_GetItem( result, n );

            const char* str_res = PyString_AsString( element );

            str_item = FROM_UTF8( str_res );
            ret.Add( str_item, 1 );
        }
        ret = PyArrayStringToWx( result );

        Py_DECREF( result );
    }
+61 −6
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#include <wxstruct.h>
#include <common.h>
#include <colors.h>
#include <macros.h>

/* init functions defined by swig */

@@ -165,7 +166,7 @@ bool pcbnewInitPythonScripting()
    {
        PyLOCK lock;

        PyRun_SimpleString( "import sys\n"
        PyRun_SimpleString( "import sys, traceback\n"
                            "sys.path.append(\".\")\n"
                            "import pcbnew\n"
                            "pcbnew.LoadPlugins()"
@@ -295,3 +296,57 @@ wxWindow* CreatePythonShellWindow( wxWindow* parent )


#endif

wxArrayString PyArrayStringToWx( PyObject* aArrayString )
{
    wxArrayString   ret;

    int             list_size = PyList_Size( aArrayString );

    for( int n = 0; n<list_size; n++ )
    {
        PyObject* element = PyList_GetItem( aArrayString, n );

        ret.Add( FROM_UTF8( PyString_AsString( element ) ), 1 );
    }

    return ret;
}


wxString PyErrStringWithTraceback()
{
    wxString err;

    if( !PyErr_Occurred() )
        return err;

    PyObject*   type;
    PyObject*   value;
    PyObject*   traceback;

    PyErr_Fetch( &type, &value, &traceback );

    PyObject*   tracebackModuleString = PyString_FromString( (char*) "traceback" );
    PyObject*   tracebackModule = PyImport_Import( tracebackModuleString );


    PyObject*   formatException = PyObject_GetAttrString( tracebackModule,
                                                          (char*) "format_exception" );
    PyObject*   args = Py_BuildValue( "(O,O,O)", type, value, traceback );

    PyObject*   result = PyObject_CallObject( formatException, args );

    Py_DECREF( args );

    wxArrayString res = PyArrayStringToWx( result );

    for( int i = 0; i<res.Count(); i++ )
    {
        err += res[i] + wxT( "\n" );
    }

    PyErr_Clear();

    return err;
}
+3 −0
Original line number Diff line number Diff line
@@ -53,4 +53,7 @@ public:

#endif

wxArrayString   PyArrayStringToWx( PyObject* arr );
wxString        PyErrStringWithTraceback();

#endif    // __PYTHON_SCRIPTING_H