Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Public Member Functions | Static Public Member Functions
QString_from_python_str_or_unicode Struct Reference

Boost::Python type converter for Python `unicode' or `str' to QString. More...

List of all members.

Public Member Functions

 QString_from_python_str_or_unicode ()

Static Public Member Functions

static void * convertible (PyObject *obj_ptr)
static void construct (PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)

Detailed Description

Boost::Python type converter for Python `unicode' or `str' to QString.

Definition at line 49 of file scripter2_qttype_qstring.cpp.

Constructor & Destructor Documentation

Definition at line 51 of file scripter2_qttype_qstring.cpp.

        boost::python::converter::registry::push_back(  &convertible,
                                                        boost::python::type_id<QString>() );

Member Function Documentation

static void QString_from_python_str_or_unicode::construct ( PyObject *  obj_ptr,
boost::python::converter::rvalue_from_python_stage1_data *  data 
) [inline, static]

Definition at line 67 of file scripter2_qttype_qstring.cpp.

        // First, convert the input to Python `unicode'.
        PyObject* temp_obj_ptr;
        if (PyString_Check(obj_ptr))
            // Coerce the `str' to `unicode' using sysdefaultencoding. UnicodeDecodeError
            // is thrown if the string doesn't make sense in that encoding.
            temp_obj_ptr = PyUnicode_FromObject(obj_ptr); // new reference
            if (temp_obj_ptr == 0)
            temp_obj_ptr = obj_ptr;
            Py_INCREF(temp_obj_ptr); // to balance DECREF at end

        // FIXME: This implementation is probably rather inefficient
        Py_UNICODE* value = PyUnicode_AsUnicode(temp_obj_ptr);
        if (value == 0)
        int unichars = PyUnicode_GET_SIZE(temp_obj_ptr);
#if defined(Py_UNICODE_WIDE)

        // Python is using a 4-byte unsigned buffer of UCS-4
        // FIXME: Qt doesn't give us any direct way to load UCS-4, so we're doing
        //        it a rather clunky way that can probably be improved.
        // FIXME: Qt can't represent UCS-4 characters; we need to check for this
        //        and throw an exception.
        QString tempString("");
        int i;
        for (i = 0; i < unichars; i++)
        // Python is using a 2-byte unsigned buffer of UCS-2 with
        // limited support for UTF-16
        QString tempString(QString::fromUtf16(value, unichars));
        void* storage = ((boost::python::converter::rvalue_from_python_storage<QString>*) data)->storage.bytes;
        new (storage) QString(tempString);
        data->convertible = storage;
static void* QString_from_python_str_or_unicode::convertible ( PyObject *  obj_ptr) [inline, static]

Definition at line 59 of file scripter2_qttype_qstring.cpp.

        if (! (PyUnicode_Check(obj_ptr) || PyString_Check(obj_ptr)) )
                return 0;
        return obj_ptr;

The documentation for this struct was generated from the following file: