Python 2 Unicode strings can be used as inputs to char * or std::string types

Requires SWIG_PYTHON_2_UNICODE to be defined when compiling generated code.
This commit is contained in:
William S Fulton 2015-12-19 03:52:33 +00:00
commit 01611702ec
5 changed files with 88 additions and 0 deletions

View file

@ -12,3 +12,12 @@ if sys.version_info[0:2] >= (3, 1):
raise ValueError('Test comparison mismatch')
if unicode_strings.non_utf8_std_string() != test_string:
raise ValueError('Test comparison mismatch')
# Testing SWIG_PYTHON_2_UNICODE flag which allows unicode strings to be passed to C
if sys.version_info[0:2] < (3, 0):
assert unicode_strings.charstring("hello1") == "hello1"
assert unicode_strings.charstring(str(u"hello2")) == "hello2"
assert unicode_strings.charstring(u"hello3") == "hello3"
assert unicode_strings.charstring(unicode("hello4")) == "hello4"
unicode_strings.charstring(u"hell\xb05")
unicode_strings.charstring(u"hell\u00f66")

View file

@ -2,6 +2,10 @@
%include <std_string.i>
%begin %{
#define SWIG_PYTHON_2_UNICODE
%}
%inline %{
const char* non_utf8_c_str(void) {
@ -12,4 +16,8 @@ std::string non_utf8_std_string(void) {
return std::string("h\xe9llo w\xc3\xb6rld");
}
char *charstring(char *s) {
return s;
}
%}