KiCad PCB EDA Suite
wx_python_helpers.cpp File Reference

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

#include <Python.h>
#include <macros.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 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

◆ WX_DEFAULTENCODING_SIZE

#define WX_DEFAULTENCODING_SIZE   64

Definition at line 37 of file wx_python_helpers.cpp.

Function Documentation

◆ Py2wxString()

wxString Py2wxString ( PyObject *  src)

Definition at line 57 of file wx_python_helpers.cpp.

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}
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]

References FROM_UTF8(), and wxPythonEncoding.

◆ wx2PyString()

PyObject * wx2PyString ( const wxString &  src)

Definition at line 107 of file wx_python_helpers.cpp.

108{
109 return PyUnicode_FromString( src.utf8_str() );
110}

◆ wxArrayString2PyList()

PyObject * wxArrayString2PyList ( const wxArrayString &  lst)

Definition at line 42 of file wx_python_helpers.cpp.

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}

◆ wxGetDefaultPyEncoding()

const char * wxGetDefaultPyEncoding ( )

Definition at line 120 of file wx_python_helpers.cpp.

121{
122 return wxPythonEncoding;
123}

References wxPythonEncoding.

◆ wxSetDefaultPyEncoding()

void wxSetDefaultPyEncoding ( const char *  encoding)

Definition at line 113 of file wx_python_helpers.cpp.

114{
115 strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
117}
#define WX_DEFAULTENCODING_SIZE

References WX_DEFAULTENCODING_SIZE, and wxPythonEncoding.

Variable Documentation

◆ wxPythonEncoding

char wxPythonEncoding[WX_DEFAULTENCODING_SIZE] = "ascii"
static