TypeConversion.c 20.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
#define __Pyx_PyBytes_FromString        PyBytes_FromString
#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
23
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*);
24

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
#define __Pyx_PyObject_AsUString(s)    ((unsigned char*) __Pyx_PyObject_AsString(s))
35 36 37 38 39
#define __Pyx_PyObject_FromCString(s)  __Pyx_PyObject_FromString((const char*)s)
#define __Pyx_PyBytes_FromCString(s)   __Pyx_PyBytes_FromString((const char*)s)
#define __Pyx_PyByteArray_FromCString(s)   __Pyx_PyByteArray_FromString((const char*)s)
#define __Pyx_PyStr_FromCString(s)     __Pyx_PyStr_FromString((const char*)s)
#define __Pyx_PyUnicode_FromCString(s) __Pyx_PyUnicode_FromString((const char*)s)
40

41 42 43 44 45
#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++) ;
46
    return (size_t)(u_end - u - 1);
47
}
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
    PyObject* sys;
75
    PyObject* default_encoding = NULL;
Robert Bradshaw's avatar
Robert Bradshaw committed
76 77
    PyObject* ascii_chars_u = NULL;
    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 83 84 85 86
    Py_DECREF(sys);
    if (!default_encoding) goto bad;
    default_encoding_c = PyBytes_AsString(default_encoding);
    if (!default_encoding_c) goto bad;
    if (strcmp(default_encoding_c, "ascii") == 0) {
87 88
        __Pyx_sys_getdefaultencoding_not_ascii = 0;
    } else {
Robert Bradshaw's avatar
Robert Bradshaw committed
89 90 91 92 93 94 95
        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);
96
        if (!ascii_chars_u) goto bad;
Robert Bradshaw's avatar
Robert Bradshaw committed
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) {
Robert Bradshaw's avatar
Robert Bradshaw committed
99 100
            PyErr_Format(
                PyExc_ValueError,
101
                "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
102 103
                default_encoding_c);
            goto bad;
104
        }
105 106
        Py_DECREF(ascii_chars_u);
        Py_DECREF(ascii_chars_b);
107
    }
108
    Py_DECREF(default_encoding);
109 110 111
    return 0;
bad:
    Py_XDECREF(default_encoding);
Robert Bradshaw's avatar
Robert Bradshaw committed
112 113
    Py_XDECREF(ascii_chars_u);
    Py_XDECREF(ascii_chars_b);
114 115
    return -1;
}
Robert Bradshaw's avatar
Robert Bradshaw committed
116 117
#endif 

118 119 120 121 122 123 124
#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
125 126
#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
static char* __PYX_DEFAULT_STRING_ENCODING;
127

128
static int __Pyx_init_sys_getdefaultencoding_params(void) {
129
    PyObject* sys;
Robert Bradshaw's avatar
Robert Bradshaw committed
130 131
    PyObject* default_encoding = NULL;
    char* default_encoding_c;
132

Robert Bradshaw's avatar
Robert Bradshaw committed
133
    sys = PyImport_ImportModule("sys");
Stefan Behnel's avatar
Stefan Behnel committed
134
    if (!sys) goto bad;
Robert Bradshaw's avatar
Robert Bradshaw committed
135
    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
136
    Py_DECREF(sys);
Stefan Behnel's avatar
Stefan Behnel committed
137
    if (!default_encoding) goto bad;
138 139
    default_encoding_c = PyBytes_AsString(default_encoding);
    if (!default_encoding_c) goto bad;
Robert Bradshaw's avatar
Robert Bradshaw committed
140
    __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c));
Stefan Behnel's avatar
Stefan Behnel committed
141
    if (!__PYX_DEFAULT_STRING_ENCODING) goto bad;
Robert Bradshaw's avatar
Robert Bradshaw committed
142
    strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);
143
    Py_DECREF(default_encoding);
Robert Bradshaw's avatar
Robert Bradshaw committed
144 145 146 147 148 149
    return 0;
bad:
    Py_XDECREF(default_encoding);
    return -1;
}
#endif
150 151
#endif

152
/////////////// TypeConversions ///////////////
153
//@requires: PyLongInternals
154 155 156

/* Type Conversion Functions */

157
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) {
158
    return __Pyx_PyUnicode_FromStringAndSize(c_str, (Py_ssize_t)strlen(c_str));
159 160 161 162 163 164 165 166
}

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

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

230 231 232 233 234 235 236 237 238 239 240
/* 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;
241
#if PY_MAJOR_VERSION < 3
242 243 244 245 246 247
  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;
248
#if PY_MAJOR_VERSION < 3
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  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) {
264
#if PY_MAJOR_VERSION < 3
265 266 267 268 269
    if (!PyInt_Check(res) && !PyLong_Check(res)) {
#else
    if (!PyLong_Check(res)) {
#endif
      PyErr_Format(PyExc_TypeError,
270
                   "__%.4s__ returned non-%.4s (type %.200s)",
271 272 273 274 275 276 277 278 279 280 281 282 283 284
                   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;
}

static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
  Py_ssize_t ival;
285
  PyObject *x;
286
#if PY_MAJOR_VERSION < 3
287
  if (likely(PyInt_CheckExact(b)))
288 289 290 291 292 293 294 295 296 297 298 299 300 301
      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);
  }
302
  x = PyNumber_Index(b);
303 304 305 306 307 308
  if (!x) return -1;
  ival = PyInt_AsSsize_t(x);
  Py_DECREF(x);
  return ival;
}

309
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
310
    return PyInt_FromSize_t(ival);
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
/////////////// 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,
347 348
                    "only single character unicode strings can be converted to Py_UCS4, "
                    "got length %" CYTHON_FORMAT_SSIZE_T "d", length);
349 350
       return (Py_UCS4)-1;
   }
351
   ival = __Pyx_PyInt_As_long(x);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
   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
374
    #if Py_UNICODE_SIZE > 2
375 376
    const long maxval = 1114111;
    #else
377 378 379
    const long maxval = 65535;
    #endif
    #else
380 381 382 383 384
    static long maxval = 0;
    #endif
    if (PyUnicode_Check(x)) {
        if (unlikely(__Pyx_PyUnicode_GET_LENGTH(x) != 1)) {
            PyErr_Format(PyExc_ValueError,
385 386
                         "only single character unicode strings can be converted to Py_UNICODE, "
                         "got length %" CYTHON_FORMAT_SSIZE_T "d", __Pyx_PyUnicode_GET_LENGTH(x));
387 388 389 390 391 392 393 394 395 396 397 398
            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
399
        ival = __Pyx_PyInt_As_long(x);
400 401 402 403 404 405 406 407 408 409 410 411 412
    }
    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;
}
413

Robert Bradshaw's avatar
Robert Bradshaw committed
414

415 416
/////////////// CIntToPy.proto ///////////////

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

419 420
/////////////// CIntToPy ///////////////

Robert Bradshaw's avatar
Robert Bradshaw committed
421 422 423 424
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
425 426
        if (sizeof({{TYPE}}) < sizeof(long)) {
            return PyInt_FromLong((long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
427
        } else if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
Stefan Behnel's avatar
Stefan Behnel committed
428
            return PyLong_FromUnsignedLong((unsigned long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
429
        } else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) {
430
            return PyLong_FromUnsignedLongLong((unsigned long long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
431 432 433
        }
    } else {
        if (sizeof({{TYPE}}) <= sizeof(long)) {
Stefan Behnel's avatar
Stefan Behnel committed
434
            return PyInt_FromLong((long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
435
        } else if (sizeof({{TYPE}}) <= sizeof(long long)) {
436
            return PyLong_FromLongLong((long long) value);
Robert Bradshaw's avatar
Robert Bradshaw committed
437 438 439 440 441 442 443 444 445 446 447
        }
    }
    {
        int one = 1; int little = (int)*(unsigned char *)&one;
        unsigned char *bytes = (unsigned char *)&value;
        return _PyLong_FromByteArray(bytes, sizeof({{TYPE}}),
                                     little, !is_unsigned);
    }
}


448 449
/////////////// CIntFromPyVerify ///////////////

450
// see CIntFromPy
451
#define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value)       \
Robert Bradshaw's avatar
Robert Bradshaw committed
452
    {                                                                     \
453
        func_type value = func_value;                                     \
Robert Bradshaw's avatar
Robert Bradshaw committed
454 455
        if (sizeof(target_type) < sizeof(func_type)) {                    \
            if (unlikely(value != (func_type) (target_type) value)) {     \
456
                func_type zero = 0;                                       \
457 458 459 460
                if (is_unsigned && unlikely(value < zero))                \
                    goto raise_neg_overflow;                              \
                else                                                      \
                    goto raise_overflow;                                  \
Robert Bradshaw's avatar
Robert Bradshaw committed
461 462 463
            }                                                             \
        }                                                                 \
        return (target_type) value;                                       \
464 465
    }

Robert Bradshaw's avatar
Robert Bradshaw committed
466

467 468 469 470 471 472 473 474 475
/////////////// PyLongInternals ///////////////

#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
 #if CYTHON_USE_PYLONG_INTERNALS
  #include "longintrepr.h"
 #endif
#endif


476 477 478 479 480
/////////////// CIntFromPy.proto ///////////////

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

/////////////// CIntFromPy ///////////////
481
//@requires: CIntFromPyVerify
482
//@requires: PyLongInternals
483 484 485 486 487 488 489

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)) {
490
            __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyInt_AS_LONG(x))
491
        } else {
492
            long val = PyInt_AS_LONG(x);
493
            if (is_unsigned && unlikely(val < 0)) {
494
                goto raise_neg_overflow;
495
            }
496
            return ({{TYPE}}) val;
497 498 499 500 501 502
        }
    } else
#endif
    if (likely(PyLong_Check(x))) {
        if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
503
 #if CYTHON_USE_PYLONG_INTERNALS
504 505 506
            switch (Py_SIZE(x)) {
                case  0: return 0;
                case  1: __PYX_VERIFY_RETURN_INT({{TYPE}}, digit, ((PyLongObject*)x)->ob_digit[0]);
507
            }
508
 #endif
509 510
#endif
            if (unlikely(Py_SIZE(x) < 0)) {
511
                goto raise_neg_overflow;
512 513
            }
            if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
514
                __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, PyLong_AsUnsignedLong(x))
515
            } else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) {
516
                __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long long, PyLong_AsUnsignedLongLong(x))
517 518 519
            }
        } else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
520
 #if CYTHON_USE_PYLONG_INTERNALS
521 522 523 524
            switch (Py_SIZE(x)) {
                case  0: return 0;
                case  1: __PYX_VERIFY_RETURN_INT({{TYPE}},  digit, +(((PyLongObject*)x)->ob_digit[0]));
                case -1: __PYX_VERIFY_RETURN_INT({{TYPE}}, sdigit, -(sdigit) ((PyLongObject*)x)->ob_digit[0]);
525
            }
526
 #endif
527 528
#endif
            if (sizeof({{TYPE}}) <= sizeof(long)) {
529
                __PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyLong_AsLong(x))
530
            } else if (sizeof({{TYPE}}) <= sizeof(long long)) {
531
                __PYX_VERIFY_RETURN_INT({{TYPE}}, long long, PyLong_AsLongLong(x))
532 533
            }
        }
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
        {
#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;
        }
561 562 563 564 565 566 567 568 569
    } else {
        {{TYPE}} val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return ({{TYPE}}) -1;
        val = {{FROM_PY_FUNCTION}}(tmp);
        Py_DECREF(tmp);
        return val;
    }

570 571 572 573 574 575 576 577 578 579
raise_overflow:
    PyErr_SetString(PyExc_OverflowError,
        "value too large to convert to {{TYPE}}");
    return ({{TYPE}}) -1;

raise_neg_overflow:
    PyErr_SetString(PyExc_OverflowError,
        "can't convert negative value to {{TYPE}}");
    return ({{TYPE}}) -1;
}