Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / cython / src / Cython / Utility / TypeConversion.c
1 /////////////// TypeConversions.proto ///////////////
2
3 /* Type Conversion Predeclarations */
4
5 #define __Pyx_fits_Py_ssize_t(v, type, is_signed)  (    \
6     (sizeof(type) < sizeof(Py_ssize_t))  ||             \
7     (sizeof(type) > sizeof(Py_ssize_t) &&               \
8           likely(v < (type)PY_SSIZE_T_MAX ||            \
9                  v == (type)PY_SSIZE_T_MAX)  &&         \
10           (!is_signed || likely(v > (type)PY_SSIZE_T_MIN ||       \
11                                 v == (type)PY_SSIZE_T_MIN)))  ||  \
12     (sizeof(type) == sizeof(Py_ssize_t) &&              \
13           (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||        \
14                                v == (type)PY_SSIZE_T_MAX)))  )
15
16 static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject*);
17 static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length);
18
19 #define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s))
20 #define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l)
21 #define __Pyx_PyBytes_FromString        PyBytes_FromString
22 #define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
23 static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*);
24
25 #if PY_MAJOR_VERSION < 3
26     #define __Pyx_PyStr_FromString        __Pyx_PyBytes_FromString
27     #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize
28 #else
29     #define __Pyx_PyStr_FromString        __Pyx_PyUnicode_FromString
30     #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize
31 #endif
32
33 #define __Pyx_PyObject_AsSString(s)    ((signed char*) __Pyx_PyObject_AsString(s))
34 #define __Pyx_PyObject_AsUString(s)    ((unsigned char*) __Pyx_PyObject_AsString(s))
35 #define __Pyx_PyObject_FromUString(s)  __Pyx_PyObject_FromString((const char*)s)
36 #define __Pyx_PyBytes_FromUString(s)   __Pyx_PyBytes_FromString((const char*)s)
37 #define __Pyx_PyByteArray_FromUString(s)   __Pyx_PyByteArray_FromString((const char*)s)
38 #define __Pyx_PyStr_FromUString(s)     __Pyx_PyStr_FromString((const char*)s)
39 #define __Pyx_PyUnicode_FromUString(s) __Pyx_PyUnicode_FromString((const char*)s)
40
41 #if PY_MAJOR_VERSION < 3
42 static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u)
43 {
44     const Py_UNICODE *u_end = u;
45     while (*u_end++) ;
46     return (size_t)(u_end - u - 1);
47 }
48 #else
49 #define __Pyx_Py_UNICODE_strlen Py_UNICODE_strlen
50 #endif
51
52 #define __Pyx_PyUnicode_FromUnicode(u)       PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u))
53 #define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode
54 #define __Pyx_PyUnicode_AsUnicode            PyUnicode_AsUnicode
55
56 #define __Pyx_Owned_Py_None(b) (Py_INCREF(Py_None), Py_None)
57 #define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
58 static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);
59 static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);
60
61 static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
62 static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
63
64 #if CYTHON_COMPILING_IN_CPYTHON
65 #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
66 #else
67 #define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)
68 #endif
69 #define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x))
70
71 #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
72 static int __Pyx_sys_getdefaultencoding_not_ascii;
73 static int __Pyx_init_sys_getdefaultencoding_params(void) {
74     PyObject* sys;
75     PyObject* default_encoding = NULL;
76     PyObject* ascii_chars_u = NULL;
77     PyObject* ascii_chars_b = NULL;
78     const char* default_encoding_c;
79     sys = PyImport_ImportModule("sys");
80     if (!sys) goto bad;
81     default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
82     Py_DECREF(sys);
83     if (!default_encoding) goto bad;
84     default_encoding_c = PyBytes_AsString(default_encoding);
85     if (!default_encoding_c) goto bad;
86     if (strcmp(default_encoding_c, "ascii") == 0) {
87         __Pyx_sys_getdefaultencoding_not_ascii = 0;
88     } else {
89         char ascii_chars[128];
90         int c;
91         for (c = 0; c < 128; c++) {
92             ascii_chars[c] = c;
93         }
94         __Pyx_sys_getdefaultencoding_not_ascii = 1;
95         ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL);
96         if (!ascii_chars_u) goto bad;
97         ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL);
98         if (!ascii_chars_b || !PyBytes_Check(ascii_chars_b) || memcmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) {
99             PyErr_Format(
100                 PyExc_ValueError,
101                 "This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.",
102                 default_encoding_c);
103             goto bad;
104         }
105         Py_DECREF(ascii_chars_u);
106         Py_DECREF(ascii_chars_b);
107     }
108     Py_DECREF(default_encoding);
109     return 0;
110 bad:
111     Py_XDECREF(default_encoding);
112     Py_XDECREF(ascii_chars_u);
113     Py_XDECREF(ascii_chars_b);
114     return -1;
115 }
116 #endif 
117
118 #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3
119 #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL)
120 #else
121 #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL)
122
123 // __PYX_DEFAULT_STRING_ENCODING is either a user provided string constant
124 // or we need to look it up here
125 #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
126 static char* __PYX_DEFAULT_STRING_ENCODING;
127
128 static int __Pyx_init_sys_getdefaultencoding_params(void) {
129     PyObject* sys;
130     PyObject* default_encoding = NULL;
131     char* default_encoding_c;
132
133     sys = PyImport_ImportModule("sys");
134     if (!sys) goto bad;
135     default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
136     Py_DECREF(sys);
137     if (!default_encoding) goto bad;
138     default_encoding_c = PyBytes_AsString(default_encoding);
139     if (!default_encoding_c) goto bad;
140     __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c));
141     if (!__PYX_DEFAULT_STRING_ENCODING) goto bad;
142     strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);
143     Py_DECREF(default_encoding);
144     return 0;
145 bad:
146     Py_XDECREF(default_encoding);
147     return -1;
148 }
149 #endif
150 #endif
151
152 /////////////// TypeConversions ///////////////
153
154 /* Type Conversion Functions */
155
156 static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) {
157     return __Pyx_PyUnicode_FromStringAndSize(c_str, (Py_ssize_t)strlen(c_str));
158 }
159
160 static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {
161     Py_ssize_t ignore;
162     return __Pyx_PyObject_AsStringAndSize(o, &ignore);
163 }
164
165 static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
166 #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
167     if (
168 #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
169             __Pyx_sys_getdefaultencoding_not_ascii && 
170 #endif
171             PyUnicode_Check(o)) {
172 #if PY_VERSION_HEX < 0x03030000
173         char* defenc_c;
174         // borrowed, cached reference
175         PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL);
176         if (!defenc) return NULL;
177         defenc_c = PyBytes_AS_STRING(defenc);
178 #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
179         {
180             char* end = defenc_c + PyBytes_GET_SIZE(defenc);
181             char* c;
182             for (c = defenc_c; c < end; c++) {
183                 if ((unsigned char) (*c) >= 128) {
184                     // raise the error
185                     PyUnicode_AsASCIIString(o);
186                     return NULL;
187                 }
188             }
189         }
190 #endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/
191         *length = PyBytes_GET_SIZE(defenc);
192         return defenc_c;
193 #else /* PY_VERSION_HEX < 0x03030000 */
194         if (PyUnicode_READY(o) == -1) return NULL;
195 #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
196         if (PyUnicode_IS_ASCII(o)) {
197             // cached for the lifetime of the object
198             *length = PyUnicode_GET_LENGTH(o);
199             return PyUnicode_AsUTF8(o);
200         } else {
201             // raise the error
202             PyUnicode_AsASCIIString(o);
203             return NULL;
204         }
205 #else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
206         return PyUnicode_AsUTF8AndSize(o, length);
207 #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
208 #endif /* PY_VERSION_HEX < 0x03030000 */
209     } else
210 #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII  || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */
211
212 #if !CYTHON_COMPILING_IN_PYPY
213 #if PY_VERSION_HEX >= 0x02060000
214     if (PyByteArray_Check(o)) {
215         *length = PyByteArray_GET_SIZE(o);
216         return PyByteArray_AS_STRING(o);
217     } else
218 #endif
219 #endif
220     {
221         char* result;
222         int r = PyBytes_AsStringAndSize(o, &result, length);
223         if (unlikely(r < 0)) {
224             return NULL;
225         } else {
226             return result;
227         }
228     }
229 }
230
231 /* Note: __Pyx_PyObject_IsTrue is written to minimize branching. */
232 static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
233    int is_true = x == Py_True;
234    if (is_true | (x == Py_False) | (x == Py_None)) return is_true;
235    else return PyObject_IsTrue(x);
236 }
237
238 static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) {
239   PyNumberMethods *m;
240   const char *name = NULL;
241   PyObject *res = NULL;
242 #if PY_MAJOR_VERSION < 3
243   if (PyInt_Check(x) || PyLong_Check(x))
244 #else
245   if (PyLong_Check(x))
246 #endif
247     return Py_INCREF(x), x;
248   m = Py_TYPE(x)->tp_as_number;
249 #if PY_MAJOR_VERSION < 3
250   if (m && m->nb_int) {
251     name = "int";
252     res = PyNumber_Int(x);
253   }
254   else if (m && m->nb_long) {
255     name = "long";
256     res = PyNumber_Long(x);
257   }
258 #else
259   if (m && m->nb_int) {
260     name = "int";
261     res = PyNumber_Long(x);
262   }
263 #endif
264   if (res) {
265 #if PY_MAJOR_VERSION < 3
266     if (!PyInt_Check(res) && !PyLong_Check(res)) {
267 #else
268     if (!PyLong_Check(res)) {
269 #endif
270       PyErr_Format(PyExc_TypeError,
271                    "__%.4s__ returned non-%.4s (type %.200s)",
272                    name, name, Py_TYPE(res)->tp_name);
273       Py_DECREF(res);
274       return NULL;
275     }
276   }
277   else if (!PyErr_Occurred()) {
278     PyErr_SetString(PyExc_TypeError,
279                     "an integer is required");
280   }
281   return res;
282 }
283
284 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
285  #if CYTHON_USE_PYLONG_INTERNALS
286   #include "longintrepr.h"
287  #endif
288 #endif
289 static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
290   Py_ssize_t ival;
291   PyObject *x;
292 #if PY_MAJOR_VERSION < 3
293   if (likely(PyInt_CheckExact(b)))
294       return PyInt_AS_LONG(b);
295 #endif
296   if (likely(PyLong_CheckExact(b))) {
297     #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
298      #if CYTHON_USE_PYLONG_INTERNALS
299        switch (Py_SIZE(b)) {
300        case -1: return -(sdigit)((PyLongObject*)b)->ob_digit[0];
301        case  0: return 0;
302        case  1: return ((PyLongObject*)b)->ob_digit[0];
303        }
304      #endif
305     #endif
306   #if PY_VERSION_HEX < 0x02060000
307     return PyInt_AsSsize_t(b);
308   #else
309     return PyLong_AsSsize_t(b);
310   #endif
311   }
312   x = PyNumber_Index(b);
313   if (!x) return -1;
314   ival = PyInt_AsSsize_t(x);
315   Py_DECREF(x);
316   return ival;
317 }
318
319 static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
320 #if PY_VERSION_HEX < 0x02050000
321    if (ival <= LONG_MAX)
322        return PyInt_FromLong((long)ival);
323    else {
324        unsigned char *bytes = (unsigned char *) &ival;
325        int one = 1; int little = (int)*(unsigned char*)&one;
326        return _PyLong_FromByteArray(bytes, sizeof(size_t), little, 0);
327    }
328 #else
329    return PyInt_FromSize_t(ival);
330 #endif
331 }
332
333
334 /////////////// FromPyStructUtility.proto ///////////////
335 {{struct_type_decl}};
336 static {{struct_type_decl}} {{funcname}}(PyObject *);
337
338 /////////////// FromPyStructUtility ///////////////
339 static {{struct_type_decl}} {{funcname}}(PyObject * o) {
340     {{struct_type_decl}} result;
341     PyObject *value = NULL;
342
343     if (!PyMapping_Check(o)) {
344         PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "a mapping", Py_TYPE(o)->tp_name);
345         goto bad;
346     }
347
348     {{for member in var_entries:}}
349         {{py:attr = "result." + member.cname}}
350
351         value = PyObject_GetItem(o, PYIDENT("{{member.name}}"));
352         if (!value) {
353             PyErr_Format(PyExc_ValueError, \
354                 "No value specified for struct attribute '%.{{max(200, len(member.name))}}s'", "{{member.name}}");
355             goto bad;
356         }
357         {{attr}} = {{member.type.from_py_function}}(value);
358         if ({{member.type.error_condition(attr)}})
359             goto bad;
360
361         Py_DECREF(value);
362     {{endfor}}
363
364     return result;
365 bad:
366     Py_XDECREF(value);
367     return result;
368 }
369
370 /////////////// ObjectAsUCS4.proto ///////////////
371
372 static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject*);
373
374 /////////////// ObjectAsUCS4 ///////////////
375
376 static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject* x) {
377    long ival;
378    if (PyUnicode_Check(x)) {
379        Py_ssize_t length;
380        #if CYTHON_PEP393_ENABLED
381        length = PyUnicode_GET_LENGTH(x);
382        if (likely(length == 1)) {
383            return PyUnicode_READ_CHAR(x, 0);
384        }
385        #else
386        length = PyUnicode_GET_SIZE(x);
387        if (likely(length == 1)) {
388            return PyUnicode_AS_UNICODE(x)[0];
389        }
390        #if Py_UNICODE_SIZE == 2
391        else if (PyUnicode_GET_SIZE(x) == 2) {
392            Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0];
393            if (high_val >= 0xD800 && high_val <= 0xDBFF) {
394                Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1];
395                if (low_val >= 0xDC00 && low_val <= 0xDFFF) {
396                    return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1)));
397                }
398            }
399        }
400        #endif
401        #endif
402        PyErr_Format(PyExc_ValueError,
403                     "only single character unicode strings can be converted to Py_UCS4, "
404                     "got length %" CYTHON_FORMAT_SSIZE_T "d", length);
405        return (Py_UCS4)-1;
406    }
407    ival = __Pyx_PyInt_As_long(x);
408    if (unlikely(ival < 0)) {
409        if (!PyErr_Occurred())
410            PyErr_SetString(PyExc_OverflowError,
411                            "cannot convert negative value to Py_UCS4");
412        return (Py_UCS4)-1;
413    } else if (unlikely(ival > 1114111)) {
414        PyErr_SetString(PyExc_OverflowError,
415                        "value too large to convert to Py_UCS4");
416        return (Py_UCS4)-1;
417    }
418    return (Py_UCS4)ival;
419 }
420
421 /////////////// ObjectAsPyUnicode.proto ///////////////
422
423 static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject*);
424
425 /////////////// ObjectAsPyUnicode ///////////////
426
427 static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) {
428     long ival;
429     #if CYTHON_PEP393_ENABLED
430     #if Py_UNICODE_SIZE > 2
431     const long maxval = 1114111;
432     #else
433     const long maxval = 65535;
434     #endif
435     #else
436     static long maxval = 0;
437     #endif
438     if (PyUnicode_Check(x)) {
439         if (unlikely(__Pyx_PyUnicode_GET_LENGTH(x) != 1)) {
440             PyErr_Format(PyExc_ValueError,
441                          "only single character unicode strings can be converted to Py_UNICODE, "
442                          "got length %" CYTHON_FORMAT_SSIZE_T "d", __Pyx_PyUnicode_GET_LENGTH(x));
443             return (Py_UNICODE)-1;
444         }
445         #if CYTHON_PEP393_ENABLED
446         ival = PyUnicode_READ_CHAR(x, 0);
447         #else
448         return PyUnicode_AS_UNICODE(x)[0];
449         #endif
450     } else {
451         #if !CYTHON_PEP393_ENABLED
452         if (unlikely(!maxval))
453             maxval = (long)PyUnicode_GetMax();
454         #endif
455         ival = __Pyx_PyInt_As_long(x);
456     }
457     if (unlikely(ival < 0)) {
458         if (!PyErr_Occurred())
459             PyErr_SetString(PyExc_OverflowError,
460                             "cannot convert negative value to Py_UNICODE");
461         return (Py_UNICODE)-1;
462     } else if (unlikely(ival > maxval)) {
463         PyErr_SetString(PyExc_OverflowError,
464                         "value too large to convert to Py_UNICODE");
465         return (Py_UNICODE)-1;
466     }
467     return (Py_UNICODE)ival;
468 }
469
470
471 /////////////// CIntToPy.proto ///////////////
472
473 static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value);
474
475 /////////////// CIntToPy ///////////////
476
477 static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
478     const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = 0;
479     const int is_unsigned = neg_one > const_zero;
480     if (is_unsigned) {
481         if (sizeof({{TYPE}}) < sizeof(long)) {
482             return PyInt_FromLong((long) value);
483         } else if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
484             return PyLong_FromUnsignedLong((unsigned long) value);
485         } else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) {
486             return PyLong_FromUnsignedLongLong((unsigned long long) value);
487         }
488     } else {
489         if (sizeof({{TYPE}}) <= sizeof(long)) {
490             return PyInt_FromLong((long) value);
491         } else if (sizeof({{TYPE}}) <= sizeof(long long)) {
492             return PyLong_FromLongLong((long long) value);
493         }
494     }
495     {
496         int one = 1; int little = (int)*(unsigned char *)&one;
497         unsigned char *bytes = (unsigned char *)&value;
498         return _PyLong_FromByteArray(bytes, sizeof({{TYPE}}),
499                                      little, !is_unsigned);
500     }
501 }
502
503
504 /////////////// CIntFromPyVerify ///////////////
505
506 #define __PYX_VERIFY_RETURN_INT(target_type, func_type, func)             \
507     {                                                                     \
508         func_type value = func(x);                                        \
509         if (sizeof(target_type) < sizeof(func_type)) {                    \
510             if (unlikely(value != (func_type) (target_type) value)) {     \
511                 func_type zero = 0;                                       \
512                 PyErr_SetString(PyExc_OverflowError,                      \
513                     (is_unsigned && unlikely(value < zero)) ?             \
514                     "can't convert negative value to " #target_type :     \
515                     "value too large to convert to " #target_type);       \
516                 return (target_type) -1;                                  \
517             }                                                             \
518         }                                                                 \
519         return (target_type) value;                                       \
520     }
521
522
523 /////////////// CIntFromPy.proto ///////////////
524
525 static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *);
526
527 /////////////// CIntFromPy ///////////////
528 //@requires: CIntFromPyVerify
529
530 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
531  #if CYTHON_USE_PYLONG_INTERNALS
532   #include "longintrepr.h"
533  #endif
534 #endif
535 static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
536     const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = 0;
537     const int is_unsigned = neg_one > const_zero;
538 #if PY_MAJOR_VERSION < 3
539     if (likely(PyInt_Check(x))) {
540         if (sizeof({{TYPE}}) < sizeof(long)) {
541             __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyInt_AS_LONG)
542         } else {
543             long val = PyInt_AS_LONG(x);
544             if (is_unsigned && unlikely(val < 0)) {
545                 PyErr_SetString(PyExc_OverflowError,
546                                 "can't convert negative value to {{TYPE}}");
547                 return ({{TYPE}}) -1;
548             }
549             return ({{TYPE}}) val;
550         }
551     } else
552 #endif
553     if (likely(PyLong_Check(x))) {
554         if (is_unsigned) {
555 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
556  #if CYTHON_USE_PYLONG_INTERNALS
557             if (sizeof(digit) <= sizeof({{TYPE}})) {
558                 switch (Py_SIZE(x)) {
559                     case  0: return 0;
560                     case  1: return ({{TYPE}}) ((PyLongObject*)x)->ob_digit[0];
561                 }
562             }
563  #endif
564 #endif
565             if (unlikely(Py_SIZE(x) < 0)) {
566                 PyErr_SetString(PyExc_OverflowError,
567                                 "can't convert negative value to {{TYPE}}");
568                 return ({{TYPE}}) -1;
569             }
570             if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
571                 __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, PyLong_AsUnsignedLong)
572             } else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) {
573                 __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long long, PyLong_AsUnsignedLongLong)
574             }
575         } else {
576 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
577  #if CYTHON_USE_PYLONG_INTERNALS
578             if (sizeof(digit) <= sizeof({{TYPE}})) {
579                 switch (Py_SIZE(x)) {
580                     case  0: return 0;
581                     case  1: return +({{TYPE}}) ((PyLongObject*)x)->ob_digit[0];
582                     case -1: return -({{TYPE}}) ((PyLongObject*)x)->ob_digit[0];
583                 }
584             }
585  #endif
586 #endif
587             if (sizeof({{TYPE}}) <= sizeof(long)) {
588                 __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyLong_AsLong)
589             } else if (sizeof({{TYPE}}) <= sizeof(long long)) {
590                 __PYX_VERIFY_RETURN_INT({{TYPE}}, long long, PyLong_AsLongLong)
591             }
592         }
593         {
594 #if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
595             PyErr_SetString(PyExc_RuntimeError,
596                             "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
597 #else
598             {{TYPE}} val;
599             PyObject *v = __Pyx_PyNumber_Int(x);
600  #if PY_MAJOR_VERSION < 3
601             if (likely(v) && !PyLong_Check(v)) {
602                 PyObject *tmp = v;
603                 v = PyNumber_Long(tmp);
604                 Py_DECREF(tmp);
605             }
606  #endif
607             if (likely(v)) {
608                 int one = 1; int is_little = (int)*(unsigned char *)&one;
609                 unsigned char *bytes = (unsigned char *)&val;
610                 int ret = _PyLong_AsByteArray((PyLongObject *)v,
611                                               bytes, sizeof(val),
612                                               is_little, !is_unsigned);
613                 Py_DECREF(v);
614                 if (likely(!ret))
615                     return val;
616             }
617 #endif
618             return ({{TYPE}}) -1;
619         }
620     } else {
621         {{TYPE}} val;
622         PyObject *tmp = __Pyx_PyNumber_Int(x);
623         if (!tmp) return ({{TYPE}}) -1;
624         val = {{FROM_PY_FUNCTION}}(tmp);
625         Py_DECREF(tmp);
626         return val;
627     }
628 }
629