Refactoring python to wxArrayString, and better exception error reporting
This commit is contained in:
parent
f83a200e5e
commit
31a693cb8d
|
@ -63,25 +63,9 @@ PyObject* PYTHON_FOOTPRINT_WIZARD::CallMethod( const char* aMethod, PyObject* aA
|
||||||
|
|
||||||
if( PyErr_Occurred() )
|
if( PyErr_Occurred() )
|
||||||
{
|
{
|
||||||
wxString message;
|
wxMessageBox( PyErrStringWithTraceback(),
|
||||||
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,
|
|
||||||
wxT( "Exception on python footprint wizard code" ),
|
wxT( "Exception on python footprint wizard code" ),
|
||||||
wxICON_ERROR | wxOK );
|
wxICON_ERROR | wxOK );
|
||||||
|
|
||||||
PyErr_Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if( result )
|
if( result )
|
||||||
|
@ -140,17 +124,7 @@ wxArrayString PYTHON_FOOTPRINT_WIZARD::CallRetArrayStrMethod( const char* aMet
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int list_size = PyList_Size( result );
|
ret = PyArrayStringToWx( 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 );
|
|
||||||
}
|
|
||||||
|
|
||||||
Py_DECREF( result );
|
Py_DECREF( result );
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
#include <wxstruct.h>
|
#include <wxstruct.h>
|
||||||
#include <common.h>
|
#include <common.h>
|
||||||
#include <colors.h>
|
#include <colors.h>
|
||||||
|
#include <macros.h>
|
||||||
|
|
||||||
/* init functions defined by swig */
|
/* init functions defined by swig */
|
||||||
|
|
||||||
|
@ -62,11 +63,11 @@ static int SwigNumModules = 0;
|
||||||
|
|
||||||
static void swigAddModule( const char* name, void (* initfunc)() )
|
static void swigAddModule( const char* name, void (* initfunc)() )
|
||||||
{
|
{
|
||||||
SwigImportInittab[SwigNumModules].name = (char*) name;
|
SwigImportInittab[SwigNumModules].name = (char*) name;
|
||||||
SwigImportInittab[SwigNumModules].initfunc = initfunc;
|
SwigImportInittab[SwigNumModules].initfunc = initfunc;
|
||||||
SwigNumModules++;
|
SwigNumModules++;
|
||||||
SwigImportInittab[SwigNumModules].name = (char*) 0;
|
SwigImportInittab[SwigNumModules].name = (char*) 0;
|
||||||
SwigImportInittab[SwigNumModules].initfunc = 0;
|
SwigImportInittab[SwigNumModules].initfunc = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -165,7 +166,7 @@ bool pcbnewInitPythonScripting()
|
||||||
{
|
{
|
||||||
PyLOCK lock;
|
PyLOCK lock;
|
||||||
|
|
||||||
PyRun_SimpleString( "import sys\n"
|
PyRun_SimpleString( "import sys, traceback\n"
|
||||||
"sys.path.append(\".\")\n"
|
"sys.path.append(\".\")\n"
|
||||||
"import pcbnew\n"
|
"import pcbnew\n"
|
||||||
"pcbnew.LoadPlugins()"
|
"pcbnew.LoadPlugins()"
|
||||||
|
@ -198,7 +199,7 @@ void RedirectStdio()
|
||||||
"output = wx.PyOnDemandOutputWindow()\n"
|
"output = wx.PyOnDemandOutputWindow()\n"
|
||||||
"sys.stderr = output\n";
|
"sys.stderr = output\n";
|
||||||
|
|
||||||
PyLOCK lock;
|
PyLOCK lock;
|
||||||
|
|
||||||
PyRun_SimpleString( python_redirect );
|
PyRun_SimpleString( python_redirect );
|
||||||
}
|
}
|
||||||
|
@ -295,3 +296,57 @@ wxWindow* CreatePythonShellWindow( wxWindow* parent )
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#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
|
#endif
|
||||||
|
|
||||||
|
wxArrayString PyArrayStringToWx( PyObject* arr );
|
||||||
|
wxString PyErrStringWithTraceback();
|
||||||
|
|
||||||
#endif // __PYTHON_SCRIPTING_H
|
#endif // __PYTHON_SCRIPTING_H
|
||||||
|
|
Loading…
Reference in New Issue