Python Swig binding using UCS-4

Does anyone know if there is a way to do SWIG string encoding like UCS-4 for Python? The SWIG documentation states that this may be possible with typemaps, but does not contain any other details or examples.

In context, I am working on extending Blender 3D software using a set of Python scripts. We need to associate these scripts with various software tools for robotics, we do this with SWIG to compile Python libraries. Blender uses its own Python 3.2, precompiled with the --with-wide-unicode option, so it uses UCS-4 Unicode strings. However, in defatult, SWIG encodes the strings as UCS-2, so when interacting with Blender I always get the error message: "undefined: PyUnicodeUCS2 _ *".

+4
source share
1 answer

This is from the SWIG docs, you may have noticed this:

At this time, SWIG provides limited support for Unicode and wide format strings (type C wchar_t). Some languages ​​provide typemaps for wchar_t, but keep in mind that they may not be portable on different operating systems. This is a delicate topic that is poorly understood by many programmers and is not executed sequentially in different languages. For scripting languages ​​that support Unicode, Unicode strings are often available in an 8-bit representation, such as UTF-8, which can be mapped to a char * type (in this case, the SWIG interface will probably work). If the program you are wrapping uses Unicode, there is no guarantee that the Unicode characters in the target language will use the same internal representation (for example, UCS-2 and UCS-4). You may need to write some special conversion functions.

So it looks like you should map it to char *, and then figure out how to manually convert it if necessary. It seems like it's random to start with.

+1
source

All Articles