forked from google/clif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.cc
412 lines (374 loc) · 10.3 KB
/
types.cc
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "clif/python/types.h"
#include <climits>
#include "absl/numeric/int128.h"
namespace clif {
//// To Python conversions.
// bytes
PyObject* Clif_PyObjFrom(std::string_view c, const py::PostConv& pc) {
return pc.Apply(PyBytes_FromStringAndSize(c.data(), c.size()));
}
PyObject* UnicodeFromBytes(PyObject* b) {
if (!b || PyUnicode_Check(b)) return b;
if (!PyBytes_Check(b)) {
PyErr_Format(PyExc_TypeError, "expecting bytes, got %s %s",
ClassName(b), ClassType(b));
Py_DECREF(b);
return nullptr;
}
PyObject* u = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(b),
PyBytes_GET_SIZE(b));
Py_DECREF(b);
return u;
}
PyObject* UnicodeFromBytesIfPossible(PyObject* b) {
if (b == nullptr || !PyBytes_Check(b)) {
return b;
}
PyObject* u = PyUnicode_FromStringAndSize(PyBytes_AS_STRING(b),
PyBytes_GET_SIZE(b));
Py_DECREF(b);
return u;
}
//// From Python conversions.
// int (long)
namespace {
void ChangeOverflowErrorToTypeError() {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
Py_DECREF(type);
type = PyExc_TypeError;
Py_INCREF(type);
PyErr_Restore(type, value, traceback);
}
}
} // namespace
bool Clif_PyObjAs(PyObject* py, int* c) {
CHECK(c != nullptr);
long i; //NOLINT: runtime/int
if (PyLong_Check(py)) {
i = PyLong_AsLong(py);
if (PyErr_Occurred()) {
ChangeOverflowErrorToTypeError();
return false;
}
#if SIZEOF_INT < SIZEOF_LONG
if (i > INT_MAX || i < INT_MIN) {
PyErr_SetString(PyExc_TypeError, "value too large for int");
return false;
}
#endif
} else {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
*c = i;
return true;
}
bool Clif_PyObjAs(PyObject* py, short* c) { //NOLINT: runtime/int
CHECK(c != nullptr);
long i; // NOLINT: runtime/int
if (!Clif_PyObjAs(py, &i)) {
return false;
}
if (i > SHRT_MAX || i < SHRT_MIN) {
PyErr_SetString(PyExc_TypeError, "value too large for short int");
return false;
}
*c = i;
return true;
}
bool Clif_PyObjAs(PyObject* py, signed char* c) {
CHECK(c != nullptr);
long i; // NOLINT: runtime/int
if (!Clif_PyObjAs(py, &i)) {
return false;
}
if (SCHAR_MIN > i || i > SCHAR_MAX) {
PyErr_Format(PyExc_TypeError, "value %ld is out of range for signed char",
i);
return false;
}
*c = i;
return true;
}
// uint8
bool Clif_PyObjAs(PyObject* py, unsigned char* c) {
CHECK(c != nullptr);
unsigned long i; // NOLINT: runtime/int
if (!Clif_PyObjAs(py, &i)) {
return false;
}
if (i > UCHAR_MAX) {
PyErr_Format(PyExc_TypeError, "value %ld is too large for unsigned char",
i);
return false;
}
*c = i;
return true;
}
bool Clif_PyObjAs(PyObject* py, unsigned short* c) { //NOLINT: runtime/int
CHECK(c != nullptr);
unsigned long i; //NOLINT: runtime/int
if (PyLong_Check(py)) {
i = PyLong_AsUnsignedLong(py);
} else {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
if (PyErr_Occurred()) {
ChangeOverflowErrorToTypeError();
return false;
}
if (i > USHRT_MAX) {
PyErr_SetString(PyExc_TypeError, "value too large for unsigned short");
return false;
}
*c = i;
return true;
}
bool Clif_PyObjAs(PyObject* py, unsigned int* c) {
CHECK(c != nullptr);
unsigned long i; //NOLINT: runtime/int
if (PyLong_Check(py)) {
i = PyLong_AsUnsignedLong(py);
} else {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
if (PyErr_Occurred()) {
ChangeOverflowErrorToTypeError();
return false;
}
if (i > UINT_MAX) {
PyErr_SetString(PyExc_TypeError, "value too large for unsigned int");
return false;
}
*c = i;
return true;
}
bool Clif_PyObjAs(PyObject* py, unsigned long* c) { //NOLINT: runtime/int
CHECK(c != nullptr);
if (PyLong_Check(py)) {
*c = PyLong_AsUnsignedLong(py);
} else {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
if (PyErr_Occurred()) {
ChangeOverflowErrorToTypeError();
return false;
}
return true;
}
bool Clif_PyObjAs(PyObject* py, long* c) { //NOLINT: runtime/int
CHECK(c != nullptr);
if (PyLong_Check(py)) {
*c = PyLong_AsSsize_t(py);
} else {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
if (PyErr_Occurred()) {
ChangeOverflowErrorToTypeError();
return false;
}
return true;
}
// int64
bool Clif_PyObjAs(PyObject* py, long long* c) { //NOLINT: runtime/int
CHECK(c != nullptr);
if (!PyLong_Check(py)) {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
*c = PyLong_AsLongLong(py);
if (PyErr_Occurred()) {
ChangeOverflowErrorToTypeError();
return false;
}
return true;
}
// uint64
bool Clif_PyObjAs(PyObject* py, unsigned long long* c) { //NOLINT: runtime/int
CHECK(c != nullptr);
if (PyLong_Check(py)) {
*c = PyLong_AsUnsignedLongLong(py);
} else {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
if (PyErr_Occurred()) {
ChangeOverflowErrorToTypeError();
return false;
}
return true;
}
// int128
bool Clif_PyObjAs(PyObject* py, absl::int128* c) { // NOLINT: runtime/int
CHECK(c != nullptr);
if (PyLong_Check(py)) {
auto lo = PyLong_AsUnsignedLongLong(
PyNumber_And(py, PyLong_FromUnsignedLongLong(0xFFFFFFFFFFFFFFFF)));
auto hi = PyLong_AsLongLong(PyNumber_Rshift(py, PyLong_FromLong(64)));
*c = absl::MakeInt128(hi, lo);
} else {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
return !PyErr_Occurred();
}
// uint128
bool Clif_PyObjAs(PyObject* py, absl::uint128* c) { // NOLINT: runtime/int
CHECK(c != nullptr);
if (PyLong_Check(py)) {
auto lo = PyLong_AsUnsignedLongLong(
PyNumber_And(py, PyLong_FromUnsignedLongLong(0xFFFFFFFFFFFFFFFF)));
auto hi =
PyLong_AsUnsignedLongLong(PyNumber_Rshift(py, PyLong_FromLong(64)));
*c = absl::MakeUint128(hi, lo);
} else {
PyErr_SetString(PyExc_TypeError, "expecting int");
return false;
}
return !PyErr_Occurred();
}
#ifdef ABSL_HAVE_INTRINSIC_INT128
bool Clif_PyObjAs(PyObject* py, __int128* c) {
CHECK(c != nullptr);
absl::int128 tmp;
if (!Clif_PyObjAs(py, &tmp)) return false;
*c = __int128{tmp};
return true;
}
bool Clif_PyObjAs(PyObject* py, unsigned __int128* c) {
CHECK(c != nullptr);
absl::uint128 tmp;
if (!Clif_PyObjAs(py, &tmp)) return false;
*c = (unsigned __int128){tmp};
return true;
}
#endif // ABSL_HAVE_INTRINSIC_INT128
// float (double)
bool Clif_PyObjAs(PyObject* py, double* c) {
CHECK(c != nullptr);
double f = PyFloat_AsDouble(py);
if (f == -1.0 && PyErr_Occurred()) return false;
*c = f;
return true;
}
bool Clif_PyObjAs(PyObject* py, float* c) {
CHECK(c != nullptr);
double f = PyFloat_AsDouble(py);
if (f == -1.0 && PyErr_Occurred()) return false;
*c = static_cast<float>(f);
return true;
}
// complex
bool Clif_PyObjAs(PyObject* py, std::complex<double>* c) {
CHECK(c != nullptr);
double real = PyComplex_RealAsDouble(py);
if (real == -1.0 && PyErr_Occurred()) return false;
double imag = PyComplex_ImagAsDouble(py);
if (imag == -1.0 && PyErr_Occurred()) return false;
*c = std::complex<double>(real, imag);
return true;
}
bool Clif_PyObjAs(PyObject* py, std::complex<float>* c) {
CHECK(c != nullptr);
double real = PyComplex_RealAsDouble(py);
if (real == -1.0 && PyErr_Occurred()) return false;
double imag = PyComplex_ImagAsDouble(py);
if (imag == -1.0 && PyErr_Occurred()) return false;
*c = std::complex<float>(static_cast<float>(real), static_cast<float>(imag));
return true;
}
// bool
bool Clif_PyObjAs(PyObject* py, bool* c) {
CHECK(c != nullptr);
if (!PyBool_Check(py)) {
PyErr_SetString(PyExc_TypeError, "expecting bool");
return false;
}
*c = (py == Py_True);
return true;
}
namespace py {
// bytes/unicode
template<typename C>
bool ObjToStr(PyObject* py, C copy) {
#if PY_VERSION_HEX >= 0x03030000
const char* data;
Py_ssize_t length;
if (PyUnicode_Check(py)) {
data = PyUnicode_AsUTF8AndSize(py, &length);
if (!data) return false;
} else if (PyBytes_Check(py)) {
data = PyBytes_AS_STRING(py);
length = PyBytes_GET_SIZE(py);
} else {
PyErr_SetString(PyExc_TypeError, "expecting str");
return false;
}
copy(data, length);
return true;
#else
bool decref = false;
if (PyUnicode_Check(py)) {
py = PyUnicode_AsUTF8String(py);
if (!py) return false;
decref = true;
} else if (!PyBytes_Check(py)) {
PyErr_SetString(PyExc_TypeError, "expecting str");
return false;
}
copy(PyBytes_AS_STRING(py), PyBytes_GET_SIZE(py));
if (decref) Py_DECREF(py);
return true;
#endif
}
} // namespace py
bool Clif_PyObjAs(PyObject* p, std::string* c) {
CHECK(c != nullptr);
return py::ObjToStr(p,
[c](const char* data, size_t length) { c->assign(data, length); });
}
bool Clif_PyObjAs(PyObject* p, std::shared_ptr<std::string>* c) {
CHECK(c != nullptr);
std::shared_ptr<std::string> pt(new std::string);
if (!Clif_PyObjAs(p, pt.get())) return false;
*c = std::move(pt);
return true;
}
bool Clif_PyObjAs(PyObject* p, std::string_view* c) {
CHECK(c != nullptr);
if (PyUnicode_Check(p)) {
Py_ssize_t length;
const char* data = PyUnicode_AsUTF8AndSize(p, &length);
if (data == nullptr) {
return false;
}
*c = std::string_view(data, length);
return true;
}
if (PyBytes_Check(p)) {
*c = std::string_view(PyBytes_AS_STRING(p), PyBytes_GET_SIZE(p));
return true;
}
PyErr_SetString(PyExc_TypeError, "expecting str or bytes");
return false;
}
} // namespace clif