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( !PyString_Check( src ) && !PyUnicode_Check( src ) )
78  {
79  obj = PyObject_Str( src );
80  must_unref_obj = true;
81 
82  if( PyErr_Occurred() )
83  return NULL;
84  }
85 
86  if( PyString_Check( obj ) )
87  {
88  uni_str = PyUnicode_FromEncodedObject( obj, wxPythonEncoding, "strict" );
89  must_unref_str = true;
90 
91  if( PyErr_Occurred() )
92  return NULL;
93  }
94 
95  result = new wxString();
96  size_t len = PyUnicode_GET_SIZE( uni_str );
97 
98  if( len )
99  {
100  PyUnicode_AsWideChar( (PyUnicodeObject*) uni_str,
101  wxStringBuffer( *result, len ), len );
102  }
103 
104  if( must_unref_str )
105  {
106  Py_DECREF( uni_str );
107  }
108 
109  if( must_unref_obj )
110  {
111  Py_DECREF( obj );
112  }
113 
114 #else
115  // normal string (or object) to normal python string
116  PyObject* str = src;
117 
118  if( PyUnicode_Check( src ) ) // if it's unicode convert to normal string
119  {
120  str = PyUnicode_AsEncodedString( src, wxPythonEncoding, "strict" );
121 
122  if( PyErr_Occurred() )
123  return NULL;
124  }
125  else if( !PyString_Check( src ) ) // if it's not a string, str(obj)
126  {
127  str = PyObject_Str( src );
128  must_unref_str = true;
129 
130  if( PyErr_Occurred() )
131  return NULL;
132  }
133 
134  // get the string pointer and size
135  char* str_ptr;
136  Py_ssize_t str_size;
137  PyString_AsStringAndSize( str, &str_ptr, &str_size );
138 
139  // build the wxString from our pointer / size
140  result = new wxString( str_ptr, str_size );
141 
142  if( must_unref_str )
143  {
144  Py_DECREF( str );
145  }
146 
147 #endif
148 
149  return result;
150 }
151 
152 
153 wxString Py2wxString( PyObject* src )
154 {
155  wxString result;
156  wxString* resPtr = newWxStringFromPy( src );
157 
158  // In case of exception clear it and return an empty string
159  if( resPtr==NULL )
160  {
161  PyErr_Clear();
162  return wxEmptyString;
163  }
164 
165  result = *resPtr;
166 
167  delete resPtr;
168 
169  return result;
170 }
171 
172 
173 PyObject* wx2PyString( const wxString& src )
174 {
175  PyObject* str;
176 
177 #if wxUSE_UNICODE
178  str = PyUnicode_FromWideChar( src.c_str(), src.Len() );
179 #else
180  str = PyString_FromStringAndSize( src.c_str(), src.Len() );
181 #endif
182  return str;
183 }
184 
185 
186 void wxSetDefaultPyEncoding( const char* encoding )
187 {
188  strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
190 }
191 
192 
194 {
195  return wxPythonEncoding;
196 }
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
PyObject * wxArrayString2PyList(const wxArrayString &lst)
PyObject * wx2PyString(const wxString &src)
const char * wxGetDefaultPyEncoding()
wxString * newWxStringFromPy(PyObject *src)
#define WX_DEFAULTENCODING_SIZE
wxString Py2wxString(PyObject *src)
void wxSetDefaultPyEncoding(const char *encoding)