int PySlice_GetIndices(PySliceObject *r, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) { /* XXX support long ints */ if (r->step == Py_None) { *step = 1; } else { if (!PyLong_Check(r->step)) return -1; *step = PyLong_AsSsize_t(r->step); } if (r->start == Py_None) { *start = *step < 0 ? length-1 : 0; } else { if (!PyLong_Check(r->start)) return -1; *start = PyLong_AsSsize_t(r->start); if (*start < 0) *start += length; } if (r->stop == Py_None) { *stop = *step < 0 ? -1 : length; } else { if (!PyLong_Check(r->stop)) return -1; *stop = PyLong_AsSsize_t(r->stop); if (*stop < 0) *stop += length; } if (*stop > length) return -1; if (*start >= length) return -1; if (*step == 0) return -1; return 0; }
// set capture size int ImageViewport_setCaptureSize (PyImage * self, PyObject * value, void * closure) { // check validity of parameter if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0)) || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1))) { PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints"); return -1; } // set capture size short size [] = { short(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))), short(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1))) }; try { // can throw in case of resize and buffer exports getImageViewport(self)->setCaptureSize(size); } catch (Exception & exp) { exp.report(); return -1; } // success return 0; }
/* SinglyLinkedList2.get(index) */ static PyObject * SinglyLinkedList2_get(SinglyLinkedList2 *self, PyObject *indexobj) { SinglyLinkedListNode *n; PyObject *item; Py_ssize_t index = -1; int i; if (indexobj != NULL && indexobj != Py_None) { index = PyLong_AsSsize_t(indexobj); if (index == -1 && PyErr_Occurred()) return NULL; } if (index < 0 || index > self->size - 1) { PyErr_SetString(PyExc_IndexError, "LinkedList index out of range"); return NULL; } n = self->head; for (i = 0; i < index; ++i) n = n->next; item = n->data; Py_INCREF(item); return item; }
/* 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; }
/* ArrayList.remove(index) */ static PyObject * ArrayList_remove(ArrayList *self, PyObject *indexobj) { PyObject *old_item; Py_ssize_t index = -1; int j; if (indexobj != NULL && indexobj != Py_None) { index = PyLong_AsSsize_t(indexobj); if (index == -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "WTF"); return NULL; } } if (index < 0 || index >= self->capacity || index > self->size - 1) { PyErr_SetString(PyExc_IndexError, "ArrayList index out of range"); return NULL; } old_item = self->data[index]; for (j = index; j < self->size - 1; ++j) { self->data[j] = self->data[j+1]; } Py_INCREF(Py_None); self->data[self->size-1] = Py_None; self->size -= 1; return old_item; }
// set limit static int setLimits (PyFilter * self, PyObject * value, void * closure) { // check validity of parameter if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0)) || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1))) { PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints"); return -1; } // set limits getFilter(self)->setLimits((unsigned short)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))), (unsigned short)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1)))); // success return 0; }
/* SinglyLinkedList2.set(index, item) */ static PyObject * SinglyLinkedList2_set(SinglyLinkedList2 *self, PyObject *args) { SinglyLinkedListNode *n; PyObject *indexobj = NULL, *itemobj = NULL, *old_item = NULL; Py_ssize_t index = -1; int i; if (!PyArg_ParseTuple(args, "OO", &indexobj, &itemobj)) { return NULL; } if (indexobj != NULL && indexobj != Py_None) { index = PyLong_AsSsize_t(indexobj); if (index == -1 && PyErr_Occurred()) return NULL; } if (index < 0 || index > self->size - 1) { PyErr_SetString(PyExc_IndexError, "SinglyLinkedList2 index out of range"); return NULL; } if (itemobj == NULL) { PyErr_SetString(PyExc_ValueError, "item == NULL"); return NULL; } n = self->head; for (i = 0; i < index; ++i) n = n->next; ++self->state; old_item = n->data; Py_XDECREF(old_item); Py_INCREF(itemobj); n->data = itemobj; Py_RETURN_NONE; }
/* 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; }
static PyObject * KX_PythonSeq_subscript(PyObject * self, PyObject *key) { PyObjectPlus *self_plus= BGE_PROXY_REF(((KX_PythonSeq *)self)->base); if(self_plus==NULL) { PyErr_SetString(PyExc_SystemError, "val = seq[key], KX_PythonSeq: "BGE_PROXY_ERROR_MSG); return NULL; } if (PyLong_Check(key)) { return KX_PythonSeq_getIndex(self, PyLong_AsSsize_t( key )); } else if ( PyUnicode_Check(key) ) { char *name = _PyUnicode_AsString(key); PyObjectPlus *ret = KX_PythonSeq_subscript__internal(self, name); if(ret) { return ret->GetProxy(); } else { PyErr_Format( PyExc_KeyError, "requested item \"%s\" does not exist", name); return NULL; } } else { PyErr_SetString( PyExc_TypeError, "expected a string or an index" ); return NULL; } }
static PyObject *SEQUENCE_REPEAT( ssizeargfunc repeatfunc, PyObject *seq, PyObject *n ) { if (unlikely( !PyIndex_Check( n ) )) { PyErr_Format( PyExc_TypeError, "can't multiply sequence by non-int of type '%s'", Py_TYPE( n )->tp_name ); return NULL; } PyObject *index_value = PyNumber_Index( n ); if (unlikely( index_value == NULL )) { return NULL; } /* We're done if PyInt_AsSsize_t() returns without error. */ #if PYTHON_VERSION < 300 Py_ssize_t count = PyInt_AsSsize_t( index_value ); #else Py_ssize_t count = PyLong_AsSsize_t( index_value ); #endif Py_DECREF( index_value ); if (unlikely( count == -1 )) // Note: -1 is an unlikely repetition count { PyObject *exception = GET_ERROR_OCCURRED(); if (unlikely( exception )) { if ( !EXCEPTION_MATCH_BOOL_SINGLE( exception, PyExc_OverflowError ) ) { return NULL; } PyErr_Format( PyExc_OverflowError, "cannot fit '%s' into an index-sized integer", Py_TYPE( n )->tp_name ); return NULL; } } PyObject *result = (*repeatfunc)( seq, count ); if (unlikely( result == NULL )) { return NULL; } return result; }
PyObject* KX_VertexProxy::PySetRGBA(PyObject* value) { if PyLong_Check(value) { int rgba = PyLong_AsSsize_t(value); m_vertex->SetRGBA(rgba); m_mesh->SetMeshModified(true); Py_RETURN_NONE; } else {
// set position static int ImageViewport_setPosition (PyImage * self, PyObject * value, void * closure) { // check validity of parameter if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0)) || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1))) { PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints"); return -1; } // set position GLint pos [] = { GLint(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))), GLint(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1))) }; getImageViewport(self)->setPosition(pos); // success return 0; }
// set capture size int ImageViewport_setCaptureSize (PyImage * self, PyObject * value, void * closure) { // check validity of parameter if (value == NULL || !PySequence_Check(value) || PySequence_Length(value) != 2 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0)) || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1))) { PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints"); return -1; } // set capture size short size [] = { short(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))), short(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1))) }; getImageViewport(self)->setCaptureSize(size); // success return 0; }
static Py_ssize_t range_length(rangeobject *r) { PyObject *len = range_length_obj(r); Py_ssize_t result = -1; if (len) { result = PyLong_AsSsize_t(len); Py_DECREF(len); } return result; }
void list_base::insert(object const& index, object_cref x) { #if PY_VERSION_HEX >= 0x03000000 ssize_t index_ = PyLong_AsSsize_t(index.ptr()); #else long index_ = PyInt_AsLong(index.ptr()); #endif if (index_ == -1 && PyErr_Occurred()) throw_error_already_set(); this->insert(index_, x); }
static int bpy_bmdeformvert_contains(BPy_BMDeformVert *self, PyObject *value) { const int key = PyLong_AsSsize_t(value); if (key == -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "BMDeformVert.__contains__: expected an int"); return -1; } return (defvert_find_index(self->data, key) != NULL) ? 1 : 0; }
long list_base::index(object_cref value) const { object result_obj(this->attr("index")(value)); #if PY_VERSION_HEX >= 0x03000000 ssize_t result = PyLong_AsSsize_t(result_obj.ptr()); #else long result = PyInt_AsLong(result_obj.ptr()); #endif if (result == -1) throw_error_already_set(); return result; }
static PyObject * iter_setstate(seqiterobject *it, PyObject *state) { Py_ssize_t index = PyLong_AsSsize_t(state); if (index == -1 && PyErr_Occurred()) return NULL; if (it->it_seq != NULL) { if (index < 0) index = 0; it->it_index = index; } Py_RETURN_NONE; }
static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure)) { int param= PyLong_AsSsize_t(value); if (param == -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "bpy.app.debug_value can only be set to a whole number"); return -1; } G.rt= param; return 0; }
static PyObject * tupleiter_setstate(tupleiterobject *it, PyObject *state) { Py_ssize_t index = PyLong_AsSsize_t(state); if (index == -1 && PyErr_Occurred()) return NULL; if (it->it_seq != NULL) { if (index < 0) index = 0; else if (it->it_seq != NULL && index > PyTuple_GET_SIZE(it->it_seq)) index = PyTuple_GET_SIZE(it->it_seq); it->it_index = index; } Py_RETURN_NONE; }
static PyObject *listvalue_mapping_subscript(PyObject *self, PyObject *key) { CListValue *list= static_cast<CListValue *>(BGE_PROXY_REF(self)); if (list==NULL) { PyErr_SetString(PyExc_SystemError, "value = CList[i], "BGE_PROXY_ERROR_MSG); return NULL; } if (PyUnicode_Check(key)) { CValue *item = ((CListValue*) list)->FindValue(_PyUnicode_AsString(key)); if (item) { PyObject *pyobj = item->ConvertValueToPython(); if (pyobj) return pyobj; else return item->GetProxy(); } } else if (PyIndex_Check(key)) { Py_ssize_t index = PyLong_AsSsize_t(key); return listvalue_buffer_item(self, index); /* wont add a ref */ } else if (PySlice_Check(key)) { Py_ssize_t start, stop, step, slicelength; if (PySlice_GetIndicesEx(key, list->GetCount(), &start, &stop, &step, &slicelength) < 0) return NULL; if (slicelength <= 0) { return PyList_New(0); } else if (step == 1) { return listvalue_buffer_slice(list, start, stop); } else { PyErr_SetString(PyExc_TypeError, "CList[slice]: slice steps not supported"); return NULL; } } PyErr_Format(PyExc_KeyError, "CList[key]: '%R' key not in list", key); return NULL; }
ssize_t skull_module_unpack (void* md, skull_txn_t* txn, const void* data, size_t data_len) { PyGILState_STATE state = PyGILState_Ensure(); module_data_t* mdata = (module_data_t*)md; // Prepare args PyObject* pyArgs = PyTuple_New(4); PyObject* pyModuleName = PyString_FromString(mdata->name); PyObject* pyEntryName = PyString_FromString(MODULE_UNPACK_FUNCNAME); PyObject* pyTxn = PyCapsule_New(txn, "skull_txn", NULL); PyObject* pyData = PyString_FromStringAndSize((const char*)data, (Py_ssize_t)data_len); PyTuple_SetItem(pyArgs, 0, pyModuleName); PyTuple_SetItem(pyArgs, 1, pyEntryName); PyTuple_SetItem(pyArgs, 2, pyTxn); PyTuple_SetItem(pyArgs, 3, pyData); // Call user module_unpack PyObject* pyConsumed = PyObject_CallObject(mdata->pyExecutorFunc, pyArgs); // Set to -1 by default, so if error occurred, like un-handled excpetions, // then the entity will be destroyed soon ssize_t consumed = -1; if (!pyConsumed) { if (PyErr_Occurred()) PyErr_Print(); } else { if (PyInt_Check(pyConsumed)) { consumed = PyInt_AsSsize_t(pyConsumed); } else if (PyLong_Check(pyConsumed)) { consumed = PyLong_AsSsize_t(pyConsumed); } else { fprintf(stderr, "Error: Cannot parse the pyConsumed object"); _PyObject_Dump(pyConsumed); } } Py_XDECREF(pyConsumed); Py_DECREF(pyArgs); PyGILState_Release(state); return consumed; }
/* SinglyLinkedList1.insert(index, item) */ static PyObject * SinglyLinkedList1_insert(SinglyLinkedList1 *self, PyObject *args) { SinglyLinkedListNode *n, *tmp; PyObject *indexobj = NULL, *itemobj = NULL; Py_ssize_t index = -1; int i; if (!PyArg_ParseTuple(args, "OO", &indexobj, &itemobj)) { return NULL; } if (indexobj != NULL && indexobj != Py_None) { index = PyLong_AsSsize_t(indexobj); if (index == -1 && PyErr_Occurred()) return NULL; } if (index < 0 || index > self->size - 1) { PyErr_SetString(PyExc_IndexError, "SinglyLinkedList1 index out of range"); return NULL; } if (itemobj == NULL) { PyErr_SetString(PyExc_ValueError, "item == NULL"); return NULL; } if (index == 0) return SinglyLinkedList1_prepend(self, itemobj); n = self->head; for (i = 0; i < index - 1; ++i) n = n->next; tmp = PyMem_Malloc(sizeof(SinglyLinkedListNode)); if (tmp == NULL) { PyErr_NoMemory(); return NULL; } ++self->state; ++self->size; Py_INCREF(itemobj); tmp->data = itemobj; tmp->next = n->next; n->next = tmp; Py_RETURN_NONE; }
/* * Implement setsize() for the type. */ static PyObject *sipVoidPtr_setsize(sipVoidPtrObject *v, PyObject *arg) { SIP_SSIZE_T size; #if PY_MAJOR_VERSION >= 3 size = PyLong_AsSsize_t(arg); #elif PY_VERSION_HEX >= 0x02050000 size = PyInt_AsSsize_t(arg); #else size = (int)PyInt_AsLong(arg); #endif if (PyErr_Occurred()) return NULL; v->size = size; Py_INCREF(Py_None); return Py_None; }
static int get_ssize_t(PyObject *v, Py_ssize_t *p) { Py_ssize_t x; v = get_pylong(v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsSsize_t(v); Py_DECREF(v); if (x == (Py_ssize_t)-1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) PyErr_SetString(StructError, "argument out of range"); return -1; } *p = x; return 0; }
static int _long_shared(PyObject *obj, _PyCrossInterpreterData *data) { /* Note that this means the size of shareable ints is bounded by * sys.maxsize. Hence on 32-bit architectures that is half the * size of maximum shareable ints on 64-bit. */ Py_ssize_t value = PyLong_AsSsize_t(obj); if (value == -1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { PyErr_SetString(PyExc_OverflowError, "try sending as bytes"); } return -1; } data->data = (void *)value; data->obj = NULL; data->new_object = _new_long_object; data->free = NULL; return 0; }
bool KX_PolygonMaterial::Activate(RAS_IRasterizer* rasty, TCachingInfo& cachingInfo) const { bool dopass = false; #ifdef WITH_PYTHON if (m_pymaterial) { PyObject *pyRasty = PyCapsule_New((void*)rasty, KX_POLYGONMATERIAL_CAPSULE_ID, NULL); /* new reference */ PyObject *pyCachingInfo = PyCapsule_New((void*) &cachingInfo, KX_POLYGONMATERIAL_CAPSULE_ID, NULL); /* new reference */ PyObject *ret = PyObject_CallMethod(m_pymaterial, (char *)"activate", (char *)"(NNO)", pyRasty, pyCachingInfo, (PyObject*) this->m_proxy); if (ret) { bool value = PyLong_AsSsize_t(ret); Py_DECREF(ret); dopass = value; } else { PyErr_Print(); PyErr_Clear(); PySys_SetObject( (char *)"last_traceback", NULL); } } else #endif // WITH_PYTHON { switch (m_pass++) { case 0: DefaultActivate(rasty, cachingInfo); dopass = true; break; default: m_pass = 0; dopass = false; break; } } return dopass; }
/* ArrayList.get(index) */ static PyObject * ArrayList_get(ArrayList *self, PyObject *indexobj) { Py_ssize_t index = -1; PyObject *item; if (indexobj != NULL && indexobj != Py_None) { index = PyLong_AsSsize_t(indexobj); if (index == -1 && PyErr_Occurred()) return NULL; } #if defined(NDEBUG) if (index < 0 || index >= self->capacity || index > self->size - 1) { PyErr_SetString(PyExc_IndexError, "ArrayList index out of range"); return NULL; } #endif item = self->data[index]; Py_INCREF(item); return item; }
PyObject* KX_VertexProxy::PySetRGBA(PyObject* value) { if (PyLong_Check(value)) { int rgba = PyLong_AsSsize_t(value); m_vertex->SetRGBA(rgba); m_mesh->SetMeshModified(true); Py_RETURN_NONE; } else { MT_Vector4 vec; if (PyVecTo(value, vec)) { m_vertex->SetRGBA(vec); m_mesh->SetMeshModified(true); Py_RETURN_NONE; } } PyErr_SetString(PyExc_TypeError, "vert.setRGBA(value): KX_VertexProxy, expected a 4D vector or an int"); return NULL; }
static int BPy_IDArray_SetItem(BPy_IDArray *self, int index, PyObject *value) { int i; float f; double d; if (index < 0 || index >= self->prop->len) { PyErr_SetString(PyExc_RuntimeError, "index out of range!"); return -1; } switch (self->prop->subtype) { case IDP_FLOAT: f= (float)PyFloat_AsDouble(value); if (f==-1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "expected a float"); return -1; } ((float*)self->prop->data.pointer)[index] = f; break; case IDP_DOUBLE: d= PyFloat_AsDouble(value); if (d==-1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "expected a float"); return -1; } ((double*)self->prop->data.pointer)[index] = d; break; case IDP_INT: i= PyLong_AsSsize_t(value); if (i==-1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "expected an int type"); return -1; } ((int*)self->prop->data.pointer)[index] = i; break; } return 0; }