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

Refactoring python to wxArrayString, and better exception error reporting

parent f83a200e
......@@ -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 );
}
......
......@@ -38,6 +38,7 @@
#include <wxstruct.h>
#include <common.h>
#include <colors.h>
#include <macros.h>
/* init functions defined by swig */
......@@ -62,11 +63,11 @@ static int SwigNumModules = 0;
static void swigAddModule( const char* name, void (* initfunc)() )
{
SwigImportInittab[SwigNumModules].name = (char*) name;
SwigImportInittab[SwigNumModules].initfunc = initfunc;
SwigImportInittab[SwigNumModules].name = (char*) name;
SwigImportInittab[SwigNumModules].initfunc = initfunc;
SwigNumModules++;
SwigImportInittab[SwigNumModules].name = (char*) 0;
SwigImportInittab[SwigNumModules].initfunc = 0;
SwigImportInittab[SwigNumModules].name = (char*) 0;
SwigImportInittab[SwigNumModules].initfunc = 0;
}
......@@ -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()"
......@@ -198,7 +199,7 @@ void RedirectStdio()
"output = wx.PyOnDemandOutputWindow()\n"
"sys.stderr = output\n";
PyLOCK lock;
PyLOCK lock;
PyRun_SimpleString( python_redirect );
}
......@@ -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;
}
......@@ -53,4 +53,7 @@ public:
#endif
wxArrayString PyArrayStringToWx( PyObject* arr );
wxString PyErrStringWithTraceback();
#endif // __PYTHON_SCRIPTING_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment