Introduce PyStringToWx helper to remove code duplication

This commit is contained in:
Thomas Pointhuber 2018-10-12 21:36:50 +02:00 committed by Maciej Suminski
parent ce471ec898
commit e09785db1d
4 changed files with 38 additions and 65 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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
{

View File

@ -107,6 +107,7 @@ public:
#endif
wxString PyStringToWx( PyObject* str );
wxArrayString PyArrayStringToWx( PyObject* arr );
wxString PyErrStringWithTraceback();