aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/pybind/cast.cpp
blob: a22796c1c147a5584c610be03dc415b82e4e666d (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "cast.h"
#include <util/generic/yexception.h>
#include <util/generic/buffer.h>

namespace NPyBind {
    PyObject* GetTrueRef(bool incref) {
        if (incref)
            Py_RETURN_TRUE;
        return Py_True;
    }

    PyObject* GetFalseRef(bool incref) {
        if (incref)
            Py_RETURN_FALSE;
        return Py_False;
    }

    PyObject* BuildPyObject(int val) {
        return Py_BuildValue("i", val);
    }

    PyObject* BuildPyObject(unsigned int val) {
        return Py_BuildValue("I", val);
    }

    PyObject* BuildPyObject(long int val) {
        return Py_BuildValue("l", val);
    }

    PyObject* BuildPyObject(unsigned long int val) {
        return Py_BuildValue("k", val);
    }

#ifdef PY_LONG_LONG
    PyObject* BuildPyObject(PY_LONG_LONG val) {
        return Py_BuildValue("L", val);
    }

    PyObject* BuildPyObject(unsigned PY_LONG_LONG val) {
        return Py_BuildValue("K", val);
    }
#endif

    PyObject* BuildPyObject(float val) {
        return Py_BuildValue("f", val);
    }

    PyObject* BuildPyObject(double val) {
        return Py_BuildValue("d", val);
    }

    PyObject* BuildPyObject(const TStringBuf& val) {
        if (!val.IsInited())
            Py_RETURN_NONE;

        PyObject* stringValue = Py_BuildValue("s#", val.data(), static_cast<int>(val.length()));
        if (stringValue != nullptr) {
            return stringValue;
        }
        if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
            PyErr_Clear();
        } else {
            return nullptr;
        }
        return Py_BuildValue("y#", val.data(), static_cast<int>(val.length()));
    }

    PyObject* BuildPyObject(const char* val) {
        if (val == nullptr)
            Py_RETURN_NONE;
        PyObject* stringValue = Py_BuildValue("s#", val, static_cast<int>(strlen(val)));
        if (stringValue != nullptr) {
            return stringValue;
        }
        if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
            PyErr_Clear();
        } else {
            return nullptr;
        }
        return Py_BuildValue("y#", val, static_cast<int>(strlen(val)));
    }

    PyObject* BuildPyObject(const TWtringBuf& val) {
        if (!val.IsInited())
            Py_RETURN_NONE;
        TPyObjectPtr result(PyUnicode_FromUnicode(nullptr, val.size()), true);
        Py_UNICODE* buf = PyUnicode_AS_UNICODE(result.Get());
        if (buf == nullptr)
            Py_RETURN_NONE;
        for (size_t i = 0; i < val.size(); ++i) {
            buf[i] = static_cast<Py_UNICODE>(val[i]);
        }
        return result.RefGet();
    }

    PyObject* BuildPyObject(const TBuffer& val) {
        TPyObjectPtr res(PyList_New(val.size()), true);
        for (size_t i = 0, size = val.Size(); i < size; ++i)
            PyList_SetItem(res.Get(), i, BuildPyObject(val.Data()[i]));
        return res.RefGet();
    }

    PyObject* BuildPyObject(bool val) {
        if (val)
            Py_RETURN_TRUE;
        else
            Py_RETURN_FALSE;
    }

    PyObject* BuildPyObject(PyObject* val) {
        Py_XINCREF(val);
        return val;
    }

    PyObject* BuildPyObject(TPyObjectPtr ptr) {
        return ptr.RefGet();
    }

    /* python represents (http://docs.python.org/c-api/arg.html#Py_BuildValue)
     * char, uchar, short, ushort, int, long as PyInt
     * uint, ulong as PyInt or PyLong (if exceeds sys.maxint)
     * longlong, ulonglong as PyLong
     */

    template <>
    bool FromPyObject(PyObject* obj, long& res) {
        if (PyLong_Check(obj)) {
            res = PyLong_AsLong(obj);
            return true;
        }
        if (PyFloat_Check(obj)) {
            res = static_cast<long>(PyFloat_AsDouble(obj));
            return true;
        }
#if PY_MAJOR_VERSION < 3
        res = PyInt_AsLong(obj);
#endif
        return -1 != res || !PyErr_Occurred();
    }

    template <>
    bool FromPyObject(PyObject* obj, unsigned long& res) {
        long lres;
        if (!FromPyObject(obj, lres))
            return false;
        if (lres < 0)
            return false;
        res = static_cast<unsigned long long>(lres);
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, int& res) {
        long lres;
        if (!FromPyObject(obj, lres))
            return false;
        res = static_cast<int>(lres);
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, unsigned char& res) {
        long lres;
        if (!FromPyObject(obj, lres))
            return false;
        res = static_cast<unsigned char>(lres);
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, char& res) {
        long lres;
        if (!FromPyObject(obj, lres))
            return false;
        res = static_cast<char>(lres);
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, unsigned int& res) {
        unsigned long lres;
        if (!FromPyObject(obj, lres))
            return false;
        res = static_cast<unsigned int>(lres);
        return true;
    }

#ifdef HAVE_LONG_LONG
    template <>
    bool FromPyObject(PyObject* obj, long long& res) {
        if (PyLong_Check(obj)) {
            res = PyLong_AsLongLong(obj);
            return -1 != res || !PyErr_Occurred();
        }
        long lres;
        if (!FromPyObject(obj, lres))
            return false;
        res = static_cast<long long>(lres);
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, unsigned long long& res) {
        if (PyLong_Check(obj)) {
            res = PyLong_AsUnsignedLongLong(obj);
            return static_cast<unsigned long long>(-1) != res || !PyErr_Occurred();
        }
        long lres;
        if (!FromPyObject(obj, lres))
            return false;
        res = static_cast<unsigned long long>(lres);
        return true;
    }
#endif

    template <>
    bool FromPyObject(PyObject* obj, double& res) {
        if (PyFloat_Check(obj)) {
            res = PyFloat_AsDouble(obj);
            return true;
        }
        long long lres;
        if (!FromPyObject(obj, lres))
            return false;
        res = static_cast<double>(lres);
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, float& res) {
        double dres;
        if (!FromPyObject(obj, dres))
            return false;
        res = static_cast<float>(dres);
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, bool& res) {
        if (!PyBool_Check(obj))
            return false;
        if (obj == Py_True)
            res = true;
        else
            res = false;
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, PyObject*& res) {
        Py_XINCREF(obj);
        res = obj;
        return true;
    }

    template <>
    bool FromPyObject(PyObject* obj, TPyObjectPtr& res) {
        res = TPyObjectPtr(obj);
        return true;
    }

    static inline bool _FromPyObject(PyObject* obj, TStringBuf& res) {
        char* str;
        Py_ssize_t len;
#if PY_MAJOR_VERSION >= 3
        if (PyUnicode_Check(obj)) {
            auto buf = PyUnicode_AsUTF8AndSize(obj, &len);
            res = TStringBuf(buf, len);
            return true;
        }
#endif
        if (-1 == PyBytes_AsStringAndSize(obj, &str, &len) || 0 > len)
            return false;
        res = TStringBuf(str, len);
        return true;
    }

    bool FromPyObject(PyObject* obj, TStringBuf& res) {
        return _FromPyObject(obj, res);
    }

    bool FromPyObject(PyObject* obj, TString& res) {
        TStringBuf str;
        if (!_FromPyObject(obj, str))
            return false;
        res = str;
        return true;
    }

    bool FromPyObject(PyObject* obj, TUtf16String& res) {
        if (!PyUnicode_Check(obj))
            return false;
        auto str = TPyObjectPtr(PyUnicode_AsUTF16String(obj), true);
        if (!str)
            return false;
        constexpr auto BOM_SIZE = 2;
        size_t len = (static_cast<size_t>(PyBytes_GET_SIZE(str.Get())) - BOM_SIZE) / 2;
        res.resize(len);
        memcpy(res.begin(), PyBytes_AS_STRING(str.Get()) + BOM_SIZE, len * 2);
        return (nullptr == PyErr_Occurred());
    }

    bool FromPyObject(PyObject* obj, TBuffer& res) {
        if (!PyList_Check(obj))
            return false;
        size_t cnt = PyList_Size(obj);
        res.Reserve(cnt);
        for (size_t i = 0; i < cnt; ++i) {
            PyObject* item = PyList_GET_ITEM(obj, i);
            char ch = 0;
            if (!FromPyObject(item, ch))
                return false;
            res.Append(ch);
        }
        return true;
    }
}