KiCad PCB EDA Suite
wx_python_helpers.cpp File Reference

Python wrapping helpers for wx structures/objects. More...

#include <Python.h>
#include <wx/intl.h>
#include <wx/string.h>
#include <wx/arrstr.h>

Go to the source code of this file.

Macros

#define WX_DEFAULTENCODING_SIZE   64
 

Functions

PyObject * wxArrayString2PyList (const wxArrayString &lst)
 
wxString * newWxStringFromPy (PyObject *src)
 
wxString Py2wxString (PyObject *src)
 
PyObject * wx2PyString (const wxString &src)
 
void wxSetDefaultPyEncoding (const char *encoding)
 
const char * wxGetDefaultPyEncoding ()
 

Variables

static char wxPythonEncoding [WX_DEFAULTENCODING_SIZE] = "ascii"
 

Detailed Description

Python wrapping helpers for wx structures/objects.

Definition in file wx_python_helpers.cpp.

Macro Definition Documentation

#define WX_DEFAULTENCODING_SIZE   64

Definition at line 36 of file wx_python_helpers.cpp.

Referenced by wxSetDefaultPyEncoding().

Function Documentation

wxString* newWxStringFromPy ( PyObject *  src)

Definition at line 64 of file wx_python_helpers.cpp.

References str, and wxPythonEncoding.

Referenced by Py2wxString().

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 }
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
const string & str
Definition: json11.cpp:596
wxString Py2wxString ( PyObject *  src)

Definition at line 179 of file wx_python_helpers.cpp.

References newWxStringFromPy().

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 }
wxString * newWxStringFromPy(PyObject *src)
PyObject* wx2PyString ( const wxString &  src)

Definition at line 199 of file wx_python_helpers.cpp.

References str.

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 }
const string & str
Definition: json11.cpp:596
PyObject* wxArrayString2PyList ( const wxArrayString &  lst)

Definition at line 41 of file wx_python_helpers.cpp.

References i.

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 }
size_t i
Definition: json11.cpp:597
const char* wxGetDefaultPyEncoding ( )

Definition at line 219 of file wx_python_helpers.cpp.

References wxPythonEncoding.

220 {
221  return wxPythonEncoding;
222 }
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
void wxSetDefaultPyEncoding ( const char *  encoding)

Definition at line 212 of file wx_python_helpers.cpp.

References WX_DEFAULTENCODING_SIZE, and wxPythonEncoding.

213 {
214  strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
216 }
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
#define WX_DEFAULTENCODING_SIZE

Variable Documentation

char wxPythonEncoding[WX_DEFAULTENCODING_SIZE] = "ascii"
static