From 01611702ec04fa70445fd2c7d37b9b312d3f7561 Mon Sep 17 00:00:00 2001
From: William S Fulton
+A Python 3 string is a Unicode string so by default a Python 3 string that contains Unicode +characters passed to C/C++ will be accepted and converted to a C/C++ string +(char * or std::string types). +A Python 2 string is not a unicode string by default and should a Unicode string be +passed to C/C++ it will fail to convert to a C/C++ string +(char * or std::string types). +The Python 2 behavior can be made more like Python 3 by defining +SWIG_PYTHON_2_UNICODE when compiling the generated C/C++ code. +By default when the following is wrapped: +
+ ++%module unicode_strings +char *charstring(char *s) { + return s; +} +
+An error will occur when using Unicode strings in Python 2: +
+ ++>>> from unicode_strings import * +>>> charstring("hi") +'hi' +>>> charstring(u"hi") +Traceback (most recent call last): + File "<stdin>", line 1, in ? +TypeError: in method 'charstring', argument 1 of type 'char *' +
+When the SWIG_PYTHON_2_UNICODE macro is added to the generated code: +
+ ++%module unicode_strings +%begin %{ +#define SWIG_PYTHON_2_UNICODE +%} + +char *charstring(char *s) { + return s; +} +
+Unicode strings will be successfully accepted and converted from UTF-8, +but note that they are returned as a normal Python 2 string: +
+ ++>>> from unicode_strings import * +>>> charstring("hi") +'hi' +>>> charstring(u"hi") +'hi' +>>> +