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 <miguelangel@nbee.es>
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>
31 #include <wx/intl.h>
32 #include <wx/string.h>
33 #include <wx/arrstr.h>
34 
35 
36 #define WX_DEFAULTENCODING_SIZE 64
37 
39 
40 
41 PyObject* wxArrayString2PyList( const wxArrayString& lst )
42 {
43  PyObject* list = PyList_New( 0 );
44 
45  for( size_t i = 0; i < lst.GetCount(); i++ )
46  {
47 #if wxUSE_UNICODE
48  PyObject* pyStr = PyUnicode_FromWideChar( lst[i].c_str(),
49  lst[i].Len()
50  );
51 #else
52  PyObject* pyStr = PyString_FromStringAndSize( lst[i].c_str(),
53  lst[i].Len()
54  );
55 #endif
56  PyList_Append( list, pyStr );
57  Py_DECREF( pyStr );
58  }
59 
60  return list;
61 }
62 
63 
64 wxString* newWxStringFromPy( PyObject* src )
65 {
66  bool must_unref_str = false;
67 
68  wxString* result = NULL;
69  PyObject* obj = src;
70 
71 #if wxUSE_UNICODE
72  bool must_unref_obj = false;
73  // Unicode string to python unicode string
74  PyObject* uni_str = src;
75 
76  // if not an str or unicode, try to str(src)
77 #if PY_MAJOR_VERSION >= 3
78  if( !PyBytes_Check( src ) && !PyUnicode_Check( src ) )
79 #else
80  if( !PyString_Check( src ) && !PyUnicode_Check( src ) )
81 #endif
82  {
83  obj = PyObject_Str( src );
84 
85 #if PY_MAJOR_VERSION >= 3
86  uni_str = obj; // in case of Python 3 our string is already correctly encoded
87 #endif
88 
89  must_unref_obj = true;
90 
91  if( PyErr_Occurred() )
92  return NULL;
93  }
94 
95 #if PY_MAJOR_VERSION >= 3
96  if( PyBytes_Check( obj ) )
97 #else
98  if( PyString_Check( obj ) )
99 #endif
100  {
101  uni_str = PyUnicode_FromEncodedObject( obj, wxPythonEncoding, "strict" );
102  must_unref_str = true;
103 
104  if( PyErr_Occurred() )
105  return NULL;
106  }
107 
108  result = new wxString();
109 #if PY_MAJOR_VERSION >= 3
110  size_t len = PyUnicode_GET_LENGTH( uni_str );
111 #else
112  size_t len = PyUnicode_GET_SIZE( uni_str );
113 #endif
114 
115  if( len )
116  {
117 #if PY_MAJOR_VERSION >= 3
118  PyUnicode_AsWideChar( uni_str,
119  wxStringBuffer( *result, len ), len );
120 #else
121  PyUnicode_AsWideChar( (PyUnicodeObject*) uni_str,
122  wxStringBuffer( *result, len ), len );
123 #endif
124  }
125 
126  if( must_unref_str )
127  {
128  Py_DECREF( uni_str );
129  }
130 
131  if( must_unref_obj )
132  {
133  Py_DECREF( obj );
134  }
135 
136 #else
137  // normal string (or object) to normal python string
138  PyObject* str = src;
139 
140  if( PyUnicode_Check( src ) ) // if it's unicode convert to normal string
141  {
142  str = PyUnicode_AsEncodedString( src, wxPythonEncoding, "strict" );
143 
144  if( PyErr_Occurred() )
145  return NULL;
146  }
147 #if PY_MAJOR_VERSION >= 3
148  else if( !PyUnicode_Check( src ) )
149 #else
150  else if( !PyString_Check( src ) ) // if it's not a string, str(obj)
151 #endif
152  {
153  str = PyObject_Str( src );
154  must_unref_str = true;
155 
156  if( PyErr_Occurred() )
157  return NULL;
158  }
159 
160  // get the string pointer and size
161  char* str_ptr;
162  Py_ssize_t str_size;
163  PyString_AsStringAndSize( str, &str_ptr, &str_size );
164 
165  // build the wxString from our pointer / size
166  result = new wxString( str_ptr, str_size );
167 
168  if( must_unref_str )
169  {
170  Py_DECREF( str );
171  }
172 
173 #endif
174 
175  return result;
176 }
177 
178 
179 wxString Py2wxString( PyObject* src )
180 {
181  wxString result;
182  wxString* resPtr = newWxStringFromPy( src );
183 
184  // In case of exception clear it and return an empty string
185  if( resPtr==NULL )
186  {
187  PyErr_Clear();
188  return wxEmptyString;
189  }
190 
191  result = *resPtr;
192 
193  delete resPtr;
194 
195  return result;
196 }
197 
198 
199 PyObject* wx2PyString( const wxString& src )
200 {
201  PyObject* str;
202 
203 #if wxUSE_UNICODE
204  str = PyUnicode_FromWideChar( src.c_str(), src.Len() );
205 #else
206  str = PyString_FromStringAndSize( src.c_str(), src.Len() );
207 #endif
208  return str;
209 }
210 
211 
212 void wxSetDefaultPyEncoding( const char* encoding )
213 {
214  strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
216 }
217 
218 
220 {
221  return wxPythonEncoding;
222 }
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
PyObject * wxArrayString2PyList(const wxArrayString &lst)
const string & str
Definition: json11.cpp:596
PyObject * wx2PyString(const wxString &src)
const char * wxGetDefaultPyEncoding()
wxString * newWxStringFromPy(PyObject *src)
#define WX_DEFAULTENCODING_SIZE
wxString Py2wxString(PyObject *src)
size_t i
Definition: json11.cpp:597
void wxSetDefaultPyEncoding(const char *encoding)