Exemplo n.º 1
0
Arquivo: util.c Projeto: Orav/kbengine
sqlite_int64
_pysqlite_long_as_int64(PyObject * py_val)
{
    int overflow;
#ifdef HAVE_LONG_LONG
    PY_LONG_LONG value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
#else
    long value = PyLong_AsLongAndOverflow(py_val, &overflow);
#endif
    if (value == -1 && PyErr_Occurred())
        return -1;
    if (!overflow) {
#ifdef HAVE_LONG_LONG
# if SIZEOF_LONG_LONG > 8
        if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL)
# endif
#else
# if SIZEOF_LONG > 8
        if (-0x8000000000000000L <= value && value <= 0x7FFFFFFFFFFFFFFFL)
# endif
#endif
            return value;
    }
    else if (sizeof(value) < sizeof(sqlite_int64)) {
        sqlite_int64 int64val;
        if (_PyLong_AsByteArray((PyLongObject *)py_val,
                                (unsigned char *)&int64val, sizeof(int64val),
                                IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) {
            return int64val;
        }
    }
    PyErr_SetString(PyExc_OverflowError,
                    "Python int too large to convert to SQLite INTEGER");
    return -1;
}
Exemplo n.º 2
0
static int is_listlike_dict(PyObject* dct) {
    int64_t min = INT64_MAX;
    int64_t max = INT64_MIN;
    size_t count = 0;

    PyObject *key, *value;
    Py_ssize_t pos = 0;
    while (PyDict_Next(dct, &pos, &key, &value)) {
        if (!PyLong_Check(key)) {
            return 0;
        }

        int overflow = 0;
        int64_t cur = PyLong_AsLongLongAndOverflow(key, &overflow);
        if (overflow) {
            return 0;
        }
        if (cur < min) {
            min = cur;
        }
        if (cur > max) {
            max = cur;
        }
        ++count;
    }

    // Lua lists are 1-based.
    return min == 1 && max == count;
}
Exemplo n.º 3
0
static int
longlong_convert(PyObject *ob, PY_LONG_LONG *value)
{
#ifndef PY3K
    if (PyInt_Check(ob))
    {
        (*value) = (PY_LONG_LONG)PyInt_AS_LONG(ob);
        return 1;
    }
#endif

    if (!PyLong_Check(ob))
    {
        PyErr_SetString(PyExc_TypeError, "expected integer key");
        return 0;
    }
    else
    {
        PY_LONG_LONG val;
#if PY_VERSION_HEX < 0x02070000
        /* check magnitude */
        val = PyLong_AsLongLong(ob);

        if (val == -1 && PyErr_Occurred())
            goto overflow;
#else
        int overflow;
        val = PyLong_AsLongLongAndOverflow(ob, &overflow);
        if (overflow)
            goto overflow;
#endif
        (*value) = val;
        return 1;
    }
overflow:
    PyErr_SetString(PyExc_ValueError, "long integer out of range");
    return 0;
}