本文整理汇总了C++中PyNumber_Add函数的典型用法代码示例。如果您正苦于以下问题:C++ PyNumber_Add函数的具体用法?C++ PyNumber_Add怎么用?C++ PyNumber_Add使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyNumber_Add函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: longrangeiter_next
static PyObject *
longrangeiter_next(longrangeiterobject *r)
{
PyObject *one, *product, *new_index, *result;
if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1)
return NULL;
one = PyLong_FromLong(1);
if (!one)
return NULL;
new_index = PyNumber_Add(r->index, one);
Py_DECREF(one);
if (!new_index)
return NULL;
product = PyNumber_Multiply(r->index, r->step);
if (!product) {
Py_DECREF(new_index);
return NULL;
}
result = PyNumber_Add(r->start, product);
Py_DECREF(product);
if (result) {
Py_XSETREF(r->index, new_index);
}
else {
Py_DECREF(new_index);
}
return result;
}
示例2: PyNumber_Add
//! Addition
RCP<const Number> PyNumber::add(const Number &other) const {
PyObject *other_p, *result;
if (is_a<PyNumber>(other)) {
other_p = static_cast<const PyNumber &>(other).pyobject_;
result = PyNumber_Add(pyobject_, other_p);
} else {
other_p = pymodule_->to_py_(other.rcp_from_this_cast<const Basic>());
result = PyNumber_Add(pyobject_, other_p);
Py_XDECREF(other_p);
}
return make_rcp<PyNumber>(result, pymodule_);
}
示例3: Proxy__WRAPPED_REPLACE_OR_RETURN_NULL
static PyObject *Proxy_add(PyObject *o1, PyObject *o2)
{
Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
return PyNumber_Add(o1, o2);
}
示例4: longrangeiter_reduce
static PyObject *
longrangeiter_reduce(longrangeiterobject *r)
{
PyObject *product, *stop=NULL;
PyObject *range;
/* create a range object for pickling. Must calculate the "stop" value */
product = PyNumber_Multiply(r->len, r->step);
if (product == NULL)
return NULL;
stop = PyNumber_Add(r->start, product);
Py_DECREF(product);
if (stop == NULL)
return NULL;
Py_INCREF(r->start);
Py_INCREF(r->step);
range = (PyObject*)make_range_object(&PyRange_Type,
r->start, stop, r->step);
if (range == NULL) {
Py_DECREF(r->start);
Py_DECREF(stop);
Py_DECREF(r->step);
return NULL;
}
/* return the result */
return Py_BuildValue("N(N)O", _PyObject_GetBuiltin("iter"), range, r->index);
}
示例5: compute_range_item
static PyObject *
compute_range_item(rangeobject *r, PyObject *arg)
{
int cmp_result;
PyObject *i, *result;
PyObject *zero = PyLong_FromLong(0);
if (zero == NULL)
return NULL;
/* PyLong equivalent to:
* if (arg < 0) {
* i = r->length + arg
* } else {
* i = arg
* }
*/
cmp_result = PyObject_RichCompareBool(arg, zero, Py_LT);
if (cmp_result == -1) {
Py_DECREF(zero);
return NULL;
}
if (cmp_result == 1) {
i = PyNumber_Add(r->length, arg);
if (!i) {
Py_DECREF(zero);
return NULL;
}
} else {
i = arg;
Py_INCREF(i);
}
/* PyLong equivalent to:
* if (i < 0 || i >= r->length) {
* <report index out of bounds>
* }
*/
cmp_result = PyObject_RichCompareBool(i, zero, Py_LT);
Py_DECREF(zero);
if (cmp_result == 0) {
cmp_result = PyObject_RichCompareBool(i, r->length, Py_GE);
}
if (cmp_result == -1) {
Py_DECREF(i);
return NULL;
}
if (cmp_result == 1) {
Py_DECREF(i);
PyErr_SetString(PyExc_IndexError,
"range object index out of range");
return NULL;
}
result = compute_item(r, i);
Py_DECREF(i);
return result;
}
示例6: second_func
static PyObject*
second_func(PyObject *self, PyObject *args)
{
PyObject *a, *b;
if (!PyArg_UnpackTuple(args, "func", 2, 2, &a, &b)) {
return NULL;
}
return PyNumber_Add(a, b);
}
示例7: compute_item
static PyObject *
compute_item(rangeobject *r, PyObject *i)
{
PyObject *incr, *result;
/* PyLong equivalent to:
* return r->start + (i * r->step)
*/
incr = PyNumber_Multiply(i, r->step);
if (!incr)
return NULL;
result = PyNumber_Add(r->start, incr);
Py_DECREF(incr);
return result;
}
示例8: entity_init
entity_init(EntityObject *self, PyObject *documentURI)
{
PyObject *creationIndex, *unparsed_entities;
if (documentURI == NULL || !XmlString_NullCheck(documentURI)) {
PyErr_BadInternalCall();
Py_DECREF(self);
return NULL;
}
creationIndex = PyNumber_Add(creation_counter, counter_inc);
if (creationIndex == NULL) {
Py_DECREF(self);
return NULL;
}
unparsed_entities = PyDict_New();
if (unparsed_entities == NULL) {
Py_DECREF(creationIndex);
Py_DECREF(self);
return NULL;
}
if (documentURI == Py_None) {
documentURI = PyUnicode_FromUnicode(NULL, 0);
if (documentURI == NULL) {
Py_DECREF(creationIndex);
Py_DECREF(unparsed_entities);
Py_DECREF(self);
return NULL;
}
} else {
Py_INCREF(documentURI);
}
self->creationIndex = creationIndex;
self->unparsed_entities = unparsed_entities;
Entity_SET_DOCUMENT_URI(self, documentURI);
Entity_SET_PUBLIC_ID(self, Py_None);
Py_INCREF(Py_None);
Entity_SET_SYSTEM_ID(self, Py_None);
Py_INCREF(Py_None);
/* update creation counter */
Py_INCREF(creationIndex);
Py_DECREF(creation_counter);
creation_counter = creationIndex;
return self;
}
示例9: fibo_iter
static PyObject*
fibo_iter(PyObject* self,PyObject *args){
// the numbers to be added
PyObject *first_num;
PyObject *second_num;
PyObject *tmp_res;
long int fibo_number,i;
if (!PyArg_ParseTuple(args, "l", &fibo_number))
return NULL;
//printf("\nThe value to compute is %ld",fibo_number);
if(fibo_number == 0 || fibo_number == 1)
return Py_BuildValue("l",fibo_number);
//Dont forget to decref ...
first_num = Py_BuildValue("l",0);
second_num = Py_BuildValue("l",1);
//PyObject_Print(first_num, stdout, 0);
//printf("\n\n");
//PyObject_Print(second_num, stdout, 0);
//printf("\n\n");
//computes the fibo iteratively
for (i=1;i<fibo_number;i++){
//we have a new reference
tmp_res = PyNumber_Add(first_num,second_num);
Py_INCREF(second_num);
Py_XDECREF(first_num);
first_num = second_num;
Py_INCREF(tmp_res);
Py_XDECREF(second_num);
second_num = tmp_res;
//give back the reference
Py_XDECREF(tmp_res);
}
Py_XDECREF(first_num);
return second_num;
}
示例10: Py_INCREF
/* Implementation of rowe_1 */
static PyObject *__pyx_f_6rowe_1_function(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_f_6rowe_1_function(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
PyObject *__pyx_v_data = 0;
PyObject *__pyx_v_a;
PyObject *__pyx_r;
PyObject *__pyx_1 = 0;
PyObject *__pyx_2 = 0;
int __pyx_3;
Py_ssize_t __pyx_4;
Py_ssize_t __pyx_5;
static char *__pyx_argnames[] = {"data",0};
if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "O", __pyx_argnames, &__pyx_v_data)) return 0;
Py_INCREF(__pyx_v_data);
__pyx_v_a = Py_None; Py_INCREF(Py_None);
/* "/Local/Projects/D/Pyrex/Source/Tests/Bugs/other/rowe_1.pyx":7 */
__pyx_1 = PyInt_FromLong(4); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; goto __pyx_L1;}
__pyx_2 = PyNumber_Multiply(__pyx_1, __pyx_v_a); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; goto __pyx_L1;}
Py_DECREF(__pyx_1); __pyx_1 = 0;
__pyx_3 = PyInt_AsLong(__pyx_2); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 7; goto __pyx_L1;}
Py_DECREF(__pyx_2); __pyx_2 = 0;
blarg(__pyx_3);
/* "/Local/Projects/D/Pyrex/Source/Tests/Bugs/other/rowe_1.pyx":8 */
__pyx_4 = PyInt_AsSsize_t(__pyx_v_a); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; goto __pyx_L1;}
__pyx_1 = __Pyx_GetName(__pyx_b, __pyx_n_b); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; goto __pyx_L1;}
__pyx_2 = PyNumber_Add(__pyx_v_a, __pyx_1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; goto __pyx_L1;}
Py_DECREF(__pyx_1); __pyx_1 = 0;
__pyx_5 = PyInt_AsSsize_t(__pyx_2); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; goto __pyx_L1;}
Py_DECREF(__pyx_2); __pyx_2 = 0;
__pyx_1 = PySequence_GetSlice(__pyx_v_data, __pyx_4, __pyx_5); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; goto __pyx_L1;}
__pyx_r = __pyx_1;
__pyx_1 = 0;
goto __pyx_L0;
__pyx_r = Py_None; Py_INCREF(Py_None);
goto __pyx_L0;
__pyx_L1:;
Py_XDECREF(__pyx_1);
Py_XDECREF(__pyx_2);
__Pyx_AddTraceback("rowe_1.function");
__pyx_r = 0;
__pyx_L0:;
Py_DECREF(__pyx_v_a);
Py_DECREF(__pyx_v_data);
return __pyx_r;
}
示例11: __pyx_init_filenames
/* Implementation of concatcstrings */
static struct PyMethodDef __pyx_methods[] = {
{0, 0, 0, 0}
};
static void __pyx_init_filenames(void); /*proto*/
PyMODINIT_FUNC initconcatcstrings(void); /*proto*/
PyMODINIT_FUNC initconcatcstrings(void) {
PyObject *__pyx_1 = 0;
__pyx_init_filenames();
__pyx_m = Py_InitModule4("concatcstrings", __pyx_methods, 0, 0, PYTHON_API_VERSION);
if (!__pyx_m) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;};
Py_INCREF(__pyx_m);
__pyx_b = PyImport_AddModule("__builtin__");
if (!__pyx_b) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;};
if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;};
if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;};
__pyx_1 = PyNumber_Add(__pyx_k1p, __pyx_k2p); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;}
if (PyObject_SetAttr(__pyx_m, __pyx_n_spam, __pyx_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; goto __pyx_L1;}
Py_DECREF(__pyx_1); __pyx_1 = 0;
return;
__pyx_L1:;
Py_XDECREF(__pyx_1);
__Pyx_AddTraceback("concatcstrings");
}
示例12: test_PythonAPI
void test_PythonAPI()
{
double a_ = 3.4;
PyObject* a = PyFloat_FromDouble(a_);
PyObject* b = PyFloat_FromDouble(7);
PyObject* c = PyNumber_Add(a, b);
PyObject* list = PyList_New(0);
PyList_Append(list, a);
PyList_Append(list, c);
PyList_Append(list, b);
PyObject* tp = PyList_AsTuple(list);
int tp_len = PySequence_Length(tp);
for (int i=0; i<tp_len; i++) {
PyObject* qp = PySequence_GetItem(tp, i);
double q = PyFloat_AS_DOUBLE(qp);
std::cout << "tp[" << i << "]=" << q << " ";
}
std::cout << std::endl;
}
示例13: enum_next_long
static PyObject *
enum_next_long(enumobject *en, PyObject* next_item)
{
static PyObject *one = NULL;
PyObject *result = en->en_result;
PyObject *next_index;
PyObject *stepped_up;
if (en->en_longindex == NULL) {
en->en_longindex = PyInt_FromSsize_t(PY_SSIZE_T_MAX);
if (en->en_longindex == NULL)
return NULL;
}
if (one == NULL) {
one = PyInt_FromLong(1);
if (one == NULL)
return NULL;
}
next_index = en->en_longindex;
assert(next_index != NULL);
stepped_up = PyNumber_Add(next_index, one);
if (stepped_up == NULL)
return NULL;
en->en_longindex = stepped_up;
if (result->ob_refcnt == 1) {
Py_INCREF(result);
Py_DECREF(PyTuple_GET_ITEM(result, 0));
Py_DECREF(PyTuple_GET_ITEM(result, 1));
} else {
result = PyTuple_New(2);
if (result == NULL) {
Py_DECREF(next_index);
Py_DECREF(next_item);
return NULL;
}
}
PyTuple_SET_ITEM(result, 0, next_index);
PyTuple_SET_ITEM(result, 1, next_item);
return result;
}
示例14: build_128bit_long
static PyObject* build_128bit_long(guint64 values[2])
{
PyObject *res, *x, *y;
res = PyL_ULL(values[0]); /* res = values[0] */
x = PyI_L(64); /* x = 64 */
y = PyNumber_Lshift(res, x); /* y = res << x */
Py_DECREF(x);
Py_DECREF(res); /* res = y */
res = y;
x = PyL_ULL(values[1]); /* x = values[1] */
y = PyNumber_Add(res, x); /* y = res + x */
Py_DECREF(x); /* res = y */
Py_DECREF(res);
res = y;
return res;
}
示例15: binop
/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
with LOAD_CONST binop(c1,c2)
The consts table must still be in list form so that the
new constant can be appended.
Called with codestr pointing to the BINOP.
Abandons the transformation if the folding fails (i.e. 1+'a').
If the new constant is a sequence, only folds when the size
is below a threshold value. That keeps pyc files from
becoming large in the presence of code like: (None,)*1000.
*/
static int
fold_binops_on_constants(unsigned char *codestr, PyObject *consts, PyObject **objs)
{
PyObject *newconst, *v, *w;
Py_ssize_t len_consts, size;
int opcode;
/* Pre-conditions */
assert(PyList_CheckExact(consts));
/* Create new constant */
v = objs[0];
w = objs[1];
opcode = codestr[0];
switch (opcode) {
case BINARY_POWER:
newconst = PyNumber_Power(v, w, Py_None);
break;
case BINARY_MULTIPLY:
newconst = PyNumber_Multiply(v, w);
break;
case BINARY_TRUE_DIVIDE:
newconst = PyNumber_TrueDivide(v, w);
break;
case BINARY_FLOOR_DIVIDE:
newconst = PyNumber_FloorDivide(v, w);
break;
case BINARY_MODULO:
newconst = PyNumber_Remainder(v, w);
break;
case BINARY_ADD:
newconst = PyNumber_Add(v, w);
break;
case BINARY_SUBTRACT:
newconst = PyNumber_Subtract(v, w);
break;
case BINARY_SUBSCR:
newconst = PyObject_GetItem(v, w);
break;
case BINARY_LSHIFT:
newconst = PyNumber_Lshift(v, w);
break;
case BINARY_RSHIFT:
newconst = PyNumber_Rshift(v, w);
break;
case BINARY_AND:
newconst = PyNumber_And(v, w);
break;
case BINARY_XOR:
newconst = PyNumber_Xor(v, w);
break;
case BINARY_OR:
newconst = PyNumber_Or(v, w);
break;
default:
/* Called with an unknown opcode */
PyErr_Format(PyExc_SystemError,
"unexpected binary operation %d on a constant",
opcode);
return 0;
}
if (newconst == NULL) {
if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
PyErr_Clear();
return 0;
}
size = PyObject_Size(newconst);
if (size == -1) {
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
return 0;
PyErr_Clear();
} else if (size > 20) {
Py_DECREF(newconst);
return 0;
}
/* Append folded constant into consts table */
len_consts = PyList_GET_SIZE(consts);
if (PyList_Append(consts, newconst)) {
Py_DECREF(newconst);
return 0;
}
Py_DECREF(newconst);
/* Write NOP NOP NOP NOP LOAD_CONST newconst */
codestr[-2] = LOAD_CONST;
SETARG(codestr, -2, len_consts);
return 1;
}