File: values.cc

package info (click to toggle)
wreport 3.36-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,572 kB
  • sloc: cpp: 18,927; python: 583; sh: 78; makefile: 13
file content (125 lines) | stat: -rw-r--r-- 2,865 bytes parent folder | download | duplicates (3)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "values.h"
#include "core.h"
#include <string>

namespace wreport {
namespace python {

PyObject* cstring_to_python(const char* str)
{
    return throw_ifnull(PyUnicode_FromString(str));
}

PyObject* string_to_python(const std::string& str)
{
    return throw_ifnull(PyUnicode_FromStringAndSize(str.data(), str.size()));
}

std::string string_from_python(PyObject* o)
{
    if (!PyUnicode_Check(o))
    {
        PyErr_SetString(PyExc_TypeError, "value must be an instance of str");
        throw PythonException();
    }
    ssize_t size;
    const char* res = throw_ifnull(PyUnicode_AsUTF8AndSize(o, &size));
    return std::string(res, size);
}

const char* cstring_from_python(PyObject* o)
{
    if (PyUnicode_Check(o))
        return throw_ifnull(PyUnicode_AsUTF8(o));
    PyErr_SetString(PyExc_TypeError, "value must be an instance of str");
    throw PythonException();
}

PyObject* bytes_to_python(const std::vector<uint8_t>& buffer)
{
    return throw_ifnull(PyBytes_FromStringAndSize((const char*)buffer.data(), buffer.size()));
}

bool bool_from_python(PyObject* o)
{
    int istrue = PyObject_IsTrue(o);
    if (istrue == -1) throw PythonException();
    return istrue == 1;
}

int int_from_python(PyObject* o)
{
    int res = PyLong_AsLong(o);
    if (PyErr_Occurred())
        throw PythonException();
    return res;
}

PyObject* int_to_python(int val)
{
    return throw_ifnull(PyLong_FromLong(val));
}

PyObject* unsigned_int_to_python(unsigned int val)
{
    return throw_ifnull(PyLong_FromUnsignedLong(val));
}

PyObject* long_to_python(long int val)
{
    return throw_ifnull(PyLong_FromLong(val));
}

PyObject* unsigned_long_to_python(unsigned long val)
{
    return throw_ifnull(PyLong_FromUnsignedLong(val));
}

PyObject* long_long_to_python(long long val)
{
    return throw_ifnull(PyLong_FromLongLong(val));
}

PyObject* unsigned_long_long_to_python(unsigned long long val)
{
    return throw_ifnull(PyLong_FromUnsignedLongLong(val));
}

double double_from_python(PyObject* o)
{
    double res = PyFloat_AsDouble(o);
    if (res == -1.0 && PyErr_Occurred())
        throw PythonException();
    return res;
}

PyObject* double_to_python(double val)
{
    return throw_ifnull(PyFloat_FromDouble(val));
}

std::vector<std::string> stringlist_from_python(PyObject* o)
{
    pyo_unique_ptr iter(throw_ifnull(PyObject_GetIter(o)));

    std::vector<std::string> res;
    while (pyo_unique_ptr item = PyIter_Next(iter))
        res.emplace_back(from_python<std::string>(item));

    if (PyErr_Occurred())
        throw PythonException();

    return res;
}

PyObject* stringlist_to_python(const std::vector<std::string>& val)
{
    pyo_unique_ptr res(throw_ifnull(PyList_New(val.size())));
    Py_ssize_t idx = 0;
    for (const auto& str: val)
        PyList_SET_ITEM(res.get(), idx++, to_python(str));
    return res.release();
}

}
}