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 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( !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 }
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
wxString Py2wxString ( PyObject *  src)

Definition at line 153 of file wx_python_helpers.cpp.

References newWxStringFromPy().

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

Definition at line 173 of file wx_python_helpers.cpp.

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 }
PyObject* wxArrayString2PyList ( const wxArrayString &  lst)

Definition at line 41 of file wx_python_helpers.cpp.

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 }
const char* wxGetDefaultPyEncoding ( )

Definition at line 193 of file wx_python_helpers.cpp.

References wxPythonEncoding.

194 {
195  return wxPythonEncoding;
196 }
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
void wxSetDefaultPyEncoding ( const char *  encoding)

Definition at line 186 of file wx_python_helpers.cpp.

References WX_DEFAULTENCODING_SIZE, and wxPythonEncoding.

187 {
188  strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
190 }
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE]
#define WX_DEFAULTENCODING_SIZE

Variable Documentation

char wxPythonEncoding[WX_DEFAULTENCODING_SIZE] = "ascii"
static