1 /////////////// TypeConversions.proto ///////////////
2
3 /* Type Conversion Predeclarations */
4
5 #define __Pyx_uchar_cast(c) ((unsigned char)c)
6 #define __Pyx_long_cast(x) ((long)x)
7
8 #define __Pyx_fits_Py_ssize_t(v, type, is_signed) ( \
9 (sizeof(type) < sizeof(Py_ssize_t)) || \
10 (sizeof(type) > sizeof(Py_ssize_t) && \
11 likely(v < (type)PY_SSIZE_T_MAX || \
12 v == (type)PY_SSIZE_T_MAX) && \
13 (!is_signed || likely(v > (type)PY_SSIZE_T_MIN || \
14 v == (type)PY_SSIZE_T_MIN))) || \
15 (sizeof(type) == sizeof(Py_ssize_t) && \
16 (is_signed || likely(v < (type)PY_SSIZE_T_MAX || \
17 v == (type)PY_SSIZE_T_MAX))) )
18
__Pyx_is_valid_index(Py_ssize_t i,Py_ssize_t limit)19 static CYTHON_INLINE int __Pyx_is_valid_index(Py_ssize_t i, Py_ssize_t limit) {
20 // Optimisation from Section 14.2 "Bounds Checking" in
21 // https://www.agner.org/optimize/optimizing_cpp.pdf
22 // See https://bugs.python.org/issue28397
23 // The cast to unsigned effectively tests for "0 <= i < limit".
24 return (size_t) i < (size_t) limit;
25 }
26
27 // fast and unsafe abs(Py_ssize_t) that ignores the overflow for (-PY_SSIZE_T_MAX-1)
28 #if defined (__cplusplus) && __cplusplus >= 201103L
29 #include <cstdlib>
30 #define __Pyx_sst_abs(value) std::abs(value)
31 #elif SIZEOF_INT >= SIZEOF_SIZE_T
32 #define __Pyx_sst_abs(value) abs(value)
33 #elif SIZEOF_LONG >= SIZEOF_SIZE_T
34 #define __Pyx_sst_abs(value) labs(value)
35 #elif defined (_MSC_VER)
36 // abs() is defined for long, but 64-bits type on MSVC is long long.
37 // Use MS-specific _abs64 instead.
38 #define __Pyx_sst_abs(value) ((Py_ssize_t)_abs64(value))
39 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
40 #define __Pyx_sst_abs(value) llabs(value)
41 #elif defined (__GNUC__)
42 // gcc or clang on 64 bit windows.
43 #define __Pyx_sst_abs(value) __builtin_llabs(value)
44 #else
45 #define __Pyx_sst_abs(value) ((value<0) ? -value : value)
46 #endif
47
48 static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject*);
49 static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length);
50
51 #define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s))
52 #define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l)
53 #define __Pyx_PyBytes_FromString PyBytes_FromString
54 #define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
55 static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*);
56
57 #if PY_MAJOR_VERSION < 3
58 #define __Pyx_PyStr_FromString __Pyx_PyBytes_FromString
59 #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize
60 #else
61 #define __Pyx_PyStr_FromString __Pyx_PyUnicode_FromString
62 #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize
63 #endif
64
65 #define __Pyx_PyBytes_AsWritableString(s) ((char*) PyBytes_AS_STRING(s))
66 #define __Pyx_PyBytes_AsWritableSString(s) ((signed char*) PyBytes_AS_STRING(s))
67 #define __Pyx_PyBytes_AsWritableUString(s) ((unsigned char*) PyBytes_AS_STRING(s))
68 #define __Pyx_PyBytes_AsString(s) ((const char*) PyBytes_AS_STRING(s))
69 #define __Pyx_PyBytes_AsSString(s) ((const signed char*) PyBytes_AS_STRING(s))
70 #define __Pyx_PyBytes_AsUString(s) ((const unsigned char*) PyBytes_AS_STRING(s))
71 #define __Pyx_PyObject_AsWritableString(s) ((char*) __Pyx_PyObject_AsString(s))
72 #define __Pyx_PyObject_AsWritableSString(s) ((signed char*) __Pyx_PyObject_AsString(s))
73 #define __Pyx_PyObject_AsWritableUString(s) ((unsigned char*) __Pyx_PyObject_AsString(s))
74 #define __Pyx_PyObject_AsSString(s) ((const signed char*) __Pyx_PyObject_AsString(s))
75 #define __Pyx_PyObject_AsUString(s) ((const unsigned char*) __Pyx_PyObject_AsString(s))
76 #define __Pyx_PyObject_FromCString(s) __Pyx_PyObject_FromString((const char*)s)
77 #define __Pyx_PyBytes_FromCString(s) __Pyx_PyBytes_FromString((const char*)s)
78 #define __Pyx_PyByteArray_FromCString(s) __Pyx_PyByteArray_FromString((const char*)s)
79 #define __Pyx_PyStr_FromCString(s) __Pyx_PyStr_FromString((const char*)s)
80 #define __Pyx_PyUnicode_FromCString(s) __Pyx_PyUnicode_FromString((const char*)s)
81
82 // There used to be a Py_UNICODE_strlen() in CPython 3.x, but it is deprecated since Py3.3.
__Pyx_Py_UNICODE_strlen(const Py_UNICODE * u)83 static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u) {
84 const Py_UNICODE *u_end = u;
85 while (*u_end++) ;
86 return (size_t)(u_end - u - 1);
87 }
88
89 #define __Pyx_PyUnicode_FromUnicode(u) PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u))
90 #define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode
91 #define __Pyx_PyUnicode_AsUnicode PyUnicode_AsUnicode
92
93 #define __Pyx_NewRef(obj) (Py_INCREF(obj), obj)
94 #define __Pyx_Owned_Py_None(b) __Pyx_NewRef(Py_None)
95 static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b);
96 static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);
97 static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject*);
98 static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x);
99
100 #define __Pyx_PySequence_Tuple(obj) \
101 (likely(PyTuple_CheckExact(obj)) ? __Pyx_NewRef(obj) : PySequence_Tuple(obj))
102
103 static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
104 static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
105
106 #if CYTHON_ASSUME_SAFE_MACROS
107 #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
108 #else
109 #define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)
110 #endif
111 #define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x))
112
113 #if PY_MAJOR_VERSION >= 3
114 #define __Pyx_PyNumber_Int(x) (PyLong_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Long(x))
115 #else
116 #define __Pyx_PyNumber_Int(x) (PyInt_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Int(x))
117 #endif
118 #define __Pyx_PyNumber_Float(x) (PyFloat_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Float(x))
119
120 #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
121 static int __Pyx_sys_getdefaultencoding_not_ascii;
__Pyx_init_sys_getdefaultencoding_params(void)122 static int __Pyx_init_sys_getdefaultencoding_params(void) {
123 PyObject* sys;
124 PyObject* default_encoding = NULL;
125 PyObject* ascii_chars_u = NULL;
126 PyObject* ascii_chars_b = NULL;
127 const char* default_encoding_c;
128 sys = PyImport_ImportModule("sys");
129 if (!sys) goto bad;
130 default_encoding = PyObject_CallMethod(sys, (char*) "getdefaultencoding", NULL);
131 Py_DECREF(sys);
132 if (!default_encoding) goto bad;
133 default_encoding_c = PyBytes_AsString(default_encoding);
134 if (!default_encoding_c) goto bad;
135 if (strcmp(default_encoding_c, "ascii") == 0) {
136 __Pyx_sys_getdefaultencoding_not_ascii = 0;
137 } else {
138 char ascii_chars[128];
139 int c;
140 for (c = 0; c < 128; c++) {
141 ascii_chars[c] = c;
142 }
143 __Pyx_sys_getdefaultencoding_not_ascii = 1;
144 ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL);
145 if (!ascii_chars_u) goto bad;
146 ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL);
147 if (!ascii_chars_b || !PyBytes_Check(ascii_chars_b) || memcmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) {
148 PyErr_Format(
149 PyExc_ValueError,
150 "This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.",
151 default_encoding_c);
152 goto bad;
153 }
154 Py_DECREF(ascii_chars_u);
155 Py_DECREF(ascii_chars_b);
156 }
157 Py_DECREF(default_encoding);
158 return 0;
159 bad:
160 Py_XDECREF(default_encoding);
161 Py_XDECREF(ascii_chars_u);
162 Py_XDECREF(ascii_chars_b);
163 return -1;
164 }
165 #endif
166
167 #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3
168 #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL)
169 #else
170 #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL)
171
172 // __PYX_DEFAULT_STRING_ENCODING is either a user provided string constant
173 // or we need to look it up here
174 #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
175 static char* __PYX_DEFAULT_STRING_ENCODING;
176
__Pyx_init_sys_getdefaultencoding_params(void)177 static int __Pyx_init_sys_getdefaultencoding_params(void) {
178 PyObject* sys;
179 PyObject* default_encoding = NULL;
180 char* default_encoding_c;
181
182 sys = PyImport_ImportModule("sys");
183 if (!sys) goto bad;
184 default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
185 Py_DECREF(sys);
186 if (!default_encoding) goto bad;
187 default_encoding_c = PyBytes_AsString(default_encoding);
188 if (!default_encoding_c) goto bad;
189 __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c) + 1);
190 if (!__PYX_DEFAULT_STRING_ENCODING) goto bad;
191 strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);
192 Py_DECREF(default_encoding);
193 return 0;
194 bad:
195 Py_XDECREF(default_encoding);
196 return -1;
197 }
198 #endif
199 #endif
200
201 /////////////// TypeConversions ///////////////
202
203 /* Type Conversion Functions */
204
__Pyx_PyUnicode_FromString(const char * c_str)205 static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) {
206 return __Pyx_PyUnicode_FromStringAndSize(c_str, (Py_ssize_t)strlen(c_str));
207 }
208
209 // Py3.7 returns a "const char*" for unicode strings
__Pyx_PyObject_AsString(PyObject * o)210 static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject* o) {
211 Py_ssize_t ignore;
212 return __Pyx_PyObject_AsStringAndSize(o, &ignore);
213 }
214
215 #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
216 #if !CYTHON_PEP393_ENABLED
__Pyx_PyUnicode_AsStringAndSize(PyObject * o,Py_ssize_t * length)217 static const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
218 char* defenc_c;
219 // borrowed reference, cached internally in 'o' by CPython
220 PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL);
221 if (!defenc) return NULL;
222 defenc_c = PyBytes_AS_STRING(defenc);
223 #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
224 {
225 char* end = defenc_c + PyBytes_GET_SIZE(defenc);
226 char* c;
227 for (c = defenc_c; c < end; c++) {
228 if ((unsigned char) (*c) >= 128) {
229 // raise the error
230 PyUnicode_AsASCIIString(o);
231 return NULL;
232 }
233 }
234 }
235 #endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/
236 *length = PyBytes_GET_SIZE(defenc);
237 return defenc_c;
238 }
239
240 #else /* CYTHON_PEP393_ENABLED: */
241
__Pyx_PyUnicode_AsStringAndSize(PyObject * o,Py_ssize_t * length)242 static CYTHON_INLINE const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
243 if (unlikely(__Pyx_PyUnicode_READY(o) == -1)) return NULL;
244 #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
245 if (likely(PyUnicode_IS_ASCII(o))) {
246 // cached for the lifetime of the object
247 *length = PyUnicode_GET_LENGTH(o);
248 return PyUnicode_AsUTF8(o);
249 } else {
250 // raise the error
251 PyUnicode_AsASCIIString(o);
252 return NULL;
253 }
254 #else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
255 return PyUnicode_AsUTF8AndSize(o, length);
256 #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
257 }
258 #endif /* CYTHON_PEP393_ENABLED */
259 #endif
260
261 // Py3.7 returns a "const char*" for unicode strings
__Pyx_PyObject_AsStringAndSize(PyObject * o,Py_ssize_t * length)262 static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
263 #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
264 if (
265 #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
266 __Pyx_sys_getdefaultencoding_not_ascii &&
267 #endif
268 PyUnicode_Check(o)) {
269 return __Pyx_PyUnicode_AsStringAndSize(o, length);
270 } else
271 #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */
272
273 #if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE))
274 if (PyByteArray_Check(o)) {
275 *length = PyByteArray_GET_SIZE(o);
276 return PyByteArray_AS_STRING(o);
277 } else
278 #endif
279 {
280 char* result;
281 int r = PyBytes_AsStringAndSize(o, &result, length);
282 if (unlikely(r < 0)) {
283 return NULL;
284 } else {
285 return result;
286 }
287 }
288 }
289
290 /* Note: __Pyx_PyObject_IsTrue is written to minimize branching. */
__Pyx_PyObject_IsTrue(PyObject * x)291 static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
292 int is_true = x == Py_True;
293 if (is_true | (x == Py_False) | (x == Py_None)) return is_true;
294 else return PyObject_IsTrue(x);
295 }
296
__Pyx_PyObject_IsTrueAndDecref(PyObject * x)297 static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject* x) {
298 int retval;
299 if (unlikely(!x)) return -1;
300 retval = __Pyx_PyObject_IsTrue(x);
301 Py_DECREF(x);
302 return retval;
303 }
304
__Pyx_PyNumber_IntOrLongWrongResultType(PyObject * result,const char * type_name)305 static PyObject* __Pyx_PyNumber_IntOrLongWrongResultType(PyObject* result, const char* type_name) {
306 #if PY_MAJOR_VERSION >= 3
307 if (PyLong_Check(result)) {
308 // CPython issue #17576: warn if 'result' not of exact type int.
309 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
310 "__int__ returned non-int (type %.200s). "
311 "The ability to return an instance of a strict subclass of int "
312 "is deprecated, and may be removed in a future version of Python.",
313 Py_TYPE(result)->tp_name)) {
314 Py_DECREF(result);
315 return NULL;
316 }
317 return result;
318 }
319 #endif
320 PyErr_Format(PyExc_TypeError,
321 "__%.4s__ returned non-%.4s (type %.200s)",
322 type_name, type_name, Py_TYPE(result)->tp_name);
323 Py_DECREF(result);
324 return NULL;
325 }
326
__Pyx_PyNumber_IntOrLong(PyObject * x)327 static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x) {
328 #if CYTHON_USE_TYPE_SLOTS
329 PyNumberMethods *m;
330 #endif
331 const char *name = NULL;
332 PyObject *res = NULL;
333 #if PY_MAJOR_VERSION < 3
334 if (likely(PyInt_Check(x) || PyLong_Check(x)))
335 #else
336 if (likely(PyLong_Check(x)))
337 #endif
338 return __Pyx_NewRef(x);
339 #if CYTHON_USE_TYPE_SLOTS
340 m = Py_TYPE(x)->tp_as_number;
341 #if PY_MAJOR_VERSION < 3
342 if (m && m->nb_int) {
343 name = "int";
344 res = m->nb_int(x);
345 }
346 else if (m && m->nb_long) {
347 name = "long";
348 res = m->nb_long(x);
349 }
350 #else
351 if (likely(m && m->nb_int)) {
352 name = "int";
353 res = m->nb_int(x);
354 }
355 #endif
356 #else
357 if (!PyBytes_CheckExact(x) && !PyUnicode_CheckExact(x)) {
358 res = PyNumber_Int(x);
359 }
360 #endif
361 if (likely(res)) {
362 #if PY_MAJOR_VERSION < 3
363 if (unlikely(!PyInt_Check(res) && !PyLong_Check(res))) {
364 #else
365 if (unlikely(!PyLong_CheckExact(res))) {
366 #endif
367 return __Pyx_PyNumber_IntOrLongWrongResultType(res, name);
368 }
369 }
370 else if (!PyErr_Occurred()) {
371 PyErr_SetString(PyExc_TypeError,
372 "an integer is required");
373 }
374 return res;
375 }
376
377 {{py: from Cython.Utility import pylong_join }}
378
379 static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
380 Py_ssize_t ival;
381 PyObject *x;
382 #if PY_MAJOR_VERSION < 3
383 if (likely(PyInt_CheckExact(b))) {
384 if (sizeof(Py_ssize_t) >= sizeof(long))
385 return PyInt_AS_LONG(b);
386 else
387 return PyInt_AsSsize_t(b);
388 }
389 #endif
390 if (likely(PyLong_CheckExact(b))) {
391 #if CYTHON_USE_PYLONG_INTERNALS
392 const digit* digits = ((PyLongObject*)b)->ob_digit;
393 const Py_ssize_t size = Py_SIZE(b);
394 // handle most common case first to avoid indirect branch and optimise branch prediction
395 if (likely(__Pyx_sst_abs(size) <= 1)) {
396 ival = likely(size) ? digits[0] : 0;
397 if (size == -1) ival = -ival;
398 return ival;
399 } else {
400 switch (size) {
401 {{for _size in (2, 3, 4)}}
402 {{for _case in (_size, -_size)}}
403 case {{_case}}:
404 if (8 * sizeof(Py_ssize_t) > {{_size}} * PyLong_SHIFT) {
405 return {{'-' if _case < 0 else ''}}(Py_ssize_t) {{pylong_join(_size, 'digits', 'size_t')}};
406 }
407 break;
408 {{endfor}}
409 {{endfor}}
410 }
411 }
412 #endif
413 return PyLong_AsSsize_t(b);
414 }
415 x = PyNumber_Index(b);
416 if (!x) return -1;
417 ival = PyInt_AsSsize_t(x);
418 Py_DECREF(x);
419 return ival;
420 }
421
422
423 static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b) {
424 return b ? __Pyx_NewRef(Py_True) : __Pyx_NewRef(Py_False);
425 }
426
427
428 static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
429 return PyInt_FromSize_t(ival);
430 }
431
432
433 /////////////// GCCDiagnostics.proto ///////////////
434
435 // GCC diagnostic pragmas were introduced in GCC 4.6
436 // Used to silence conversion warnings that are ok but cannot be avoided.
437 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
438 #define __Pyx_HAS_GCC_DIAGNOSTIC
439 #endif
440
441
442 /////////////// ToPyCTupleUtility.proto ///////////////
443 static PyObject* {{funcname}}({{struct_type_decl}});
444
445 /////////////// ToPyCTupleUtility ///////////////
446 static PyObject* {{funcname}}({{struct_type_decl}} value) {
447 PyObject* item = NULL;
448 PyObject* result = PyTuple_New({{size}});
449 if (!result) goto bad;
450
451 {{for ix, component in enumerate(components):}}
452 {{py:attr = "value.f%s" % ix}}
453 item = {{component.to_py_function}}({{attr}});
454 if (!item) goto bad;
455 PyTuple_SET_ITEM(result, {{ix}}, item);
456 {{endfor}}
457
458 return result;
459 bad:
460 Py_XDECREF(item);
461 Py_XDECREF(result);
462 return NULL;
463 }
464
465
466 /////////////// FromPyCTupleUtility.proto ///////////////
467 static {{struct_type_decl}} {{funcname}}(PyObject *);
468
469 /////////////// FromPyCTupleUtility ///////////////
470 static {{struct_type_decl}} {{funcname}}(PyObject * o) {
471 {{struct_type_decl}} result;
472
473 if (!PyTuple_Check(o) || PyTuple_GET_SIZE(o) != {{size}}) {
474 PyErr_Format(PyExc_TypeError, "Expected %.16s of size %d, got %.200s", "a tuple", {{size}}, Py_TYPE(o)->tp_name);
475 goto bad;
476 }
477
478 #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
479 {{for ix, component in enumerate(components):}}
480 {{py:attr = "result.f%s" % ix}}
481 {{attr}} = {{component.from_py_function}}(PyTuple_GET_ITEM(o, {{ix}}));
482 if ({{component.error_condition(attr)}}) goto bad;
483 {{endfor}}
484 #else
485 {
486 PyObject *item;
487 {{for ix, component in enumerate(components):}}
488 {{py:attr = "result.f%s" % ix}}
489 item = PySequence_ITEM(o, {{ix}}); if (unlikely(!item)) goto bad;
490 {{attr}} = {{component.from_py_function}}(item);
491 Py_DECREF(item);
492 if ({{component.error_condition(attr)}}) goto bad;
493 {{endfor}}
494 }
495 #endif
496
497 return result;
498 bad:
499 return result;
500 }
501
502
503 /////////////// UnicodeAsUCS4.proto ///////////////
504
505 static CYTHON_INLINE Py_UCS4 __Pyx_PyUnicode_AsPy_UCS4(PyObject*);
506
507 /////////////// UnicodeAsUCS4 ///////////////
508
509 static CYTHON_INLINE Py_UCS4 __Pyx_PyUnicode_AsPy_UCS4(PyObject* x) {
510 Py_ssize_t length;
511 #if CYTHON_PEP393_ENABLED
512 length = PyUnicode_GET_LENGTH(x);
513 if (likely(length == 1)) {
514 return PyUnicode_READ_CHAR(x, 0);
515 }
516 #else
517 length = PyUnicode_GET_SIZE(x);
518 if (likely(length == 1)) {
519 return PyUnicode_AS_UNICODE(x)[0];
520 }
521 #if Py_UNICODE_SIZE == 2
522 else if (PyUnicode_GET_SIZE(x) == 2) {
523 Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0];
524 if (high_val >= 0xD800 && high_val <= 0xDBFF) {
525 Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1];
526 if (low_val >= 0xDC00 && low_val <= 0xDFFF) {
527 return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1)));
528 }
529 }
530 }
531 #endif
532 #endif
533 PyErr_Format(PyExc_ValueError,
534 "only single character unicode strings can be converted to Py_UCS4, "
535 "got length %" CYTHON_FORMAT_SSIZE_T "d", length);
536 return (Py_UCS4)-1;
537 }
538
539
540 /////////////// ObjectAsUCS4.proto ///////////////
541 //@requires: UnicodeAsUCS4
542
543 #define __Pyx_PyObject_AsPy_UCS4(x) \
544 (likely(PyUnicode_Check(x)) ? __Pyx_PyUnicode_AsPy_UCS4(x) : __Pyx__PyObject_AsPy_UCS4(x))
545 static Py_UCS4 __Pyx__PyObject_AsPy_UCS4(PyObject*);
546
547 /////////////// ObjectAsUCS4 ///////////////
548
549 static Py_UCS4 __Pyx__PyObject_AsPy_UCS4_raise_error(long ival) {
550 if (ival < 0) {
551 if (!PyErr_Occurred())
552 PyErr_SetString(PyExc_OverflowError,
553 "cannot convert negative value to Py_UCS4");
554 } else {
555 PyErr_SetString(PyExc_OverflowError,
556 "value too large to convert to Py_UCS4");
557 }
558 return (Py_UCS4)-1;
559 }
560
561 static Py_UCS4 __Pyx__PyObject_AsPy_UCS4(PyObject* x) {
562 long ival;
563 ival = __Pyx_PyInt_As_long(x);
564 if (unlikely(!__Pyx_is_valid_index(ival, 1114111 + 1))) {
565 return __Pyx__PyObject_AsPy_UCS4_raise_error(ival);
566 }
567 return (Py_UCS4)ival;
568 }
569
570
571 /////////////// ObjectAsPyUnicode.proto ///////////////
572
573 static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject*);
574
575 /////////////// ObjectAsPyUnicode ///////////////
576
577 static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) {
578 long ival;
579 #if CYTHON_PEP393_ENABLED
580 #if Py_UNICODE_SIZE > 2
581 const long maxval = 1114111;
582 #else
583 const long maxval = 65535;
584 #endif
585 #else
586 static long maxval = 0;
587 #endif
588 if (PyUnicode_Check(x)) {
589 if (unlikely(__Pyx_PyUnicode_GET_LENGTH(x) != 1)) {
590 PyErr_Format(PyExc_ValueError,
591 "only single character unicode strings can be converted to Py_UNICODE, "
592 "got length %" CYTHON_FORMAT_SSIZE_T "d", __Pyx_PyUnicode_GET_LENGTH(x));
593 return (Py_UNICODE)-1;
594 }
595 #if CYTHON_PEP393_ENABLED
596 ival = PyUnicode_READ_CHAR(x, 0);
597 #else
598 return PyUnicode_AS_UNICODE(x)[0];
599 #endif
600 } else {
601 #if !CYTHON_PEP393_ENABLED
602 if (unlikely(!maxval))
603 maxval = (long)PyUnicode_GetMax();
604 #endif
605 ival = __Pyx_PyInt_As_long(x);
606 }
607 if (unlikely(!__Pyx_is_valid_index(ival, maxval + 1))) {
608 if (ival < 0) {
609 if (!PyErr_Occurred())
610 PyErr_SetString(PyExc_OverflowError,
611 "cannot convert negative value to Py_UNICODE");
612 return (Py_UNICODE)-1;
613 } else {
614 PyErr_SetString(PyExc_OverflowError,
615 "value too large to convert to Py_UNICODE");
616 }
617 return (Py_UNICODE)-1;
618 }
619 return (Py_UNICODE)ival;
620 }
621
622
623 /////////////// CIntToPy.proto ///////////////
624
625 static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value);
626
627 /////////////// CIntToPy ///////////////
628 //@requires: GCCDiagnostics
629
630 static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
631 #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
632 #pragma GCC diagnostic push
633 #pragma GCC diagnostic ignored "-Wconversion"
634 #endif
635 const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
636 #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
637 #pragma GCC diagnostic pop
638 #endif
639 const int is_unsigned = neg_one > const_zero;
640 if (is_unsigned) {
641 if (sizeof({{TYPE}}) < sizeof(long)) {
642 return PyInt_FromLong((long) value);
643 } else if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
644 return PyLong_FromUnsignedLong((unsigned long) value);
645 #ifdef HAVE_LONG_LONG
646 } else if (sizeof({{TYPE}}) <= sizeof(unsigned PY_LONG_LONG)) {
647 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) value);
648 #endif
649 }
650 } else {
651 if (sizeof({{TYPE}}) <= sizeof(long)) {
652 return PyInt_FromLong((long) value);
653 #ifdef HAVE_LONG_LONG
654 } else if (sizeof({{TYPE}}) <= sizeof(PY_LONG_LONG)) {
655 return PyLong_FromLongLong((PY_LONG_LONG) value);
656 #endif
657 }
658 }
659 {
660 int one = 1; int little = (int)*(unsigned char *)&one;
661 unsigned char *bytes = (unsigned char *)&value;
662 return _PyLong_FromByteArray(bytes, sizeof({{TYPE}}),
663 little, !is_unsigned);
664 }
665 }
666
667
668 /////////////// CIntToDigits ///////////////
669
670 static const char DIGIT_PAIRS_10[2*10*10+1] = {
671 "00010203040506070809"
672 "10111213141516171819"
673 "20212223242526272829"
674 "30313233343536373839"
675 "40414243444546474849"
676 "50515253545556575859"
677 "60616263646566676869"
678 "70717273747576777879"
679 "80818283848586878889"
680 "90919293949596979899"
681 };
682
683 static const char DIGIT_PAIRS_8[2*8*8+1] = {
684 "0001020304050607"
685 "1011121314151617"
686 "2021222324252627"
687 "3031323334353637"
688 "4041424344454647"
689 "5051525354555657"
690 "6061626364656667"
691 "7071727374757677"
692 };
693
694 static const char DIGITS_HEX[2*16+1] = {
695 "0123456789abcdef"
696 "0123456789ABCDEF"
697 };
698
699
700 /////////////// CIntToPyUnicode.proto ///////////////
701
702 static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t width, char padding_char, char format_char);
703
704 /////////////// CIntToPyUnicode ///////////////
705 //@requires: StringTools.c::BuildPyUnicode
706 //@requires: CIntToDigits
707 //@requires: GCCDiagnostics
708
709 #ifdef _MSC_VER
710 #ifndef _MSC_STDINT_H_
711 #if _MSC_VER < 1300
712 typedef unsigned short uint16_t;
713 #else
714 typedef unsigned __int16 uint16_t;
715 #endif
716 #endif
717 #else
718 #include <stdint.h>
719 #endif
720
721 // NOTE: inlining because most arguments are constant, which collapses lots of code below
722
723 static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t width, char padding_char, char format_char) {
724 // simple and conservative C string allocation on the stack: each byte gives at most 3 digits, plus sign
725 char digits[sizeof({{TYPE}})*3+2];
726 // 'dpos' points to end of digits array + 1 initially to allow for pre-decrement looping
727 char *dpos, *end = digits + sizeof({{TYPE}})*3+2;
728 const char *hex_digits = DIGITS_HEX;
729 Py_ssize_t length, ulength;
730 int prepend_sign, last_one_off;
731 {{TYPE}} remaining;
732 #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
733 #pragma GCC diagnostic push
734 #pragma GCC diagnostic ignored "-Wconversion"
735 #endif
736 const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
737 #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
738 #pragma GCC diagnostic pop
739 #endif
740 const int is_unsigned = neg_one > const_zero;
741
742 if (format_char == 'X') {
743 hex_digits += 16;
744 format_char = 'x';
745 }
746
747 // surprise: even trivial sprintf() calls don't get optimised in gcc (4.8)
748 remaining = value; /* not using abs(value) to avoid overflow problems */
749 last_one_off = 0;
750 dpos = end;
751 do {
752 int digit_pos;
753 switch (format_char) {
754 case 'o':
755 digit_pos = abs((int)(remaining % (8*8)));
756 remaining = ({{TYPE}}) (remaining / (8*8));
757 dpos -= 2;
758 *(uint16_t*)dpos = ((const uint16_t*)DIGIT_PAIRS_8)[digit_pos]; /* copy 2 digits at a time */
759 last_one_off = (digit_pos < 8);
760 break;
761 case 'd':
762 digit_pos = abs((int)(remaining % (10*10)));
763 remaining = ({{TYPE}}) (remaining / (10*10));
764 dpos -= 2;
765 *(uint16_t*)dpos = ((const uint16_t*)DIGIT_PAIRS_10)[digit_pos]; /* copy 2 digits at a time */
766 last_one_off = (digit_pos < 10);
767 break;
768 case 'x':
769 *(--dpos) = hex_digits[abs((int)(remaining % 16))];
770 remaining = ({{TYPE}}) (remaining / 16);
771 break;
772 default:
773 assert(0);
774 break;
775 }
776 } while (unlikely(remaining != 0));
777
778 if (last_one_off) {
779 assert(*dpos == '0');
780 dpos++;
781 }
782 length = end - dpos;
783 ulength = length;
784 prepend_sign = 0;
785 if (!is_unsigned && value <= neg_one) {
786 if (padding_char == ' ' || width <= length + 1) {
787 *(--dpos) = '-';
788 ++length;
789 } else {
790 prepend_sign = 1;
791 }
792 ++ulength;
793 }
794 if (width > ulength) {
795 ulength = width;
796 }
797 // single character unicode strings are cached in CPython => use PyUnicode_FromOrdinal() for them
798 if (ulength == 1) {
799 return PyUnicode_FromOrdinal(*dpos);
800 }
801 return __Pyx_PyUnicode_BuildFromAscii(ulength, dpos, (int) length, prepend_sign, padding_char);
802 }
803
804
805 /////////////// CBIntToPyUnicode.proto ///////////////
806
807 #define {{TO_PY_FUNCTION}}(value) \
808 ((value) ? __Pyx_NewRef({{TRUE_CONST}}) : __Pyx_NewRef({{FALSE_CONST}}))
809
810
811 /////////////// PyIntFromDouble.proto ///////////////
812
813 #if PY_MAJOR_VERSION < 3
814 static CYTHON_INLINE PyObject* __Pyx_PyInt_FromDouble(double value);
815 #else
816 #define __Pyx_PyInt_FromDouble(value) PyLong_FromDouble(value)
817 #endif
818
819 /////////////// PyIntFromDouble ///////////////
820
821 #if PY_MAJOR_VERSION < 3
822 static CYTHON_INLINE PyObject* __Pyx_PyInt_FromDouble(double value) {
823 if (value >= (double)LONG_MIN && value <= (double)LONG_MAX) {
824 return PyInt_FromLong((long)value);
825 }
826 return PyLong_FromDouble(value);
827 }
828 #endif
829
830
831 /////////////// CIntFromPyVerify ///////////////
832
833 // see CIntFromPy
834 #define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value) \
835 __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 0)
836
837 #define __PYX_VERIFY_RETURN_INT_EXC(target_type, func_type, func_value) \
838 __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 1)
839
840 #define __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, exc) \
841 { \
842 func_type value = func_value; \
843 if (sizeof(target_type) < sizeof(func_type)) { \
844 if (unlikely(value != (func_type) (target_type) value)) { \
845 func_type zero = 0; \
846 if (exc && unlikely(value == (func_type)-1 && PyErr_Occurred())) \
847 return (target_type) -1; \
848 if (is_unsigned && unlikely(value < zero)) \
849 goto raise_neg_overflow; \
850 else \
851 goto raise_overflow; \
852 } \
853 } \
854 return (target_type) value; \
855 }
856
857
858 /////////////// CIntFromPy.proto ///////////////
859
860 static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *);
861
862 /////////////// CIntFromPy ///////////////
863 //@requires: CIntFromPyVerify
864 //@requires: GCCDiagnostics
865
866 {{py: from Cython.Utility import pylong_join }}
867
868 static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
869 #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
870 #pragma GCC diagnostic push
871 #pragma GCC diagnostic ignored "-Wconversion"
872 #endif
873 const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
874 #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
875 #pragma GCC diagnostic pop
876 #endif
877 const int is_unsigned = neg_one > const_zero;
878 #if PY_MAJOR_VERSION < 3
879 if (likely(PyInt_Check(x))) {
880 if (sizeof({{TYPE}}) < sizeof(long)) {
881 __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyInt_AS_LONG(x))
882 } else {
883 long val = PyInt_AS_LONG(x);
884 if (is_unsigned && unlikely(val < 0)) {
885 goto raise_neg_overflow;
886 }
887 return ({{TYPE}}) val;
888 }
889 } else
890 #endif
891 if (likely(PyLong_Check(x))) {
892 if (is_unsigned) {
893 #if CYTHON_USE_PYLONG_INTERNALS
894 const digit* digits = ((PyLongObject*)x)->ob_digit;
895 switch (Py_SIZE(x)) {
896 case 0: return ({{TYPE}}) 0;
897 case 1: __PYX_VERIFY_RETURN_INT({{TYPE}}, digit, digits[0])
898 {{for _size in (2, 3, 4)}}
899 case {{_size}}:
900 if (8 * sizeof({{TYPE}}) > {{_size-1}} * PyLong_SHIFT) {
901 if (8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT) {
902 __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, {{pylong_join(_size, 'digits')}})
903 } else if (8 * sizeof({{TYPE}}) >= {{_size}} * PyLong_SHIFT) {
904 return ({{TYPE}}) {{pylong_join(_size, 'digits', TYPE)}};
905 }
906 }
907 break;
908 {{endfor}}
909 }
910 #endif
911 #if CYTHON_COMPILING_IN_CPYTHON
912 if (unlikely(Py_SIZE(x) < 0)) {
913 goto raise_neg_overflow;
914 }
915 #else
916 {
917 // misuse Py_False as a quick way to compare to a '0' int object in PyPy
918 int result = PyObject_RichCompareBool(x, Py_False, Py_LT);
919 if (unlikely(result < 0))
920 return ({{TYPE}}) -1;
921 if (unlikely(result == 1))
922 goto raise_neg_overflow;
923 }
924 #endif
925 if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
926 __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, unsigned long, PyLong_AsUnsignedLong(x))
927 #ifdef HAVE_LONG_LONG
928 } else if (sizeof({{TYPE}}) <= sizeof(unsigned PY_LONG_LONG)) {
929 __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x))
930 #endif
931 }
932 } else {
933 // signed
934 #if CYTHON_USE_PYLONG_INTERNALS
935 const digit* digits = ((PyLongObject*)x)->ob_digit;
936 switch (Py_SIZE(x)) {
937 case 0: return ({{TYPE}}) 0;
938 case -1: __PYX_VERIFY_RETURN_INT({{TYPE}}, sdigit, (sdigit) (-(sdigit)digits[0]))
939 case 1: __PYX_VERIFY_RETURN_INT({{TYPE}}, digit, +digits[0])
940 {{for _size in (2, 3, 4)}}
941 {{for _case in (-_size, _size)}}
942 case {{_case}}:
943 if (8 * sizeof({{TYPE}}){{' - 1' if _case < 0 else ''}} > {{_size-1}} * PyLong_SHIFT) {
944 if (8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT) {
945 __PYX_VERIFY_RETURN_INT({{TYPE}}, {{'long' if _case < 0 else 'unsigned long'}}, {{'-(long) ' if _case < 0 else ''}}{{pylong_join(_size, 'digits')}})
946 } else if (8 * sizeof({{TYPE}}) - 1 > {{_size}} * PyLong_SHIFT) {
947 return ({{TYPE}}) ({{'((%s)-1)*' % TYPE if _case < 0 else ''}}{{pylong_join(_size, 'digits', TYPE)}});
948 }
949 }
950 break;
951 {{endfor}}
952 {{endfor}}
953 }
954 #endif
955 if (sizeof({{TYPE}}) <= sizeof(long)) {
956 __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, long, PyLong_AsLong(x))
957 #ifdef HAVE_LONG_LONG
958 } else if (sizeof({{TYPE}}) <= sizeof(PY_LONG_LONG)) {
959 __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, PY_LONG_LONG, PyLong_AsLongLong(x))
960 #endif
961 }
962 }
963 {
964 #if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
965 PyErr_SetString(PyExc_RuntimeError,
966 "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
967 #else
968 {{TYPE}} val;
969 PyObject *v = __Pyx_PyNumber_IntOrLong(x);
970 #if PY_MAJOR_VERSION < 3
971 if (likely(v) && !PyLong_Check(v)) {
972 PyObject *tmp = v;
973 v = PyNumber_Long(tmp);
974 Py_DECREF(tmp);
975 }
976 #endif
977 if (likely(v)) {
978 int one = 1; int is_little = (int)*(unsigned char *)&one;
979 unsigned char *bytes = (unsigned char *)&val;
980 int ret = _PyLong_AsByteArray((PyLongObject *)v,
981 bytes, sizeof(val),
982 is_little, !is_unsigned);
983 Py_DECREF(v);
984 if (likely(!ret))
985 return val;
986 }
987 #endif
988 return ({{TYPE}}) -1;
989 }
990 } else {
991 {{TYPE}} val;
992 PyObject *tmp = __Pyx_PyNumber_IntOrLong(x);
993 if (!tmp) return ({{TYPE}}) -1;
994 val = {{FROM_PY_FUNCTION}}(tmp);
995 Py_DECREF(tmp);
996 return val;
997 }
998
999 raise_overflow:
1000 PyErr_SetString(PyExc_OverflowError,
1001 "value too large to convert to {{TYPE}}");
1002 return ({{TYPE}}) -1;
1003
1004 raise_neg_overflow:
1005 PyErr_SetString(PyExc_OverflowError,
1006 "can't convert negative value to {{TYPE}}");
1007 return ({{TYPE}}) -1;
1008 }
1009