TypeConversion.c 21.4 KB
Newer Older
1 2 3 4
/////////////// TypeConversions.proto ///////////////

/* Type Conversion Predeclarations */

5 6 7 8 9 10 11 12 13 14
#define __Pyx_fits_Py_ssize_t(v, type, is_signed)  (    \
    (sizeof(type) < sizeof(Py_ssize_t))  ||             \
    (sizeof(type) > sizeof(Py_ssize_t) &&               \
          likely(v < (type)PY_SSIZE_T_MAX ||            \
                 v == (type)PY_SSIZE_T_MAX)  &&         \
          (!is_signed || likely(v > (type)PY_SSIZE_T_MIN ||       \
                                v == (type)PY_SSIZE_T_MIN)))  ||  \
    (sizeof(type) == sizeof(Py_ssize_t) &&              \
          (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||        \
                               v == (type)PY_SSIZE_T_MAX)))  )
15

16 17 18
static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject*);
static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length);

19 20
#define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s))
#define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l)
21 22 23 24
#define __Pyx_PyBytes_FromString        PyBytes_FromString
#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char*);

25
#if PY_MAJOR_VERSION < 3
26 27 28 29 30 31 32
    #define __Pyx_PyStr_FromString        __Pyx_PyBytes_FromString
    #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize
#else
    #define __Pyx_PyStr_FromString        __Pyx_PyUnicode_FromString
    #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize
#endif

33
#define __Pyx_PyObject_AsSString(s)    ((signed char*) __Pyx_PyObject_AsString(s))
34 35 36
#define __Pyx_PyObject_AsUString(s)    ((unsigned char*) __Pyx_PyObject_AsString(s))
#define __Pyx_PyObject_FromUString(s)  __Pyx_PyObject_FromString((char*)s)
#define __Pyx_PyBytes_FromUString(s)   __Pyx_PyBytes_FromString((char*)s)
37
#define __Pyx_PyByteArray_FromUString(s)   __Pyx_PyByteArray_FromString((char*)s)
38 39
#define __Pyx_PyStr_FromUString(s)     __Pyx_PyStr_FromString((char*)s)
#define __Pyx_PyUnicode_FromUString(s) __Pyx_PyUnicode_FromString((char*)s)
40

41 42 43 44 45 46 47
#if PY_MAJOR_VERSION < 3
static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u)
{
    const Py_UNICODE *u_end = u;
    while (*u_end++) ;
    return u_end - u - 1;
}
48
#else
49
#define __Pyx_Py_UNICODE_strlen Py_UNICODE_strlen
50 51
#endif

52 53 54 55
#define __Pyx_PyUnicode_FromUnicode(u)       PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u))
#define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode
#define __Pyx_PyUnicode_AsUnicode            PyUnicode_AsUnicode

56 57 58 59 60 61
#define __Pyx_Owned_Py_None(b) (Py_INCREF(Py_None), Py_None)
#define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);
static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);

static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
62
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
63

64 65 66 67 68 69 70
#if CYTHON_COMPILING_IN_CPYTHON
#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
#else
#define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)
#endif
#define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x))

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 75
    PyObject* sys = NULL;
    PyObject* default_encoding = NULL;
Robert Bradshaw's avatar
Robert Bradshaw committed
76 77
    PyObject* ascii_chars_u = NULL;
    PyObject* ascii_chars_b = NULL;
78 79 80 81 82 83 84
    sys = PyImport_ImportModule("sys");
    if (sys == NULL) goto bad;
    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
    if (default_encoding == NULL) goto bad;
    if (strcmp(PyBytes_AsString(default_encoding), "ascii") == 0) {
        __Pyx_sys_getdefaultencoding_not_ascii = 0;
    } else {
Robert Bradshaw's avatar
Robert Bradshaw committed
85 86 87 88 89 90 91 92 93 94 95 96 97
        const char* default_encoding_c = PyBytes_AS_STRING(default_encoding);
        char ascii_chars[128];
        int c;
        for (c = 0; c < 128; c++) {
            ascii_chars[c] = c;
        }
        __Pyx_sys_getdefaultencoding_not_ascii = 1;
        ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL);
        if (ascii_chars_u == NULL) goto bad;
        ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL);
        if (ascii_chars_b == NULL || strncmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) {
            PyErr_Format(
                PyExc_ValueError,
98
                "This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.",
Robert Bradshaw's avatar
Robert Bradshaw committed
99 100
                default_encoding_c);
            goto bad;
101 102 103 104
        }
    }
    Py_XDECREF(sys);
    Py_XDECREF(default_encoding);
Robert Bradshaw's avatar
Robert Bradshaw committed
105 106
    Py_XDECREF(ascii_chars_u);
    Py_XDECREF(ascii_chars_b);
107 108 109 110
    return 0;
bad:
    Py_XDECREF(sys);
    Py_XDECREF(default_encoding);
Robert Bradshaw's avatar
Robert Bradshaw committed
111 112
    Py_XDECREF(ascii_chars_u);
    Py_XDECREF(ascii_chars_b);
113 114
    return -1;
}
Robert Bradshaw's avatar
Robert Bradshaw committed
115 116
#endif 

117 118 119 120 121 122 123
#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3
#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL)
#else
#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL)

// __PYX_DEFAULT_STRING_ENCODING is either a user provided string constant
// or we need to look it up here
Robert Bradshaw's avatar
Robert Bradshaw committed
124 125
#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
static char* __PYX_DEFAULT_STRING_ENCODING;
126

127
static int __Pyx_init_sys_getdefaultencoding_params(void) {
Robert Bradshaw's avatar
Robert Bradshaw committed
128 129 130 131 132 133 134 135 136 137
    PyObject* sys = NULL;
    PyObject* default_encoding = NULL;
    char* default_encoding_c;
    sys = PyImport_ImportModule("sys");
    if (sys == NULL) goto bad;
    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
    if (default_encoding == NULL) goto bad;
    default_encoding_c = PyBytes_AS_STRING(default_encoding);
    __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c));
    strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);
138 139
    Py_DECREF(sys);
    Py_DECREF(default_encoding);
Robert Bradshaw's avatar
Robert Bradshaw committed
140 141 142 143 144 145 146
    return 0;
bad:
    Py_XDECREF(sys);
    Py_XDECREF(default_encoding);
    return -1;
}
#endif
147 148
#endif

149 150 151 152
/////////////// TypeConversions ///////////////

/* Type Conversion Functions */

153 154 155 156 157 158 159 160 161 162
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char* c_str) {
    return __Pyx_PyUnicode_FromStringAndSize(c_str, strlen(c_str));
}

static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {
    Py_ssize_t ignore;
    return __Pyx_PyObject_AsStringAndSize(o, &ignore);
}

static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
Robert Bradshaw's avatar
Robert Bradshaw committed
163
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
164
    if (
165
#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
166 167 168 169
            __Pyx_sys_getdefaultencoding_not_ascii && 
#endif
            PyUnicode_Check(o)) {
#if PY_VERSION_HEX < 0x03030000
Robert Bradshaw's avatar
Robert Bradshaw committed
170
        char* defenc_c;
171 172
        // borrowed, cached reference
        PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL);
Robert Bradshaw's avatar
Robert Bradshaw committed
173
        if (!defenc) return NULL;
Robert Bradshaw's avatar
Robert Bradshaw committed
174
        defenc_c = PyBytes_AS_STRING(defenc);
Robert Bradshaw's avatar
Robert Bradshaw committed
175
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
Robert Bradshaw's avatar
Robert Bradshaw committed
176 177 178 179 180 181 182 183 184
        {
            char* end = defenc_c + PyBytes_GET_SIZE(defenc);
            char* c;
            for (c = defenc_c; c < end; c++) {
                if ((unsigned char) (*c) >= 128) {
                    // raise the error
                    PyUnicode_AsASCIIString(o);
                    return NULL;
                }
185 186
            }
        }
Robert Bradshaw's avatar
Robert Bradshaw committed
187
#endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/
188
        *length = PyBytes_GET_SIZE(defenc);
Robert Bradshaw's avatar
Robert Bradshaw committed
189
        return defenc_c;
190
#else /* PY_VERSION_HEX < 0x03030000 */
Robert Bradshaw's avatar
Robert Bradshaw committed
191
        if (PyUnicode_READY(o) == -1) return NULL;
Robert Bradshaw's avatar
Robert Bradshaw committed
192
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
Robert Bradshaw's avatar
Robert Bradshaw committed
193
        if (PyUnicode_IS_ASCII(o)) {
194 195 196 197 198 199 200 201
            // cached for the lifetime of the object
            *length = PyUnicode_GET_DATA_SIZE(o);
            return PyUnicode_AsUTF8(o);
        } else {
            // raise the error
            PyUnicode_AsASCIIString(o);
            return NULL;
        }
Robert Bradshaw's avatar
Robert Bradshaw committed
202 203 204
#else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
        return PyUnicode_AsUTF8AndSize(o, length);
#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
205 206
#endif /* PY_VERSION_HEX < 0x03030000 */
    } else
Robert Bradshaw's avatar
Robert Bradshaw committed
207
#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII  || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */
208

209
#if !CYTHON_COMPILING_IN_PYPY
210 211 212 213 214
    if (PyByteArray_Check(o)) {
        *length = PyByteArray_GET_SIZE(o);
        return PyByteArray_AS_STRING(o);
    } else
#endif
215 216 217
    {
        char* result;
        int r = PyBytes_AsStringAndSize(o, &result, length);
218
        if (unlikely(r < 0)) {
219 220 221 222 223 224 225
            return NULL;
        } else {
            return result;
        }
    }
}

226 227 228 229 230 231 232 233 234 235 236
/* Note: __Pyx_PyObject_IsTrue is written to minimize branching. */
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
   int is_true = x == Py_True;
   if (is_true | (x == Py_False) | (x == Py_None)) return is_true;
   else return PyObject_IsTrue(x);
}

static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) {
  PyNumberMethods *m;
  const char *name = NULL;
  PyObject *res = NULL;
237
#if PY_MAJOR_VERSION < 3
238 239 240 241 242 243
  if (PyInt_Check(x) || PyLong_Check(x))
#else
  if (PyLong_Check(x))
#endif
    return Py_INCREF(x), x;
  m = Py_TYPE(x)->tp_as_number;
244
#if PY_MAJOR_VERSION < 3
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  if (m && m->nb_int) {
    name = "int";
    res = PyNumber_Int(x);
  }
  else if (m && m->nb_long) {
    name = "long";
    res = PyNumber_Long(x);
  }
#else
  if (m && m->nb_int) {
    name = "int";
    res = PyNumber_Long(x);
  }
#endif
  if (res) {
260
#if PY_MAJOR_VERSION < 3
261 262 263 264 265
    if (!PyInt_Check(res) && !PyLong_Check(res)) {
#else
    if (!PyLong_Check(res)) {
#endif
      PyErr_Format(PyExc_TypeError,
266
                   "__%.4s__ returned non-%.4s (type %.200s)",
267 268 269 270 271 272 273 274 275 276 277 278
                   name, name, Py_TYPE(res)->tp_name);
      Py_DECREF(res);
      return NULL;
    }
  }
  else if (!PyErr_Occurred()) {
    PyErr_SetString(PyExc_TypeError,
                    "an integer is required");
  }
  return res;
}

279 280 281 282 283
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
 #if CYTHON_USE_PYLONG_INTERNALS
  #include "longintrepr.h"
 #endif
#endif
284 285
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
  Py_ssize_t ival;
286
  PyObject *x;
287
#if PY_MAJOR_VERSION < 3
288
  if (likely(PyInt_CheckExact(b)))
289 290 291 292 293 294 295 296 297 298 299 300 301 302
      return PyInt_AS_LONG(b);
#endif
  if (likely(PyLong_CheckExact(b))) {
    #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
     #if CYTHON_USE_PYLONG_INTERNALS
       switch (Py_SIZE(b)) {
       case -1: return -(sdigit)((PyLongObject*)b)->ob_digit[0];
       case  0: return 0;
       case  1: return ((PyLongObject*)b)->ob_digit[0];
       }
     #endif
    #endif
    return PyLong_AsSsize_t(b);
  }
303
  x = PyNumber_Index(b);
304 305 306 307 308 309
  if (!x) return -1;
  ival = PyInt_AsSsize_t(x);
  Py_DECREF(x);
  return ival;
}

310
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
311
    return PyInt_FromSize_t(ival);
312 313
}

314

Mark Florisson's avatar
Mark Florisson committed
315 316 317 318 319 320
/////////////// FromPyStructUtility.proto ///////////////
{{struct_type_decl}};
static {{struct_type_decl}} {{funcname}}(PyObject *);

/////////////// FromPyStructUtility ///////////////
static {{struct_type_decl}} {{funcname}}(PyObject * o) {
321
    {{struct_type_decl}} result;
Mark Florisson's avatar
Mark Florisson committed
322 323 324
    PyObject *value = NULL;

    if (!PyMapping_Check(o)) {
325
        PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "a mapping", Py_TYPE(o)->tp_name);
Mark Florisson's avatar
Mark Florisson committed
326 327 328 329 330 331
        goto bad;
    }

    {{for member in var_entries:}}
        {{py:attr = "result." + member.cname}}

332
        value = PyObject_GetItem(o, PYIDENT("{{member.name}}"));
Mark Florisson's avatar
Mark Florisson committed
333
        if (!value) {
334 335
            PyErr_Format(PyExc_ValueError, \
                "No value specified for struct attribute '%.{{max(200, len(member.name))}}s'", "{{member.name}}");
Mark Florisson's avatar
Mark Florisson committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
            goto bad;
        }
        {{attr}} = {{member.type.from_py_function}}(value);
        if ({{member.type.error_condition(attr)}})
            goto bad;

        Py_DECREF(value);
    {{endfor}}

    return result;
bad:
    Py_XDECREF(value);
    return result;
}

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
/////////////// ObjectAsUCS4.proto ///////////////

static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject*);

/////////////// ObjectAsUCS4 ///////////////

static CYTHON_INLINE Py_UCS4 __Pyx_PyObject_AsPy_UCS4(PyObject* x) {
   long ival;
   if (PyUnicode_Check(x)) {
       Py_ssize_t length;
       #if CYTHON_PEP393_ENABLED
       length = PyUnicode_GET_LENGTH(x);
       if (likely(length == 1)) {
           return PyUnicode_READ_CHAR(x, 0);
       }
       #else
       length = PyUnicode_GET_SIZE(x);
       if (likely(length == 1)) {
           return PyUnicode_AS_UNICODE(x)[0];
       }
       #if Py_UNICODE_SIZE == 2
       else if (PyUnicode_GET_SIZE(x) == 2) {
           Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0];
           if (high_val >= 0xD800 && high_val <= 0xDBFF) {
               Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1];
               if (low_val >= 0xDC00 && low_val <= 0xDFFF) {
                   return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1)));
               }
           }
       }
       #endif
       #endif
       PyErr_Format(PyExc_ValueError,
                    "only single character unicode strings can be converted to Py_UCS4, "
                    "got length %" CYTHON_FORMAT_SSIZE_T "d", length);
       return (Py_UCS4)-1;
   }
388
   ival = __Pyx_PyInt_As_long(x);
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
   if (unlikely(ival < 0)) {
       if (!PyErr_Occurred())
           PyErr_SetString(PyExc_OverflowError,
                           "cannot convert negative value to Py_UCS4");
       return (Py_UCS4)-1;
   } else if (unlikely(ival > 1114111)) {
       PyErr_SetString(PyExc_OverflowError,
                       "value too large to convert to Py_UCS4");
       return (Py_UCS4)-1;
   }
   return (Py_UCS4)ival;
}

/////////////// ObjectAsPyUnicode.proto ///////////////

static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject*);

/////////////// ObjectAsPyUnicode ///////////////

static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) {
    long ival;
    #if CYTHON_PEP393_ENABLED
411
    #if Py_UNICODE_SIZE > 2
412 413
    const long maxval = 1114111;
    #else
414 415 416
    const long maxval = 65535;
    #endif
    #else
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    static long maxval = 0;
    #endif
    if (PyUnicode_Check(x)) {
        if (unlikely(__Pyx_PyUnicode_GET_LENGTH(x) != 1)) {
            PyErr_Format(PyExc_ValueError,
                         "only single character unicode strings can be converted to Py_UNICODE, "
                         "got length %" CYTHON_FORMAT_SSIZE_T "d", __Pyx_PyUnicode_GET_LENGTH(x));
            return (Py_UNICODE)-1;
        }
        #if CYTHON_PEP393_ENABLED
        ival = PyUnicode_READ_CHAR(x, 0);
        #else
        return PyUnicode_AS_UNICODE(x)[0];
        #endif
    } else {
        #if !CYTHON_PEP393_ENABLED
        if (unlikely(!maxval))
            maxval = (long)PyUnicode_GetMax();
        #endif
436
        ival = __Pyx_PyInt_As_long(x);
437 438 439 440 441 442 443 444 445 446 447 448 449
    }
    if (unlikely(ival < 0)) {
        if (!PyErr_Occurred())
            PyErr_SetString(PyExc_OverflowError,
                            "cannot convert negative value to Py_UNICODE");
        return (Py_UNICODE)-1;
    } else if (unlikely(ival > maxval)) {
        PyErr_SetString(PyExc_OverflowError,
                        "value too large to convert to Py_UNICODE");
        return (Py_UNICODE)-1;
    }
    return (Py_UNICODE)ival;
}
450

Robert Bradshaw's avatar
Robert Bradshaw committed
451

452 453
/////////////// CIntToPy.proto ///////////////

Robert Bradshaw's avatar
Robert Bradshaw committed
454 455
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value);

456 457
/////////////// CIntToPy ///////////////

Robert Bradshaw's avatar
Robert Bradshaw committed
458 459 460 461
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
    const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (is_unsigned) {
Stefan Behnel's avatar
Stefan Behnel committed
462 463
        if (sizeof({{TYPE}}) < sizeof(long)) {
            return PyInt_FromLong((long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
464
        } else if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
Stefan Behnel's avatar
Stefan Behnel committed
465
            return PyLong_FromUnsignedLong((unsigned long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
466
        } else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) {
467
            return PyLong_FromUnsignedLongLong((unsigned long long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
468 469 470
        }
    } else {
        if (sizeof({{TYPE}}) <= sizeof(long)) {
Stefan Behnel's avatar
Stefan Behnel committed
471
            return PyInt_FromLong((long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
472
        } else if (sizeof({{TYPE}}) <= sizeof(long long)) {
473
            return PyLong_FromLongLong((long long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
474 475 476 477 478 479 480 481 482 483 484
        }
    }
    {
        int one = 1; int little = (int)*(unsigned char *)&one;
        unsigned char *bytes = (unsigned char *)&value;
        return _PyLong_FromByteArray(bytes, sizeof({{TYPE}}),
                                     little, !is_unsigned);
    }
}


485 486
/////////////// CIntFromPyVerify ///////////////

Robert Bradshaw's avatar
Robert Bradshaw committed
487 488 489 490 491
#define __PYX_VERIFY_RETURN_INT(target_type, func_type, func)             \
    {                                                                     \
        func_type value = func(x);                                        \
        if (sizeof(target_type) < sizeof(func_type)) {                    \
            if (unlikely(value != (func_type) (target_type) value)) {     \
492
                func_type zero = 0;                                       \
Robert Bradshaw's avatar
Robert Bradshaw committed
493 494 495 496 497 498 499 500
                PyErr_SetString(PyExc_OverflowError,                      \
                    (is_unsigned && unlikely(value < zero)) ?             \
                    "can't convert negative value to " #target_type :     \
                    "value too large to convert to " #target_type);       \
                return (target_type) -1;                                  \
            }                                                             \
        }                                                                 \
        return (target_type) value;                                       \
501 502
    }

Robert Bradshaw's avatar
Robert Bradshaw committed
503

504 505 506 507 508
/////////////// CIntFromPy.proto ///////////////

static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *);

/////////////// CIntFromPy ///////////////
509
//@requires: CIntFromPyVerify
510 511

#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
512 513 514
 #if CYTHON_USE_PYLONG_INTERNALS
  #include "longintrepr.h"
 #endif
515 516 517 518 519 520 521
#endif
static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
    const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
    if (likely(PyInt_Check(x))) {
        if (sizeof({{TYPE}}) < sizeof(long)) {
522
            __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyInt_AS_LONG)
523
        } else {
524
            long val = PyInt_AS_LONG(x);
525 526 527 528 529
            if (is_unsigned && unlikely(val < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to {{TYPE}}");
                return ({{TYPE}}) -1;
            }
530
            return ({{TYPE}}) val;
531 532 533 534 535 536
        }
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
537
 #if CYTHON_USE_PYLONG_INTERNALS
538 539 540
            if (sizeof(digit) <= sizeof({{TYPE}})) {
                switch (Py_SIZE(x)) {
                    case  0: return 0;
541
                    case  1: return ({{TYPE}}) ((PyLongObject*)x)->ob_digit[0];
542 543
                }
            }
544
 #endif
545 546 547 548 549 550 551
#endif
            if (unlikely(Py_SIZE(x) < 0)) {
                PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative value to {{TYPE}}");
                return ({{TYPE}}) -1;
            }
            if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
552
                __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, PyLong_AsUnsignedLong)
553
            } else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) {
554
                __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long long, PyLong_AsUnsignedLongLong)
555 556 557
            }
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
558
 #if CYTHON_USE_PYLONG_INTERNALS
Stefan Behnel's avatar
Stefan Behnel committed
559
            if (sizeof(digit) <= sizeof({{TYPE}})) {
560 561 562 563 564 565
                switch (Py_SIZE(x)) {
                    case  0: return 0;
                    case  1: return +({{TYPE}}) ((PyLongObject*)x)->ob_digit[0];
                    case -1: return -({{TYPE}}) ((PyLongObject*)x)->ob_digit[0];
                }
            }
566
 #endif
567 568
#endif
            if (sizeof({{TYPE}}) <= sizeof(long)) {
569
                __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyLong_AsLong)
570
            } else if (sizeof({{TYPE}}) <= sizeof(long long)) {
571
                __PYX_VERIFY_RETURN_INT({{TYPE}}, long long, PyLong_AsLongLong)
572 573
            }
        }
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
        {
#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
            PyErr_SetString(PyExc_RuntimeError,
                            "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
#else
            {{TYPE}} val;
            PyObject *v = __Pyx_PyNumber_Int(x);
 #if PY_MAJOR_VERSION < 3
            if (likely(v) && !PyLong_Check(v)) {
                PyObject *tmp = v;
                v = PyNumber_Long(tmp);
                Py_DECREF(tmp);
            }
 #endif
            if (likely(v)) {
                int one = 1; int is_little = (int)*(unsigned char *)&one;
                unsigned char *bytes = (unsigned char *)&val;
                int ret = _PyLong_AsByteArray((PyLongObject *)v,
                                              bytes, sizeof(val),
                                              is_little, !is_unsigned);
                Py_DECREF(v);
                if (likely(!ret))
                    return val;
            }
#endif
            return ({{TYPE}}) -1;
        }
601 602 603 604 605 606 607 608 609 610
    } else {
        {{TYPE}} val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return ({{TYPE}}) -1;
        val = {{FROM_PY_FUNCTION}}(tmp);
        Py_DECREF(tmp);
        return val;
    }
}