本文整理汇总了C++中PyLong_CheckExact函数的典型用法代码示例。如果您正苦于以下问题:C++ PyLong_CheckExact函数的具体用法?C++ PyLong_CheckExact怎么用?C++ PyLong_CheckExact使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyLong_CheckExact函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
}
示例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;
}
示例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)) {
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
}
示例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);
}
}
示例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;
}