Update Python Helpers

This commit is contained in:
Seth Hillbrand 2021-03-20 19:43:26 -07:00
parent 11592e6eae
commit 88e4b7f7c4
4 changed files with 19 additions and 122 deletions

View File

@ -270,55 +270,27 @@ public:
// wxString typemaps
%typemap(in) wxString {
wxString* sptr = newWxStringFromPy($input);
if (sptr == NULL) SWIG_fail;
$1 = *sptr;
delete sptr;
$1 = Py2wxString($input);
}
%typemap(out) wxString
{
%#if wxUSE_UNICODE
$result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
%#else
$result = PyString_FromStringAndSize($1.c_str(), $1.Len());
%#endif
$result = PyUnicode_FromString($1.utf8_str());
}
%typemap(varout) wxString
{
%#if wxUSE_UNICODE
$result = PyUnicode_FromWideChar($1.c_str(), $1.Len());
%#else
$result = PyString_FromStringAndSize($1.c_str(), $1.Len());
%#endif
}
%typemap(in) wxString& (bool temp=false)
{
$1 = newWxStringFromPy($input);
if ($1 == NULL) SWIG_fail;
temp = true;
$result = PyUnicode_FromString($1.utf8_str());
}
%typemap(out) wxString&
{
%#if wxUSE_UNICODE
$result = PyUnicode_FromWideChar($1->c_str(), $1->Len());
%#else
$result = PyString_FromStringAndSize($1->c_str(), $1->Len());
%#endif
$result = PyUnicode_FromString($1->utf8_str());
}
%typemap(freearg) wxString&
{
if (temp$argnum)
delete $1;
}
%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) wxString& {
$1 = PyString_Check($input) || PyUnicode_Check($input);
@ -341,11 +313,10 @@ public:
for (int i=0; i<last; i++)
{
PyObject* pyStr = PySequence_GetItem($input, i);
wxString* wxS = newWxStringFromPy(pyStr);
wxString wxS = Py2wxString(pyStr);
if (PyErr_Occurred())
SWIG_fail;
$1->Add(*wxS);
delete wxS;
$1->Add(wxS);
Py_DECREF(pyStr);
}
}

View File

@ -26,7 +26,7 @@
* @file wx_python_helpers.cpp
* @brief Python wrapping helpers for wx structures/objects
*/
#include <macros.h>
#include <Python.h>
#include <wx/intl.h>
#include <wx/string.h>
@ -44,15 +44,7 @@ PyObject* wxArrayString2PyList( const wxArrayString& lst )
for( size_t i = 0; i < lst.GetCount(); i++ )
{
#if wxUSE_UNICODE
PyObject* pyStr = PyUnicode_FromWideChar( lst[i].c_str(),
lst[i].Len()
);
#else
PyObject* pyStr = PyString_FromStringAndSize( lst[i].c_str(),
lst[i].Len()
);
#endif
PyObject* pyStr = PyUnicode_FromString( lst[i].utf8_str() );
PyList_Append( list, pyStr );
Py_DECREF( pyStr );
}
@ -61,16 +53,14 @@ PyObject* wxArrayString2PyList( const wxArrayString& lst )
}
wxString* newWxStringFromPy( PyObject* src )
wxString Py2wxString( PyObject* src )
{
bool must_unref_str = false;
wxString* result = NULL;
PyObject* obj = src;
#if wxUSE_UNICODE
bool must_unref_obj = false;
// Unicode string to python unicode string
wxString result;
PyObject* obj = src;
PyObject* uni_str = src;
// if not an str or unicode, try to str(src)
@ -81,7 +71,7 @@ wxString* newWxStringFromPy( PyObject* src )
must_unref_obj = true;
if( PyErr_Occurred() )
return NULL;
return result;
}
if( PyBytes_Check( obj ) )
@ -90,16 +80,13 @@ wxString* newWxStringFromPy( PyObject* src )
must_unref_str = true;
if( PyErr_Occurred() )
return NULL;
return result;
}
result = new wxString();
size_t len = PyUnicode_GET_LENGTH( uni_str );
if( len )
{
PyUnicode_AsWideChar( uni_str, wxStringBuffer( *result, len ), len );
}
result = FROM_UTF8( PyUnicode_AsUTF8( uni_str ) );
if( must_unref_str )
{
@ -111,60 +98,6 @@ wxString* newWxStringFromPy( PyObject* src )
Py_DECREF( obj );
}
#else
// normal string (or object) to normal python string
PyObject* str = src;
if( PyUnicode_Check( src ) ) // if it's unicode convert to normal string
{
str = PyUnicode_AsEncodedString( src, wxPythonEncoding, "strict" );
if( PyErr_Occurred() )
return NULL;
}
else if( !PyUnicode_Check( src ) )
{
str = PyObject_Str( src );
must_unref_str = true;
if( PyErr_Occurred() )
return NULL;
}
// get the string pointer and size
char* str_ptr;
Py_ssize_t str_size;
PyString_AsStringAndSize( str, &str_ptr, &str_size );
// build the wxString from our pointer / size
result = new wxString( str_ptr, str_size );
if( must_unref_str )
{
Py_DECREF( str );
}
#endif
return result;
}
wxString Py2wxString( PyObject* src )
{
wxString result;
wxString* resPtr = newWxStringFromPy( src );
// In case of exception clear it and return an empty string
if( resPtr==NULL )
{
PyErr_Clear();
return wxEmptyString;
}
result = *resPtr;
delete resPtr;
return result;
}
@ -172,14 +105,7 @@ wxString Py2wxString( PyObject* src )
PyObject* wx2PyString( const wxString& src )
{
PyObject* str;
#if wxUSE_UNICODE
str = PyUnicode_FromWideChar( src.c_str(), src.Len() );
#else
str = PyString_FromStringAndSize( src.c_str(), src.Len() );
#endif
return str;
return PyUnicode_FromString( src.utf8_str() );
}

View File

@ -8,7 +8,6 @@
PyObject* wxArrayString2PyList( const wxArrayString& lst );
wxString* newWxStringFromPy( PyObject* source );
wxString Py2wxString( PyObject* source );
PyObject* wx2PyString( const wxString& src );

View File

@ -404,6 +404,7 @@ set( SWIG_FLAGS
-I${CMAKE_CURRENT_SOURCE_DIR}
-I${CMAKE_SOURCE_DIR}/include
-I${CMAKE_SOURCE_DIR}/scripting
-I${CMAKE_SOURCE_DIR}/pcbnew/python/scripting
-I${CMAKE_SOURCE_DIR}/common/swig
-I${CMAKE_SOURCE_DIR}/libs/kimath/include
)
@ -479,7 +480,7 @@ add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/pcbnew_wrap.cxx
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/docstrings/docstrings.i
COMMAND ${SWIG_EXECUTABLE}
${SWIG_OPTS} -o ${CMAKE_CURRENT_BINARY_DIR}/pcbnew_wrap.cxx swig/pcbnew.i
${SWIG_OPTS} -o ${CMAKE_CURRENT_BINARY_DIR}/pcbnew_wrap.cxx ${CMAKE_SOURCE_DIR}/pcbnew/python/swig/pcbnew.i
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_SOURCE_DIR}/scripting/build_tools/fix_swig_imports.py