Rewrite C extensions to also support Python 3

This commit is contained in:
Thomas Pointhuber 2018-08-03 15:23:32 +02:00 committed by Maciej Suminski
parent de7b362695
commit 284904b72c
4 changed files with 65 additions and 1 deletions

View File

@ -74,7 +74,11 @@ wxString* newWxStringFromPy( PyObject* src )
PyObject* uni_str = src;
// if not an str or unicode, try to str(src)
#if PY_MAJOR_VERSION >= 3
if( !PyBytes_Check( src ) && !PyUnicode_Check( src ) )
#else
if( !PyString_Check( src ) && !PyUnicode_Check( src ) )
#endif
{
obj = PyObject_Str( src );
must_unref_obj = true;
@ -83,7 +87,11 @@ wxString* newWxStringFromPy( PyObject* src )
return NULL;
}
#if PY_MAJOR_VERSION >= 3
if( PyBytes_Check( obj ) )
#else
if( PyString_Check( obj ) )
#endif
{
uni_str = PyUnicode_FromEncodedObject( obj, wxPythonEncoding, "strict" );
must_unref_str = true;
@ -97,8 +105,13 @@ wxString* newWxStringFromPy( PyObject* src )
if( len )
{
#if PY_MAJOR_VERSION >= 3
PyUnicode_AsWideChar( uni_str,
wxStringBuffer( *result, len ), len );
#else
PyUnicode_AsWideChar( (PyUnicodeObject*) uni_str,
wxStringBuffer( *result, len ), len );
#endif
}
if( must_unref_str )
@ -122,7 +135,11 @@ wxString* newWxStringFromPy( PyObject* src )
if( PyErr_Occurred() )
return NULL;
}
#if PY_MAJOR_VERSION >= 3
else if( !PyBytes_Check( src ) )
#else
else if( !PyString_Check( src ) ) // if it's not a string, str(obj)
#endif
{
str = PyObject_Str( src );
must_unref_str = true;

View File

@ -107,7 +107,11 @@ wxString PYTHON_ACTION_PLUGIN::CallRetStrMethod( const char* aMethod, PyObject*
if( result )
{
#if PY_MAJOR_VERSION >= 3
const char* str_res = PyBytes_AS_STRING( result );
#else
const char* str_res = PyString_AsString( result );
#endif
ret = FROM_UTF8( str_res );
Py_DECREF( result );
}

View File

@ -101,7 +101,11 @@ wxString PYTHON_FOOTPRINT_WIZARD::CallRetStrMethod( const char* aMethod, PyObjec
if( result )
{
#if PY_MAJOR_VERSION >= 3
const char* str_res = PyBytes_AS_STRING( result );
#else
const char* str_res = PyString_AsString( result );
#endif
ret = FROM_UTF8( str_res );
Py_DECREF( result );
}
@ -173,10 +177,17 @@ int PYTHON_FOOTPRINT_WIZARD::GetNumParameterPages()
if( result )
{
#if PY_MAJOR_VERSION >= 3
if( !PyLong_Check( result ) )
return -1;
ret = PyLong_AsLong( result );
#else
if( !PyInt_Check( result ) )
return -1;
ret = PyInt_AsLong( result );
#endif
Py_DECREF( result );
}
@ -197,7 +208,11 @@ wxString PYTHON_FOOTPRINT_WIZARD::GetParameterPageName( int aPage )
if( result )
{
#if PY_MAJOR_VERSION >= 3
const char* str_res = PyBytes_AS_STRING( result );
#else
const char* str_res = PyString_AsString( result );
#endif
ret = FROM_UTF8( str_res );
Py_DECREF( result );
}
@ -305,7 +320,11 @@ wxString PYTHON_FOOTPRINT_WIZARD::SetParameterValues( int aPage, wxArrayString&
for( int i = 0; i < len; i++ )
{
wxString& str = aValues[i];
#if PY_MAJOR_VERSION >= 3
PyObject* py_str = PyUnicode_FromString( (const char*) str.mb_str() );
#else
PyObject* py_str = PyString_FromString( (const char*) str.mb_str() );
#endif
PyList_SetItem( py_list, i, py_str );
}

View File

@ -41,9 +41,15 @@
/* init functions defined by swig */
#if PY_MAJOR_VERSION >= 3
extern "C" PyObject* init_kicad( void );
extern "C" PyObject* init_pcbnew( void );
#else
extern "C" void init_kicad( void );
extern "C" void init_pcbnew( void );
#endif
#define EXTRA_PYTHON_MODULES 10 // this is the number of python
// modules that we want to add into the list
@ -67,7 +73,11 @@ bool IsWxPythonLoaded()
/* Add a name + initfuction to our SwigImportInittab */
#if PY_MAJOR_VERSION >= 3
static void swigAddModule( const char* name, PyObject* (* initfunc)() )
#else
static void swigAddModule( const char* name, void (* initfunc)() )
#endif
{
SwigImportInittab[SwigNumModules].name = (char*) name;
SwigImportInittab[SwigNumModules].initfunc = initfunc;
@ -236,7 +246,11 @@ static void pcbnewRunPythonMethodWithReturnedString( const char* aMethodName, wx
if( pobj )
{
PyObject* str = PyDict_GetItemString(localDict, "result" );
#if PY_MAJOR_VERSION >= 3
const char* str_res = str ? PyBytes_AS_STRING( str ) : 0;
#else
const char* str_res = str ? PyString_AsString( str ) : 0;
#endif
aNames = FROM_UTF8( str_res );
Py_DECREF( pobj );
}
@ -386,7 +400,13 @@ wxArrayString PyArrayStringToWx( PyObject* aArrayString )
PyObject* element = PyList_GetItem( aArrayString, n );
if( element )
{
#if PY_MAJOR_VERSION >= 3
ret.Add( FROM_UTF8( PyBytes_AS_STRING( element ) ), 1 );
#else
ret.Add( FROM_UTF8( PyString_AsString( element ) ), 1 );
#endif
}
}
return ret;
@ -406,7 +426,11 @@ wxString PyErrStringWithTraceback()
PyErr_Fetch( &type, &value, &traceback );
#if PY_MAJOR_VERSION >= 3
PyObject* tracebackModuleString = PyUnicode_FromString( "traceback" );
#else
PyObject* tracebackModuleString = PyString_FromString( "traceback" );
#endif
PyObject* tracebackModule = PyImport_Import( tracebackModuleString );
Py_DECREF( tracebackModuleString );