[PyQt] wchar_t type mapping

Diez B. Roggisch deets at web.de
Sat Jun 20 18:52:02 BST 2009


I want to wrap a method with the following signature:

virtual irr::gui::IGUIStaticText* addStaticText(const wchar_t* text, 
const irr::core::rect<irr::s32>& rectangle,
						      bool border=false, bool wordWrap=true, 
irr::gui::IGUIElement* parent=0, irr::s32 id=-1,
						      bool fillBackground = false) = 0;

All types are mapped or declared, and compilation succeeds.

However, on calling the function, Python barks with

Traceback (most recent call last):
   File "hello_world.py", line 64, in <module>
   File "hello_world.py", line 45, in main
TypeError: argument 1 of IGUIEnvironment.addStaticText() has an invalid type

So I tried to map the wchar_t from and to PyUnicode-objects. But this 
gives me an error that says

mac-dir:IrrSinn-1.5 deets$ python2.6 setup.py install
running install
running build
running build_ext
building 'irrlicht' extension
/Library/Frameworks/Python.framework/Versions/2.6/bin/sip -c 
build/temp.macosx-10.3-i386-2.6 -b 
build/temp.macosx-10.3-i386-2.6/irr.sbf irr.sip
sip: types.sip:613: Invalid type for %MappedType
error: command 
'/Library/Frameworks/Python.framework/Versions/2.6/bin/sip' failed with 
exit status 1

The mapping looks like this:

%MappedType wchar_t*
#include <wchar.h>

   if (!sipCpp)
     return Py_None;
   wchar_t *s = (wchar_t*)sipCpp;
   return PyUnicode_FromWideChar(s, wcslen(s));

    if (sipIsErr == NULL) {
      return PyUnicode_Check(sipPy);
   if (sipPy == Py_None) {
     *sipCppPtr = NULL;
     return 0;
   Py_ssize_t len = PyUnicode_GET_SIZE(sipPy);
   wchar_t *w = malloc(sizeof(wchar_t) * len + 1);
   w[len] = 0;
   PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t 
   PyUnicode_AsWideChar(sipPy, w, len);
   *sipCppPtr = w;
   return 1;

Any suggestions? Of course if anything else fails, I could manually wrap 
the methods in question - but I'd rather spare myself that effort.


More information about the PyQt mailing list