diff --git a/pcbnew/swig/pcbnew_action_plugins.cpp b/pcbnew/swig/pcbnew_action_plugins.cpp index f5828ba0d3..f10f0fd58b 100644 --- a/pcbnew/swig/pcbnew_action_plugins.cpp +++ b/pcbnew/swig/pcbnew_action_plugins.cpp @@ -105,27 +105,8 @@ wxString PYTHON_ACTION_PLUGIN::CallRetStrMethod( const char* aMethod, PyObject* PyObject* result = CallMethod( aMethod, aArglist ); - if( result ) - { -#if PY_MAJOR_VERSION >= 3 - const char* str_res = NULL; - PyObject* temp_bytes = PyUnicode_AsEncodedString( result, "UTF-8", "strict" ); - if ( temp_bytes != NULL ) - { - str_res = PyBytes_AS_STRING( temp_bytes ); - str_res = strdup( str_res ); - Py_DECREF( temp_bytes ); - } - else - { - wxLogMessage( "cannot encode unicode python string" ); - } -#else - const char* str_res = PyString_AsString( result ); -#endif - ret = FROM_UTF8( str_res ); - Py_DECREF( result ); - } + ret = PyStringToWx( result ); + Py_XDECREF( result ); return ret; } diff --git a/pcbnew/swig/pcbnew_footprint_wizards.cpp b/pcbnew/swig/pcbnew_footprint_wizards.cpp index b066796609..ce9af4921e 100644 --- a/pcbnew/swig/pcbnew_footprint_wizards.cpp +++ b/pcbnew/swig/pcbnew_footprint_wizards.cpp @@ -105,27 +105,8 @@ wxString PYTHON_FOOTPRINT_WIZARD::CallRetStrMethod( const char* aMethod, PyObjec return ret; } - if( result ) - { -#if PY_MAJOR_VERSION >= 3 - const char* str_res = NULL; - PyObject* temp_bytes = PyUnicode_AsEncodedString( result, "UTF-8", "strict" ); - if ( temp_bytes != NULL ) - { - str_res = PyBytes_AS_STRING( temp_bytes ); - str_res = strdup( str_res ); - Py_DECREF( temp_bytes ); - } - else - { - wxLogMessage( "cannot encode unicode python string" ); - } -#else - const char* str_res = PyString_AsString( result ); -#endif - ret = FROM_UTF8( str_res ); - Py_DECREF( result ); - } + ret = PyStringToWx( result ); + Py_XDECREF( result ); return ret; } @@ -229,27 +210,8 @@ wxString PYTHON_FOOTPRINT_WIZARD::GetParameterPageName( int aPage ) return ret; } - if( result ) - { -#if PY_MAJOR_VERSION >= 3 - const char* str_res = NULL; - PyObject* temp_bytes = PyUnicode_AsEncodedString( result, "UTF-8", "strict" ); - if ( temp_bytes != NULL ) - { - str_res = PyBytes_AS_STRING( temp_bytes ); - str_res = strdup( str_res ); - Py_DECREF( temp_bytes ); - } - else - { - wxLogMessage( "cannot encode unicode python string" ); - } -#else - const char* str_res = PyString_AsString( result ); -#endif - ret = FROM_UTF8( str_res ); - Py_DECREF( result ); - } + ret = PyStringToWx( result ); + Py_XDECREF( result ); return ret; } diff --git a/pcbnew/swig/python_scripting.cpp b/pcbnew/swig/python_scripting.cpp index 9837d6ce6a..bda380926d 100644 --- a/pcbnew/swig/python_scripting.cpp +++ b/pcbnew/swig/python_scripting.cpp @@ -408,6 +408,36 @@ wxWindow* CreatePythonShellWindow( wxWindow* parent, const wxString& aFramenameI #endif +wxString PyStringToWx( PyObject* aString ) +{ + wxString ret; + + if( !aString ) + return ret; + + printf("PyStringToWx\n"); +#if PY_MAJOR_VERSION >= 3 + const char* str_res = NULL; + PyObject* temp_bytes = PyUnicode_AsEncodedString( aString, "UTF-8", "strict" ); + if ( temp_bytes != NULL ) + { + str_res = PyBytes_AS_STRING( temp_bytes ); + ret = FROM_UTF8( str_res ); + Py_DECREF( temp_bytes ); + } + else + { + wxLogMessage( "cannot encode unicode python string" ); + } +#else + const char* str_res = PyString_AsString( aString ); + ret = FROM_UTF8( str_res ); +#endif + + return ret; +} + + wxArrayString PyArrayStringToWx( PyObject* aArrayString ) { wxArrayString ret; @@ -429,9 +459,8 @@ wxArrayString PyArrayStringToWx( PyObject* aArrayString ) if ( temp_bytes != NULL ) { str_res = PyBytes_AS_STRING( temp_bytes ); - str_res = strdup( str_res ); - Py_DECREF( temp_bytes ); ret.Add( FROM_UTF8( str_res ), 1 ); + Py_DECREF( temp_bytes ); } else { diff --git a/pcbnew/swig/python_scripting.h b/pcbnew/swig/python_scripting.h index 927edbf4bf..373cc665a2 100644 --- a/pcbnew/swig/python_scripting.h +++ b/pcbnew/swig/python_scripting.h @@ -107,6 +107,7 @@ public: #endif +wxString PyStringToWx( PyObject* str ); wxArrayString PyArrayStringToWx( PyObject* arr ); wxString PyErrStringWithTraceback();