KiCad PCB EDA Suite
wx_python_helpers.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Miguel Angel Ajo <[email protected]>
5  * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <Python.h> // must be first to avoid wx/python typedef conflicts on msvc
31 #include <macros.h>
32 #include <wx/intl.h>
33 #include <wx/string.h>
34 #include <wx/arrstr.h>
35 
36 
37 #define WX_DEFAULTENCODING_SIZE 64
38 
40 
41 
42 PyObject* wxArrayString2PyList( const wxArrayString& lst )
43 {
44  PyObject* list = PyList_New( 0 );
45 
46  for( size_t i = 0; i < lst.GetCount(); i++ )
47  {
48  PyObject* pyStr = PyUnicode_FromString( lst[i].utf8_str() );
49  PyList_Append( list, pyStr );
50  Py_DECREF( pyStr );
51  }
52 
53  return list;
54 }
55 
56 
57 wxString Py2wxString( PyObject* src )
58 {
59  bool must_unref_str = false;
60  bool must_unref_obj = false;
61 
62  wxString result;
63 
64  PyObject* obj = src;
65  PyObject* uni_str = src;
66 
67  // if not an str or unicode, try to str(src)
68  if( !PyBytes_Check( src ) && !PyUnicode_Check( src ) )
69  {
70  obj = PyObject_Str( src );
71  uni_str = obj; // in case of Python 3 our string is already correctly encoded
72  must_unref_obj = true;
73 
74  if( PyErr_Occurred() )
75  return result;
76  }
77 
78  if( PyBytes_Check( obj ) )
79  {
80  uni_str = PyUnicode_FromEncodedObject( obj, wxPythonEncoding, "strict" );
81  must_unref_str = true;
82 
83  if( PyErr_Occurred() )
84  return result;
85  }
86 
87  size_t len = PyUnicode_GET_LENGTH( uni_str );
88 
89  if( len )
90  result = FROM_UTF8( PyUnicode_AsUTF8( uni_str ) );
91 
92  if( must_unref_str )
93  {
94  Py_DECREF( uni_str );
95  }
96 
97  if( must_unref_obj )
98  {
99  Py_DECREF( obj );
100  }
101 
102 
103  return result;
104 }
105 
106 
107 PyObject* wx2PyString( const wxString& src )
108 {
109  return PyUnicode_FromString( src.utf8_str() );
110 }
111 
112 
113 void wxSetDefaultPyEncoding( const char* encoding )
114 {
115  strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
117 }
118 
119 
121 {
122  return wxPythonEncoding;
123 }
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
PyObject * wxArrayString2PyList(const wxArrayString &lst)
PyObject * wx2PyString(const wxString &src)
This file contains miscellaneous commonly used macros and functions.
const char * wxGetDefaultPyEncoding()
#define WX_DEFAULTENCODING_SIZE
wxString Py2wxString(PyObject *src)
void wxSetDefaultPyEncoding(const char *encoding)