File: python_qt_wrappers.cpp

package info (click to toggle)
kopeninghours 25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 920 kB
  • sloc: cpp: 3,918; yacc: 795; lex: 335; python: 18; sh: 17; makefile: 16
file content (57 lines) | stat: -rw-r--r-- 1,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
    SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <boost/python/converter/registry.hpp>
#include <boost/python/to_python_converter.hpp>
#include <boost/python/object.hpp>
#include <QByteArray>

using namespace boost::python;

struct QByteArray_to_python_str
{
  static PyObject* convert(QByteArray const& s)
  {
      return incref(object(s.constData()).ptr());
  }
};

struct QByteArray_from_python_str
{
    QByteArray_from_python_str()
    {
        converter::registry::push_back(&convertible, &construct, type_id<QByteArray>());
    }

    // Determine if objPtr can be converted in a QByteArray
    static void* convertible(PyObject* obj_ptr)
    {
        if (!PyUnicode_Check(obj_ptr)) return nullptr;
        return obj_ptr;
    }

    // Convert objPtr into a QByteArray
    static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data)
    {
        // Extract the character data from the python string
        handle<> rawBytesHandle{PyUnicode_AsUTF8String(obj_ptr)};
        Q_ASSERT(rawBytesHandle);

        // Grab a pointer to memory into which to construct the new QByteArray
        void* storage = ((converter::rvalue_from_python_storage<QByteArray>*) data)->storage.bytes;

        // in-place construct the new QByteArray using the character data extracted from the python object
        new (storage) QByteArray{PyBytes_AsString(rawBytesHandle.get()), int(PyBytes_Size(rawBytesHandle.get()))};
        data->convertible = storage;
    }
};

void register_qt_wrappers()
{
    to_python_converter<
            QByteArray,
            QByteArray_to_python_str>();
    QByteArray_from_python_str();
}