• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ PyLong_CheckExact函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中PyLong_CheckExact函数的典型用法代码示例。如果您正苦于以下问题:C++ PyLong_CheckExact函数的具体用法?C++ PyLong_CheckExact怎么用?C++ PyLong_CheckExact使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了PyLong_CheckExact函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: em_list_setitem

/* Insert item in external memory list.
 *
 * XXX: Support slice objects and negative indices?
 */
static int em_list_setitem(em_list_t *self, PyObject *key, PyObject *value)
{
    Py_ssize_t index;

    int ret = -1;

    /* Python 3 supports only long integers. */
#if PY_MAJOR_VERSION < 3
    if(PyInt_CheckExact(key))
    {
        index = PyInt_AsSsize_t(key);
        ret = em_list_setitem_safe(self, index, value);
    }
    else if(PyLong_CheckExact(key))
#else
    if(PyLong_CheckExact(key))
#endif
    {
        index = PyLong_AsSsize_t(key);
        ret = em_list_setitem_safe(self, index, value);
    }
    else
        PyErr_SetString(PyExc_TypeError, "Invalid index type");

    return ret;
}
开发者ID:huku-,项目名称:pyrsistence,代码行数:30,代码来源:em_list.c


示例2: PyInt_AsSsize_t

/* Retrieve item from external memory list.
 *
 * XXX: Support slice objects and negative indices?
 */
static PyObject *em_list_getitem(em_list_t *self, PyObject *key)
{
    Py_ssize_t index;

    PyObject *r = NULL;

    /* Python 3 supports only long integers. */
#if PY_MAJOR_VERSION < 3
    if(PyInt_CheckExact(key))
    {
        index = PyInt_AsSsize_t(key);
        r = em_list_getitem_internal(self, index);
    }
    else if(PyLong_CheckExact(key))
#else
    if(PyLong_CheckExact(key))
#endif
    {
        index = PyLong_AsSsize_t(key);
        r = em_list_getitem_internal(self, index);
    }
    else
        PyErr_SetString(PyExc_TypeError, "Invalid key object type");

    return r;
}
开发者ID:huku-,项目名称:pyrsistence,代码行数:30,代码来源:em_list.c


示例3: BINARY_OPERATION_ADD_LONG_LONG_INPLACE

bool BINARY_OPERATION_ADD_LONG_LONG_INPLACE(PyObject **operand1, PyObject *operand2) {
    assert(operand1);
    CHECK_OBJECT(*operand1);
    CHECK_OBJECT(operand2);
    assert(PyLong_CheckExact(*operand1));
    assert(PyLong_CheckExact(operand2));

    // TODO: Consider adding this shortcut, we might often be able to use
    // existing values at least in case of smaller right hand side, but it
    // may equally often not work out and not be worth it. CPython doesn't
    // try it.
#if 0
    // Adding floats to a new float could get special code too.
    if (Py_REFCNT(*operand1) == 1) {
        return LONG_ADD_INCREMENTAL(operand1, operand2);
    }
#endif

    PyObject *result = PyNumber_InPlaceAdd(*operand1, operand2);

    if (unlikely(result == NULL)) {
        return false;
    }

    // We got an object handed, that we have to release.
    Py_DECREF(*operand1);

    // That's our return value then. As we use a dedicated variable, it's
    // OK that way.
    *operand1 = result;

    return true;
}
开发者ID:kayhayen,项目名称:Nuitka,代码行数:33,代码来源:HelpersOperationBinaryInplaceAdd.c


示例4: _set_int

static int
_set_int(const char *name, int *target, PyObject *src, int dflt)
{
    if (src == NULL)
        *target = dflt;
    else {
        long value;
        if (!PyLong_CheckExact(src)) {
            PyErr_Format(PyExc_TypeError,
                         "\"%s\" must be an integer", name);
            return -1;
        }
        value = PyLong_AsLong(src);
        if (value == -1 && PyErr_Occurred())
            return -1;
#if SIZEOF_LONG > SIZEOF_INT
        if (value > INT_MAX || value < INT_MIN) {
            PyErr_Format(PyExc_ValueError,
                         "integer out of range for \"%s\"", name);
            return -1;
        }
#endif
        *target = (int)value;
    }
    return 0;
}
开发者ID:Naddiseo,项目名称:cpython,代码行数:26,代码来源:_csv.c


示例5: __Pyx_PyInt_AsUnsignedLongLong

static INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject* x) {
#if PY_VERSION_HEX < 0x03000000
    if (likely(PyInt_CheckExact(x) || PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        if (unlikely(val < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to unsigned PY_LONG_LONG");
            return (unsigned PY_LONG_LONG)-1;
        }
        return (unsigned PY_LONG_LONG)val;
    } else
#endif
    if (likely(PyLong_CheckExact(x) || PyLong_Check(x))) {
        if (unlikely(Py_SIZE(x) < 0)) {
            PyErr_SetString(PyExc_OverflowError,
                            "can't convert negative value to unsigned PY_LONG_LONG");
            return (unsigned PY_LONG_LONG)-1;
        }
        return PyLong_AsUnsignedLongLong(x);
    } else {
        unsigned PY_LONG_LONG val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (unsigned PY_LONG_LONG)-1;
        val = __Pyx_PyInt_AsUnsignedLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}
开发者ID:Mouse-Imaging-Centre,项目名称:generate_deformation_fields,代码行数:28,代码来源:setup.c


示例6: range_index

static PyObject *
range_index(rangeobject *r, PyObject *ob)
{
    int contains;

    if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) {
        Py_ssize_t index;
        index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX);
        if (index == -1)
            return NULL;
        return PyLong_FromSsize_t(index);
    }

    contains = range_contains_long(r, ob);
    if (contains == -1)
        return NULL;

    if (contains) {
        PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
        if (tmp == NULL)
            return NULL;
        /* idx = (ob - r.start) // r.step */
        idx = PyNumber_FloorDivide(tmp, r->step);
        Py_DECREF(tmp);
        return idx;
    }

    /* object is not in the range */
    PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
    return NULL;
}
开发者ID:PiJoules,项目名称:cpython-modified,代码行数:31,代码来源:rangeobject.c


示例7: write_element_to_buffer

/* Write a single value to the buffer (also write it's type_byte, for which
 * space has already been reserved.
 *
 * returns 0 on failure */
static int write_element_to_buffer(bson_buffer* buffer, int type_byte, PyObject* value, unsigned char check_keys) {
    /* TODO this isn't quite the same as the Python version:
     * here we check for type equivalence, not isinstance in some
     * places. */
    if (PyInt_CheckExact(value) || PyLong_CheckExact(value)) {
        const long long_value = PyInt_AsLong(value);
        const int int_value = (int)long_value;
        if (PyErr_Occurred() || long_value != int_value) { /* Overflow */
            long long long_long_value;
            PyErr_Clear();
            long_long_value = PyLong_AsLongLong(value);
            if (PyErr_Occurred()) { /* Overflow AGAIN */
                PyErr_SetString(PyExc_OverflowError,
                                "MongoDB can only handle up to 8-byte ints");
                return 0;
            }
            *(buffer->buffer + type_byte) = 0x12;
            return buffer_write_bytes(buffer, (const char*)&long_long_value, 8);
        }
        *(buffer->buffer + type_byte) = 0x10;
        return buffer_write_bytes(buffer, (const char*)&int_value, 4);
    } else if (PyBool_Check(value)) {
        const long bool = PyInt_AsLong(value);
        const char c = bool ? 0x01 : 0x00;
        *(buffer->buffer + type_byte) = 0x08;
        return buffer_write_bytes(buffer, &c, 1);
    } else if (PyFloat_CheckExact(value)) {
开发者ID:clouddb,项目名称:idb.py,代码行数:31,代码来源:_cbsonmodule.c


示例8: range_contains

static int
range_contains(rangeobject *r, PyObject *ob) {
    if (PyLong_CheckExact(ob) || PyBool_Check(ob))
        return range_contains_long(r, ob);

    return (int)_PySequence_IterSearch((PyObject*)r, ob,
                                       PY_ITERSEARCH_CONTAINS);
}
开发者ID:pogigroo,项目名称:py3k-__format__,代码行数:8,代码来源:rangeobject.c


示例9: GMPy_MPZ_IAdd_Slot

static PyObject *
GMPy_MPZ_IAdd_Slot(PyObject *self, PyObject *other)
{
    MPZ_Object *result = NULL;

    if (CHECK_MPZANY(other)) {
        if ((result = GMPy_MPZ_New(NULL))) {
            mpz_add(result->z, MPZ(self), MPZ(other));
        }
        return (PyObject*)result;
    }

    if (PyLong_CheckExact(other)) {
        if ((result = GMPy_MPZ_New(NULL))) {
            switch (Py_SIZE((PyLongObject*)other)) {
            case -1:
                mpz_sub_ui(result->z, MPZ(self), ((PyLongObject*)other)->ob_digit[0]);
                return (PyObject*)result;
            case 0:
            case 1:
                mpz_add_ui(result->z, MPZ(self), ((PyLongObject*)other)->ob_digit[0]);
                return (PyObject*)result;
            default:
                break;
            }
        }
        else {
            return NULL;
        }
    }

    if (PyIntOrLong_Check(other)) {
        int error;
        long temp = GMPy_Integer_AsLongAndError(other, &error);

        if ((result = GMPy_MPZ_New(NULL))) {
            if (!error) {
                if (temp >= 0) {
                    mpz_add_ui(result->z, MPZ(self), temp);
                }
                else {
                    mpz_sub_ui(result->z, MPZ(self), -temp);
                }
            }
            else {
                mpz_t tempz;
                mpz_inoc(tempz);
                mpz_set_PyIntOrLong(tempz, other);
                mpz_add(result->z, MPZ(self), tempz);
                mpz_cloc(tempz);
            }
        }
        return (PyObject*)result;
    }

    Py_RETURN_NOTIMPLEMENTED;
}
开发者ID:martingkelly,项目名称:gmpy,代码行数:57,代码来源:gmpy2_mpz_inplace.c


示例10: pint_getquoted

static PyObject *
pint_getquoted(pintObject *self, PyObject *args)
{
    PyObject *res = NULL;

    /* Convert subclass to int to handle IntEnum and other subclasses
     * whose str() is not the number. */
    if (PyLong_CheckExact(self->wrapped)
#if PY_MAJOR_VERSION < 2
        || PyInt_CheckExact(self->wrapped)
#endif
       ) {
        res = PyObject_Str(self->wrapped);
    } else {
        PyObject *tmp;
        if (!(tmp = PyObject_CallFunctionObjArgs(
                (PyObject *)&PyLong_Type, self->wrapped, NULL))) {
            goto exit;
        }
        res = PyObject_Str(tmp);
        Py_DECREF(tmp);
    }

    if (!res) {
        goto exit;
    }

#if PY_MAJOR_VERSION > 2
    /* unicode to bytes in Py3 */
    {
        PyObject *tmp = PyUnicode_AsUTF8String(res);
        Py_DECREF(res);
        if (!(res = tmp)) {
            goto exit;
        }
    }
#endif

    if ('-' == Bytes_AS_STRING(res)[0]) {
        /* Prepend a space in front of negative numbers (ticket #57) */
        PyObject *tmp;
        if (!(tmp = Bytes_FromString(" "))) {
            Py_DECREF(res);
            res = NULL;
            goto exit;
        }
        Bytes_ConcatAndDel(&tmp, res);
        if (!(res = tmp)) {
            goto exit;
        }
    }

exit:
    return res;
}
开发者ID:dvarrazzo,项目名称:psycopg,代码行数:55,代码来源:adapter_pint.c


示例11:

    static void *convertible(::PyObject *obj_ptr)
    {
        if (!obj_ptr
            || (
#if PY_MAJOR_VERSION < 3
                   !PyInt_CheckExact(obj_ptr) &&
#endif
                   !PyLong_CheckExact(obj_ptr))) {
            return nullptr;
        }
        return obj_ptr;
    }
开发者ID:bluescarni,项目名称:piranha,代码行数:12,代码来源:python_converters.hpp


示例12: range_index

static PyObject *
range_index(rangeobject *r, PyObject *ob)
{
    PyObject *idx, *tmp;
    int contains;
    PyObject *format_tuple, *err_string;
    static PyObject *err_format = NULL;

    if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) {
        Py_ssize_t index;
        index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX);
        if (index == -1)
            return NULL;
        return PyLong_FromSsize_t(index);
    }

    contains = range_contains_long(r, ob);
    if (contains == -1)
        return NULL;

    if (!contains)
        goto value_error;

    tmp = PyNumber_Subtract(ob, r->start);
    if (tmp == NULL)
        return NULL;

    /* idx = (ob - r.start) // r.step */
    idx = PyNumber_FloorDivide(tmp, r->step);
    Py_DECREF(tmp);
    return idx;

value_error:

    /* object is not in the range */
    if (err_format == NULL) {
        err_format = PyUnicode_FromString("%r is not in range");
        if (err_format == NULL)
            return NULL;
    }
    format_tuple = PyTuple_Pack(1, ob);
    if (format_tuple == NULL)
        return NULL;
    err_string = PyUnicode_Format(err_format, format_tuple);
    Py_DECREF(format_tuple);
    if (err_string == NULL)
        return NULL;
    PyErr_SetObject(PyExc_ValueError, err_string);
    Py_DECREF(err_string);
    return NULL;
}
开发者ID:pogigroo,项目名称:py3k-__format__,代码行数:51,代码来源:rangeobject.c


示例13: __pyx_PyInt_AsLongLong

static INLINE PY_LONG_LONG __pyx_PyInt_AsLongLong(PyObject* x) {
    if (PyInt_CheckExact(x)) {
        return PyInt_AS_LONG(x);
    }
    else if (PyLong_CheckExact(x)) {
        return PyLong_AsLongLong(x);
    }
    else {
        PY_LONG_LONG val;
        PyObject* tmp = PyNumber_Int(x); if (!tmp) return (PY_LONG_LONG)-1;
        val = __pyx_PyInt_AsLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}
开发者ID:liguow,项目名称:everseq,代码行数:15,代码来源:_core.c


示例14: range_count

static PyObject *
range_count(rangeobject *r, PyObject *ob)
{
    if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
        int result = range_contains_long(r, ob);
        if (result == -1)
            return NULL;
        return PyLong_FromLong(result);
    } else {
        Py_ssize_t count;
        count = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_COUNT);
        if (count == -1)
            return NULL;
        return PyLong_FromSsize_t(count);
    }
}
开发者ID:Victor-Savu,项目名称:cpython,代码行数:16,代码来源:rangeobject.c


示例15: JavaRandom_next

static PyObject* JavaRandom_next(JavaRandomObject* self, PyObject* arg1)
{
    if (!PyLong_CheckExact(arg1))
    {
        PyErr_SetString(PyExc_TypeError, "The first attribute value must be an int");
        return NULL;
    }

    int bits = PyLong_AsLong(arg1);

    long long int next_rnd_bits = java_random_next(&self->rnd, bits);

    PyObject* result = PyLong_FromLongLong(next_rnd_bits);

    return result;
}
开发者ID:MBradbury,项目名称:python_java_random,代码行数:16,代码来源:java_random_module.c


示例16: PyArray_GetAttrString_SuppressException

/*
 * PyArray_GetAttrString_SuppressException:
 *
 * Stripped down version of PyObject_GetAttrString,
 * avoids lookups for None, tuple, and List objects,
 * and doesn't create a PyErr since this code ignores it.
 *
 * This can be much faster then PyObject_GetAttrString where
 * exceptions are not used by caller.
 *
 * 'obj' is the object to search for attribute.
 *
 * 'name' is the attribute to search for.
 *
 * Returns attribute value on success, 0 on failure.
 */
PyObject *
PyArray_GetAttrString_SuppressException(PyObject *obj, char *name)
{
    PyTypeObject *tp = Py_TYPE(obj);
    PyObject *res = (PyObject *)NULL;

    /* We do not need to check for special attributes on trivial types */
    if (obj == Py_None ||
            /* Basic number types */
#if !defined(NPY_PY3K)
            PyInt_CheckExact(obj) ||
#endif
            PyLong_CheckExact(obj) ||
            PyFloat_CheckExact(obj) ||
            /* Basic sequence types */
            PyList_CheckExact(obj) ||
            PyTuple_CheckExact(obj)) {
        return NULL;
    }

    /* Attribute referenced by (char *)name */
    if (tp->tp_getattr != NULL) {
        res = (*tp->tp_getattr)(obj, name);
        if (res == NULL) {
            PyErr_Clear();
        }
    }
    /* Attribute referenced by (PyObject *)name */
    else if (tp->tp_getattro != NULL) {
#if defined(NPY_PY3K)
        PyObject *w = PyUnicode_InternFromString(name);
#else
        PyObject *w = PyString_InternFromString(name);
#endif
        if (w == NULL) {
            return (PyObject *)NULL;
        }
        res = (*tp->tp_getattro)(obj, w);
        Py_DECREF(w);
        if (res == NULL) {
            PyErr_Clear();
        }
    }
    return res;
}
开发者ID:JStech,项目名称:numpy,代码行数:61,代码来源:common.c


示例17: python_compress

static PyObject *
python_compress(PyObject *self, PyObject *args) {
    char *input, *output;
    Py_ssize_t inlen;
    PyObject *pyoutlen = Py_None;
    long outlen;
    PyObject *result;

    if (!PyArg_ParseTuple(args, "s#|O", &input, &inlen, &pyoutlen))
        return NULL;

    if (pyoutlen == Py_None)
        outlen = inlen - 1;
    else if (PyInt_CheckExact(pyoutlen))
        outlen = PyInt_AsLong(pyoutlen);
    else if (PyLong_CheckExact(pyoutlen))
        outlen = PyLong_AsLong(pyoutlen);
    else {
        PyErr_SetString(PyExc_TypeError, "max_len must be an integer");
        return NULL;
    }

    if (inlen == 1) outlen++; /* work around for what looks like a liblzf bug */
    if (outlen <= 0) {
        PyErr_SetString(PyExc_ValueError, "max_len must be > 0");
        return NULL;
    }

    output = (char *)malloc(outlen + 1);
    if (output == NULL) {
        PyErr_SetString(PyExc_MemoryError, "out of memory");
        return NULL;
    }
    outlen = lzf_compress(input, inlen, output, outlen + 1);

    if (outlen)
        result = PYBYTES_FSAS(output, outlen);
    else {
        Py_XINCREF(Py_None);
        result = Py_None;
    }
    free(output);

    return result;
}
开发者ID:gillsoft,项目名称:python-lzf,代码行数:45,代码来源:lzf_module.c


示例18: __Pyx_PyInt_AsSignedLongLong

static INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject* x) {
#if PY_VERSION_HEX < 0x03000000
    if (likely(PyInt_CheckExact(x) || PyInt_Check(x))) {
        long val = PyInt_AS_LONG(x);
        return (signed PY_LONG_LONG)val;
    } else
#endif
    if (likely(PyLong_CheckExact(x) || PyLong_Check(x))) {
        return PyLong_AsLongLong(x);
    } else {
        signed PY_LONG_LONG val;
        PyObject *tmp = __Pyx_PyNumber_Int(x);
        if (!tmp) return (signed PY_LONG_LONG)-1;
        val = __Pyx_PyInt_AsSignedLongLong(tmp);
        Py_DECREF(tmp);
        return val;
    }
}
开发者ID:Mouse-Imaging-Centre,项目名称:generate_deformation_fields,代码行数:18,代码来源:setup.c


示例19: encode_object

void encode_object(PyObject *arg) {
	Py_ssize_t size, i;
	char *p;

	if (PyString_CheckExact(arg)) {
		uint8_t *s, *sEnd;
		*d++ = '"';
		ENCODE_STR(arg, s, sEnd)
		*d++ = '"';
	} else if (PyUnicode_CheckExact(arg)) {
		*d++ = '"';
		Py_UNICODE *u, *uEnd;
		ENCODE_UNICODE(arg, u, uEnd)
		*d++ = '"';
	} else if (PyInt_CheckExact(arg) || PyLong_CheckExact(arg)) {
		i = PyInt_AsSsize_t(arg);
		uint64_t val = i < 0 ? *d++ = '-', (uint64_t)(~i + 1) : i;
		ITOA(val)
	} else if (PyList_CheckExact(arg)) {
开发者ID:S-YOU,项目名称:myson,代码行数:19,代码来源:myson.c


示例20: escape

static PyObject*
escape(PyObject *self, PyObject *text)
{
	PyObject *s = NULL, *rv = NULL, *html;

	/* we don't have to escape integers, bools or floats */
	if (PyLong_CheckExact(text) ||
#if PY_MAJOR_VERSION < 3
	    PyInt_CheckExact(text) ||
#endif
	    PyFloat_CheckExact(text) || PyBool_Check(text) ||
	    text == Py_None)
		return PyObject_CallFunctionObjArgs(markup, text, NULL);

	/* if the object has an __html__ method that performs the escaping */
	html = PyObject_GetAttrString(text, "__html__");
	if (html) {
		rv = PyObject_CallObject(html, NULL);
		Py_DECREF(html);
		return rv;
	}

	/* otherwise make the object unicode if it isn't, then escape */
	PyErr_Clear();
	if (!PyUnicode_Check(text)) {
#if PY_MAJOR_VERSION < 3
		PyObject *unicode = PyObject_Unicode(text);
#else
		PyObject *unicode = PyObject_Str(text);
#endif
		if (!unicode)
			return NULL;
		s = escape_unicode((PyUnicodeObject*)unicode);
		Py_DECREF(unicode);
	}
	else
		s = escape_unicode((PyUnicodeObject*)text);

	/* convert the unicode string into a markup object. */
	rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL);
	Py_DECREF(s);
	return rv;
}
开发者ID:10sr,项目名称:hue,代码行数:43,代码来源:_speedups.c



注:本文中的PyLong_CheckExact函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ PyLong_FromLong函数代码示例发布时间:2022-05-30
下一篇:
C++ PyLong_Check函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap