forked from wxWidgets/Phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.sip
74 lines (62 loc) · 2.33 KB
/
string.sip
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
//--------------------------------------------------------------------------
// Name: string.sip
// Purpose: Implements a %MappedType for wxString
//
// Author: Robin Dunn
//
// Created: 9-Nov-2010
// Copyright: (c) 2010-2018 by Total Control Software
// Licence: wxWindows license
//--------------------------------------------------------------------------
// We don't want the Python user to ever need to deal directly with wxString
// at all, so it will be mapped to and from Python Unicode objects using the
// code snippets below.
// NOTE: Currently we assume that string objects are encoded in utf-8.
%MappedType wxString
{
// TODO: It is still possible to have a wx build that uses utf-8 inside
// wxString, so we should probably be checking wxUSE_UNICODE_WCHAR or
// wxUSE_UNICODE_UTF8 for this conversion.
%ConvertToTypeCode
#if wxUSE_UNICODE_WCHAR == 0
#error wxString converison can only handle WCHAR wxStrings currently
#endif
// Code to test a PyObject for compatibility with wxString
if (!sipIsErr) {
if (PyBytes_Check(sipPy) || PyUnicode_Check(sipPy))
return TRUE;
return FALSE;
}
// Code to convert a compatible PyObject to a wxString
PyObject* uni = sipPy;
if (PyBytes_Check(sipPy)) {
// if it's a string object convert it to unicode first, assuming utf-8
uni = PyUnicode_FromEncodedObject(sipPy, "utf-8", "strict");
if (PyErr_Occurred()) {
*sipIsErr = 1;
return 0;
}
}
*sipCppPtr = new wxString();
size_t len = PyUnicode_GET_SIZE(uni);
if (len) {
wxPyUnicode_AsWideChar(uni, wxStringBuffer(**sipCppPtr, len), len);
}
if (PyBytes_Check(sipPy))
Py_DECREF(uni); // release the temporary Unicode object we created
return sipGetState(sipTransferObj);
%End
%ConvertFromTypeCode
// Convert a wxString to a Python Unicode object. See wxpy_api.sip
return wx2PyString(*sipCpp);
%End
};
// Used just for testing the MappedType code, it can be removed later
%ModuleCode
wxString testStringTypemap(const wxString& str)
{
wxString local = str;
return local;
}
%End
wxString testStringTypemap(const wxString& str);