2012-03-17 17:30:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Miguel Angel Ajo <miguelangel@nbee.es>
|
|
|
|
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file wx_python_helpers.cpp
|
|
|
|
* @brief Python wrapping helpers for wx structures/objects
|
|
|
|
*/
|
2021-04-27 02:20:02 +00:00
|
|
|
|
|
|
|
#include <Python.h> // must be first to avoid wx/python typedef conflicts on msvc
|
2021-03-21 02:43:26 +00:00
|
|
|
#include <macros.h>
|
2012-03-10 21:40:41 +00:00
|
|
|
#include <wx/intl.h>
|
|
|
|
#include <wx/string.h>
|
2012-04-23 06:59:37 +00:00
|
|
|
#include <wx/arrstr.h>
|
|
|
|
|
|
|
|
|
2012-03-10 21:40:41 +00:00
|
|
|
#define WX_DEFAULTENCODING_SIZE 64
|
|
|
|
|
|
|
|
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE] = "ascii";
|
|
|
|
|
2012-04-23 06:59:37 +00:00
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
PyObject* wxArrayString2PyList( const wxArrayString& lst )
|
2012-04-23 06:59:37 +00:00
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
PyObject* list = PyList_New( 0 );
|
|
|
|
|
|
|
|
for( size_t i = 0; i < lst.GetCount(); i++ )
|
|
|
|
{
|
2021-03-21 02:43:26 +00:00
|
|
|
PyObject* pyStr = PyUnicode_FromString( lst[i].utf8_str() );
|
2012-06-27 21:19:19 +00:00
|
|
|
PyList_Append( list, pyStr );
|
|
|
|
Py_DECREF( pyStr );
|
2012-04-23 06:59:37 +00:00
|
|
|
}
|
2012-06-27 21:19:19 +00:00
|
|
|
|
2012-04-23 06:59:37 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-21 02:43:26 +00:00
|
|
|
wxString Py2wxString( PyObject* src )
|
2012-06-27 21:19:19 +00:00
|
|
|
{
|
|
|
|
bool must_unref_str = false;
|
2021-03-21 02:43:26 +00:00
|
|
|
bool must_unref_obj = false;
|
2012-04-23 06:59:37 +00:00
|
|
|
|
2021-03-21 02:43:26 +00:00
|
|
|
wxString result;
|
2012-03-10 21:40:41 +00:00
|
|
|
|
2021-03-21 02:43:26 +00:00
|
|
|
PyObject* obj = src;
|
2012-06-27 21:19:19 +00:00
|
|
|
PyObject* uni_str = src;
|
2012-03-10 21:40:41 +00:00
|
|
|
|
|
|
|
// if not an str or unicode, try to str(src)
|
2018-08-03 13:23:32 +00:00
|
|
|
if( !PyBytes_Check( src ) && !PyUnicode_Check( src ) )
|
2012-03-10 21:40:41 +00:00
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
obj = PyObject_Str( src );
|
2018-08-06 10:31:50 +00:00
|
|
|
uni_str = obj; // in case of Python 3 our string is already correctly encoded
|
2012-06-27 21:19:19 +00:00
|
|
|
must_unref_obj = true;
|
|
|
|
|
|
|
|
if( PyErr_Occurred() )
|
2021-03-21 02:43:26 +00:00
|
|
|
return result;
|
2012-03-10 21:40:41 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 13:23:32 +00:00
|
|
|
if( PyBytes_Check( obj ) )
|
2012-03-10 21:40:41 +00:00
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
uni_str = PyUnicode_FromEncodedObject( obj, wxPythonEncoding, "strict" );
|
2012-03-10 21:40:41 +00:00
|
|
|
must_unref_str = true;
|
2012-06-27 21:19:19 +00:00
|
|
|
|
|
|
|
if( PyErr_Occurred() )
|
2021-03-21 02:43:26 +00:00
|
|
|
return result;
|
2012-03-10 21:40:41 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:31:50 +00:00
|
|
|
size_t len = PyUnicode_GET_LENGTH( uni_str );
|
2012-06-27 21:19:19 +00:00
|
|
|
|
|
|
|
if( len )
|
2021-03-21 02:43:26 +00:00
|
|
|
result = FROM_UTF8( PyUnicode_AsUTF8( uni_str ) );
|
2012-03-10 21:40:41 +00:00
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
if( must_unref_str )
|
2014-03-27 17:45:05 +00:00
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
Py_DECREF( uni_str );
|
2014-03-27 17:45:05 +00:00
|
|
|
}
|
2012-06-27 21:19:19 +00:00
|
|
|
|
|
|
|
if( must_unref_obj )
|
2014-03-27 17:45:05 +00:00
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
Py_DECREF( obj );
|
2014-03-27 17:45:05 +00:00
|
|
|
}
|
2012-06-27 21:19:19 +00:00
|
|
|
|
2012-03-10 21:40:41 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
PyObject* wx2PyString( const wxString& src )
|
2012-03-10 21:40:41 +00:00
|
|
|
{
|
2021-03-21 02:43:26 +00:00
|
|
|
return PyUnicode_FromString( src.utf8_str() );
|
2012-03-10 21:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
void wxSetDefaultPyEncoding( const char* encoding )
|
2012-03-10 21:40:41 +00:00
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
|
2015-01-18 11:49:32 +00:00
|
|
|
wxPythonEncoding[ WX_DEFAULTENCODING_SIZE - 1 ] = '\0';
|
2012-03-10 21:40:41 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
|
2012-03-10 21:40:41 +00:00
|
|
|
const char* wxGetDefaultPyEncoding()
|
|
|
|
{
|
|
|
|
return wxPythonEncoding;
|
|
|
|
}
|