return -1; } func = instance_getattr(inst, lenstr); if (func == NULL) return -1; res = PyEval_CallObject(func, (PyObject *)NULL); Py_DECREF(func); if (res == NULL) return -1; if (PyInt_Check(res)) { outcome = PyInt_AsSsize_t(res); if (outcome == -1 && PyErr_Occurred()) { Py_DECREF(res); return -1; }#if SIZEOF_SIZE_T < SIZEOF_INT /* Overflow check -- range of PyInt is more than C int */ if (outcome != (int)outcome) { PyErr_SetString(PyExc_OverflowError, "__len__() should return 0 <= outcome < 2**31"); outcome = -1; } else#endif if (outcome < 0) { PyErr_SetString(PyExc_ValueError, "__len__() should return >= 0"); outcome = -1; } } else { PyErr_SetString(PyExc_TypeError, "__len__() should return an int"); outcome = -1; } Py_DECREF(res); return outcome;} static PyObject *instance_subscript(PyInstanceObject *inst, PyObject *key){ PyObject *func; PyObject *arg; PyObject *res; if (getitemstr == NULL) { getitemstr = PyString_InternFromString("__getitem__"); if (getitemstr == NULL) return NULL; } func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; arg = PyTuple_Pack(1, key); if (arg == NULL) { Py_DECREF(func); return NULL; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); return res;} static intinstance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value){ PyObject *func; PyObject *arg; PyObject *res; if (value == NULL) { if (delitemstr == NULL) { delitemstr = PyString_InternFromString("__delitem__"); if (delitemstr == NULL) return -1; } func = instance_getattr(inst, delitemstr); } else { if (setitemstr == NULL) { setitemstr = PyString_InternFromString("__setitem__"); if (setitemstr == NULL) return -1; } func = instance_getattr(inst, setitemstr); } if (func == NULL) return -1; if (value == NULL) arg = PyTuple_Pack(1, key); else arg = PyTuple_Pack(2, key, value); if (arg == NULL) { Py_DECREF(func); return -1; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); if (res == NULL) return -1; Py_DECREF(res); return 0;} static PyMappingMethods instance_as_mapping = { (lenfunc)instance_length, /* mp_length */ (binaryfunc)instance_subscript, /* mp_subscript */ (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */}; static PyObject *instance_item(PyInstanceObject *inst, Py_ssize_t i){ PyObject *func, *res; if (getitemstr == NULL) { getitemstr = PyString_InternFromString("__getitem__"); if (getitemstr == NULL) return NULL; } func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; res = PyObject_CallFunction(func, "n", i); Py_DECREF(func); return res;} static PyObject *instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j){ PyObject *func, *arg, *res; static PyObject *getslicestr; if (getslicestr == NULL) { getslicestr = PyString_InternFromString("__getslice__"); if (getslicestr == NULL) return NULL; } func = instance_getattr(inst, getslicestr); if (func == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); if (getitemstr == NULL) { getitemstr = PyString_InternFromString("__getitem__"); if (getitemstr == NULL) return NULL; } func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); } else arg = Py_BuildValue("(nn)", i, j); if (arg == NULL) { Py_DECREF(func); return NULL; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); return res;} static intinstance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item){ PyObject *func, *arg, *res; if (item == NULL) { if (delitemstr == NULL) { delitemstr = PyString_InternFromString("__delitem__"); if (delitemstr == NULL) return -1; } func = instance_getattr(inst, delitemstr); } else { if (setitemstr == NULL) { setitemstr = PyString_InternFromString("__setitem__"); if (setitemstr == NULL) return -1; } func = instance_getattr(inst, setitemstr); } if (func == NULL) return -1; if (item == NULL) arg = PyInt_FromSsize_t(i); else arg = Py_BuildValue("(nO)", i, item); if (arg == NULL) { Py_DECREF(func); return -1; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); if (res == NULL) return -1; Py_DECREF(res); return 0;} static intinstance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value){ PyObject *func, *arg, *res; static PyObject *setslicestr, *delslicestr; if (value == NULL) { if (delslicestr == NULL) { delslicestr = PyString_InternFromString("__delslice__"); if (delslicestr == NULL) return -1; } func = instance_getattr(inst, delslicestr); if (func == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return -1; PyErr_Clear(); if (delitemstr == NULL) { delitemstr = PyString_InternFromString("__delitem__"); if (delitemstr == NULL) return -1; } func = instance_getattr(inst, delitemstr); if (func == NULL) return -1; arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); } else arg = Py_BuildValue("(nn)", i, j); } else { if (setslicestr == NULL) { setslicestr = PyString_InternFromString("__setslice__"); if (setslicestr == NULL) return -1; } func = instance_getattr(inst, setslicestr); if (func == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return -1; PyErr_Clear(); if (setitemstr == NULL) { setitemstr = PyString_InternFromString("__setitem__"); if (setitemstr == NULL) return -1; } func = instance_getattr(inst, setitemstr); if (func == NULL) return -1; arg = Py_BuildValue("(NO)", _PySlice_FromIndices(i, j), value); } else arg = Py_BuildValue("(nnO)", i, j, value); } if (arg == NULL) { Py_DECREF(func); return -1; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); if (res == NULL) return -1; Py_DECREF(res); return 0;} static intinstance_contains(PyInstanceObject *inst, PyObject *member){ static PyObject *__contains__; PyObject *func; /* Try __contains__ first. * If that can't be done, try iterator-based searching. */ if(__contains__ == NULL) { __contains__ = PyString_InternFromString("__contains__"); if(__contains__ == NULL) return -1; } func = instance_getattr(inst, __contains__); if (func) { PyObject *res; int ret; PyObject *arg = PyTuple_Pack(1, member); if(arg == NULL) { Py_DECREF(func); return -1; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); if(res == NULL) return -1; ret = PyObject_IsTrue(res); Py_DECREF(res); return ret; } /* Couldn't find __contains__. */ if (PyErr_ExceptionMatches(PyExc_AttributeError)) { Py_ssize_t rc; /* Assume the failure was simply due to that there is no * __contains__ attribute, and try iterating instead. */ PyErr_Clear(); rc = _PySequence_IterSearch((PyObject *)inst, member, PY_ITERSEARCH_CONTAINS); if (rc >= 0) return rc > 0; } return -1;} static PySequenceMethodsinstance_as_sequence = { (lenfunc)instance_length, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ (ssizeargfunc)instance_item, /* sq_item */ (ssizessizeargfunc)instance_slice, /* sq_slice */ (ssizeobjargproc)instance_ass_item, /* sq_ass_item */ (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */ (objobjproc)instance_contains, /* sq_contains */}; static PyObject *generic_unary_op(PyInstanceObject *self, PyObject *methodname){ PyObject *func, *res; if ((func = instance_getattr(self, methodname)) == NULL) return NULL; res = PyEval_CallObject(func, (PyObject *)NULL); Py_DECREF(func); return res;} static PyObject *generic_binary_op(PyObject *v, PyObject *w, char *opname){ PyObject *result; PyObject *args; PyObject *func = PyObject_GetAttrString(v, opname); if (func == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } args = PyTuple_Pack(1, w); if (args == NULL) { Py_DECREF(func); return NULL; } result = PyEval_CallObject(func, args); Py_DECREF(args); Py_DECREF(func); return result;} static PyObject *coerce_obj; /* Try one half of a binary operator involving a class instance. */static PyObject *half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc, int swapped){ PyObject *args; PyObject *coercefunc; PyObject *coerced = NULL; PyObject *v1; PyObject *result; if (!PyInstance_Check(v)) { Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } if (coerce_obj == NULL) { coerce_obj = PyString_InternFromString("__coerce__"); if (coerce_obj == NULL) return NULL; } coercefunc = PyObject_GetAttr(v, coerce_obj); if (coercefunc == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); return generic_binary_op(v, w, opname); } args = PyTuple_Pack(1, w); if (args == NULL) { Py_DECREF(coercefunc); return NULL; } coerced = PyEval_CallObject(coercefunc, args); Py_DECREF(args); Py_DECREF(coercefunc); if (coerced == NULL) { return NULL; } if (coerced == Py_None || coerced == Py_NotImplemented) { Py_DECREF(coerced); return generic_binary_op(v, w, opname); } if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { Py_DECREF(coerced); PyErr_SetString(PyExc_TypeError, "coercion should return None or 2-tuple"); return NULL; } v1 = PyTuple_GetItem(coerced, 0); w = PyTuple_GetItem(coerced, 1); if (v1->ob_type == v->ob_type && PyInstance_Check(v)) { /* prevent recursion if __coerce__ returns self as the first * argument */ result = generic_binary_op(v1, w, opname); } else { if (Py_EnterRecursiveCall(" after coercion")) return NULL; if (swapped) result = (thisfunc)(w, v1); else result = (thisfunc)(v1, w); Py_LeaveRecursiveCall(); } Py_DECREF(coerced); return result;} /* Implement a binary operator involving at least one class instance. */static PyObject *do_binop(PyObject *v, PyObject *w, char *opname, char *ropname, binaryfunc thisfunc){ PyObject *result = half_binop(v, w, opname, thisfunc, 0); if (result == Py_NotImplemented) { Py_DECREF(result); result = half_binop(w, v, ropname, thisfunc, 1); } return result;} static PyObject *do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname, char *ropname, binaryfunc thisfunc){ PyObject *result = half_binop(v, w, iopname, thisfunc, 0); if (result == Py_NotImplemented) { Py_DECREF(result); result = do_binop(v, w, opname, ropname, thisfunc); } return result;} static intinstance_coerce(PyObject **pv, PyObject **pw){ PyObject *v = *pv; PyObject *w = *pw; PyObject *coercefunc; PyObject *args; PyObject *coerced; if (coerce_obj == NULL) { coerce_obj = PyString_InternFromString("__coerce__"); if (coerce_obj == NULL) return -1; } coercefunc = PyObject_GetAttr(v, coerce_obj); if (coercefunc == NULL) { /* No __coerce__ method */ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return -1; PyErr_Clear(); return 1; }Lines 1001–1500 of 2580. Previous lines Load more
return -1; } func = instance_getattr(inst, lenstr); if (func == NULL) return -1; res = PyEval_CallObject(func, (PyObject *)NULL); Py_DECREF(func); if (res == NULL) return -1; if (PyInt_Check(res)) { outcome = PyInt_AsSsize_t(res); if (outcome == -1 && PyErr_Occurred()) { Py_DECREF(res); return -1; } #if SIZEOF_SIZE_T < SIZEOF_INT /* Overflow check -- range of PyInt is more than C int */ if (outcome != (int)outcome) { PyErr_SetString(PyExc_OverflowError, "__len__() should return 0 <= outcome < 2**31"); outcome = -1; } else #endif if (outcome < 0) { PyErr_SetString(PyExc_ValueError, "__len__() should return >= 0"); outcome = -1; } } else { PyErr_SetString(PyExc_TypeError, "__len__() should return an int"); outcome = -1; } Py_DECREF(res); return outcome; } static PyObject * instance_subscript(PyInstanceObject *inst, PyObject *key) { PyObject *func; PyObject *arg; PyObject *res; if (getitemstr == NULL) { getitemstr = PyString_InternFromString("__getitem__"); if (getitemstr == NULL) return NULL; } func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; arg = PyTuple_Pack(1, key); if (arg == NULL) { Py_DECREF(func); return NULL; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); return res; } static int instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value) { PyObject *func; PyObject *arg; PyObject *res; if (value == NULL) { if (delitemstr == NULL) { delitemstr = PyString_InternFromString("__delitem__"); if (delitemstr == NULL) return -1; } func = instance_getattr(inst, delitemstr); } else { if (setitemstr == NULL) { setitemstr = PyString_InternFromString("__setitem__"); if (setitemstr == NULL) return -1; } func = instance_getattr(inst, setitemstr); } if (func == NULL) return -1; if (value == NULL) arg = PyTuple_Pack(1, key); else arg = PyTuple_Pack(2, key, value); if (arg == NULL) { Py_DECREF(func); return -1; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); if (res == NULL) return -1; Py_DECREF(res); return 0; } static PyMappingMethods instance_as_mapping = { (lenfunc)instance_length, /* mp_length */ (binaryfunc)instance_subscript, /* mp_subscript */ (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */ }; static PyObject * instance_item(PyInstanceObject *inst, Py_ssize_t i) { PyObject *func, *res; if (getitemstr == NULL) { getitemstr = PyString_InternFromString("__getitem__"); if (getitemstr == NULL) return NULL; } func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; res = PyObject_CallFunction(func, "n", i); Py_DECREF(func); return res; } static PyObject * instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j) { PyObject *func, *arg, *res; static PyObject *getslicestr; if (getslicestr == NULL) { getslicestr = PyString_InternFromString("__getslice__"); if (getslicestr == NULL) return NULL; } func = instance_getattr(inst, getslicestr); if (func == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); if (getitemstr == NULL) { getitemstr = PyString_InternFromString("__getitem__"); if (getitemstr == NULL) return NULL; } func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); } else arg = Py_BuildValue("(nn)", i, j); if (arg == NULL) { Py_DECREF(func); return NULL; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); return res; } static int instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item) { PyObject *func, *arg, *res; if (item == NULL) { if (delitemstr == NULL) { delitemstr = PyString_InternFromString("__delitem__"); if (delitemstr == NULL) return -1; } func = instance_getattr(inst, delitemstr); } else { if (setitemstr == NULL) { setitemstr = PyString_InternFromString("__setitem__"); if (setitemstr == NULL) return -1; } func = instance_getattr(inst, setitemstr); } if (func == NULL) return -1; if (item == NULL) arg = PyInt_FromSsize_t(i); else arg = Py_BuildValue("(nO)", i, item); if (arg == NULL) { Py_DECREF(func); return -1; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); if (res == NULL) return -1; Py_DECREF(res); return 0; } static int instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value) { PyObject *func, *arg, *res; static PyObject *setslicestr, *delslicestr; if (value == NULL) { if (delslicestr == NULL) { delslicestr = PyString_InternFromString("__delslice__"); if (delslicestr == NULL) return -1; } func = instance_getattr(inst, delslicestr); if (func == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return -1; PyErr_Clear(); if (delitemstr == NULL) { delitemstr = PyString_InternFromString("__delitem__"); if (delitemstr == NULL) return -1; } func = instance_getattr(inst, delitemstr); if (func == NULL) return -1; arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); } else arg = Py_BuildValue("(nn)", i, j); } else { if (setslicestr == NULL) { setslicestr = PyString_InternFromString("__setslice__"); if (setslicestr == NULL) return -1; } func = instance_getattr(inst, setslicestr); if (func == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return -1; PyErr_Clear(); if (setitemstr == NULL) { setitemstr = PyString_InternFromString("__setitem__"); if (setitemstr == NULL) return -1; } func = instance_getattr(inst, setitemstr); if (func == NULL) return -1; arg = Py_BuildValue("(NO)", _PySlice_FromIndices(i, j), value); } else arg = Py_BuildValue("(nnO)", i, j, value); } if (arg == NULL) { Py_DECREF(func); return -1; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); if (res == NULL) return -1; Py_DECREF(res); return 0; } static int instance_contains(PyInstanceObject *inst, PyObject *member) { static PyObject *__contains__; PyObject *func; /* Try __contains__ first. * If that can't be done, try iterator-based searching. */ if(__contains__ == NULL) { __contains__ = PyString_InternFromString("__contains__"); if(__contains__ == NULL) return -1; } func = instance_getattr(inst, __contains__); if (func) { PyObject *res; int ret; PyObject *arg = PyTuple_Pack(1, member); if(arg == NULL) { Py_DECREF(func); return -1; } res = PyEval_CallObject(func, arg); Py_DECREF(func); Py_DECREF(arg); if(res == NULL) return -1; ret = PyObject_IsTrue(res); Py_DECREF(res); return ret; } /* Couldn't find __contains__. */ if (PyErr_ExceptionMatches(PyExc_AttributeError)) { Py_ssize_t rc; /* Assume the failure was simply due to that there is no * __contains__ attribute, and try iterating instead. */ PyErr_Clear(); rc = _PySequence_IterSearch((PyObject *)inst, member, PY_ITERSEARCH_CONTAINS); if (rc >= 0) return rc > 0; } return -1; } static PySequenceMethods instance_as_sequence = { (lenfunc)instance_length, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ (ssizeargfunc)instance_item, /* sq_item */ (ssizessizeargfunc)instance_slice, /* sq_slice */ (ssizeobjargproc)instance_ass_item, /* sq_ass_item */ (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */ (objobjproc)instance_contains, /* sq_contains */ }; static PyObject * generic_unary_op(PyInstanceObject *self, PyObject *methodname) { PyObject *func, *res; if ((func = instance_getattr(self, methodname)) == NULL) return NULL; res = PyEval_CallObject(func, (PyObject *)NULL); Py_DECREF(func); return res; } static PyObject * generic_binary_op(PyObject *v, PyObject *w, char *opname) { PyObject *result; PyObject *args; PyObject *func = PyObject_GetAttrString(v, opname); if (func == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } args = PyTuple_Pack(1, w); if (args == NULL) { Py_DECREF(func); return NULL; } result = PyEval_CallObject(func, args); Py_DECREF(args); Py_DECREF(func); return result; } static PyObject *coerce_obj; /* Try one half of a binary operator involving a class instance. */ static PyObject * half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc, int swapped) { PyObject *args; PyObject *coercefunc; PyObject *coerced = NULL; PyObject *v1; PyObject *result; if (!PyInstance_Check(v)) { Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } if (coerce_obj == NULL) { coerce_obj = PyString_InternFromString("__coerce__"); if (coerce_obj == NULL) return NULL; } coercefunc = PyObject_GetAttr(v, coerce_obj); if (coercefunc == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; PyErr_Clear(); return generic_binary_op(v, w, opname); } args = PyTuple_Pack(1, w); if (args == NULL) { Py_DECREF(coercefunc); return NULL; } coerced = PyEval_CallObject(coercefunc, args); Py_DECREF(args); Py_DECREF(coercefunc); if (coerced == NULL) { return NULL; } if (coerced == Py_None || coerced == Py_NotImplemented) { Py_DECREF(coerced); return generic_binary_op(v, w, opname); } if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { Py_DECREF(coerced); PyErr_SetString(PyExc_TypeError, "coercion should return None or 2-tuple"); return NULL; } v1 = PyTuple_GetItem(coerced, 0); w = PyTuple_GetItem(coerced, 1); if (v1->ob_type == v->ob_type && PyInstance_Check(v)) { /* prevent recursion if __coerce__ returns self as the first * argument */ result = generic_binary_op(v1, w, opname); } else { if (Py_EnterRecursiveCall(" after coercion")) return NULL; if (swapped) result = (thisfunc)(w, v1); else result = (thisfunc)(v1, w); Py_LeaveRecursiveCall(); } Py_DECREF(coerced); return result; } /* Implement a binary operator involving at least one class instance. */ static PyObject * do_binop(PyObject *v, PyObject *w, char *opname, char *ropname, binaryfunc thisfunc) { PyObject *result = half_binop(v, w, opname, thisfunc, 0); if (result == Py_NotImplemented) { Py_DECREF(result); result = half_binop(w, v, ropname, thisfunc, 1); } return result; } static PyObject * do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname, char *ropname, binaryfunc thisfunc) { PyObject *result = half_binop(v, w, iopname, thisfunc, 0); if (result == Py_NotImplemented) { Py_DECREF(result); result = do_binop(v, w, opname, ropname, thisfunc); } return result; } static int instance_coerce(PyObject **pv, PyObject **pw) { PyObject *v = *pv; PyObject *w = *pw; PyObject *coercefunc; PyObject *args; PyObject *coerced; if (coerce_obj == NULL) { coerce_obj = PyString_InternFromString("__coerce__"); if (coerce_obj == NULL) return -1; } coercefunc = PyObject_GetAttr(v, coerce_obj); if (coercefunc == NULL) { /* No __coerce__ method */ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return -1; PyErr_Clear(); return 1; }