1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 Miguel Angel Ajo <miguelangel@nbee.es>
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file wx_python_helpers.cpp
* @brief Python wrapping helpers for wx structures/objects
*/
#include <Python.h>
#include <wx/intl.h>
#include <wx/string.h>
#include <wx/arrstr.h>
#define WX_DEFAULTENCODING_SIZE 64
static char wxPythonEncoding[WX_DEFAULTENCODING_SIZE] = "ascii";
PyObject* wxArrayString2PyList( const wxArrayString& lst )
{
PyObject* list = PyList_New( 0 );
for( size_t i = 0; i < lst.GetCount(); i++ )
{
#if wxUSE_UNICODE
PyObject* pyStr = PyUnicode_FromWideChar( lst[i].c_str(),
lst[i].Len()
);
#else
PyObject* pyStr = PyString_FromStringAndSize( lst[i].c_str(),
lst[i].Len()
);
#endif
PyList_Append( list, pyStr );
Py_DECREF( pyStr );
}
return list;
}
wxString* newWxStringFromPy( PyObject* src )
{
bool must_unref_str = false;
wxString* result = NULL;
PyObject* obj = src;
#if wxUSE_UNICODE
bool must_unref_obj = false;
// Unicode string to python unicode string
PyObject* uni_str = src;
// if not an str or unicode, try to str(src)
if( !PyString_Check( src ) && !PyUnicode_Check( src ) )
{
obj = PyObject_Str( src );
must_unref_obj = true;
if( PyErr_Occurred() )
return NULL;
}
if( PyString_Check( obj ) )
{
uni_str = PyUnicode_FromEncodedObject( obj, wxPythonEncoding, "strict" );
must_unref_str = true;
if( PyErr_Occurred() )
return NULL;
}
result = new wxString();
size_t len = PyUnicode_GET_SIZE( uni_str );
if( len )
{
PyUnicode_AsWideChar( (PyUnicodeObject*) uni_str,
wxStringBuffer( *result, len ), len );
}
if( must_unref_str )
{
Py_DECREF( uni_str );
}
if( must_unref_obj )
{
Py_DECREF( obj );
}
#else
// normal string (or object) to normal python string
PyObject* str = src;
if( PyUnicode_Check( src ) ) // if it's unicode convert to normal string
{
str = PyUnicode_AsEncodedString( src, wxPythonEncoding, "strict" );
if( PyErr_Occurred() )
return NULL;
}
else if( !PyString_Check( src ) ) // if it's not a string, str(obj)
{
str = PyObject_Str( src );
must_unref_str = true;
if( PyErr_Occurred() )
return NULL;
}
// get the string pointer and size
char* str_ptr;
Py_ssize_t str_size;
PyString_AsStringAndSize( str, &str_ptr, &str_size );
// build the wxString from our pointer / size
result = new wxString( str_ptr, str_size );
if( must_unref_str )
{
Py_DECREF( str );
}
#endif
return result;
}
wxString Py2wxString( PyObject* src )
{
wxString result;
wxString* resPtr = newWxStringFromPy( src );
// In case of exception clear it and return an empty string
if( resPtr==NULL )
{
PyErr_Clear();
return wxEmptyString;
}
result = *resPtr;
delete resPtr;
return result;
}
PyObject* wx2PyString( const wxString& src )
{
PyObject* str;
#if wxUSE_UNICODE
str = PyUnicode_FromWideChar( src.c_str(), src.Len() );
#else
str = PyString_FromStringAndSize( src.c_str(), src.Len() );
#endif
return str;
}
void wxSetDefaultPyEncoding( const char* encoding )
{
strncpy( wxPythonEncoding, encoding, WX_DEFAULTENCODING_SIZE );
wxPythonEncoding[ WX_DEFAULTENCODING_SIZE - 1 ] = '\0';
}
const char* wxGetDefaultPyEncoding()
{
return wxPythonEncoding;
}
|