📄 abstract.c
字号:
if (m && m->sq_repeat)
return m->sq_repeat(o, count);
return type_error("object can't be repeated");
}
DL_EXPORT(PyObject *)
PySequence_InPlaceConcat(PyObject *s, PyObject *o)
{
PySequenceMethods *m;
if (s == NULL || o == NULL)
return null_error();
// XXX:CW32
m = (PySequenceMethods *)s->ob_type->tp_as_sequence;
if (m && HASINPLACE(s) && m->sq_inplace_concat)
return m->sq_inplace_concat(s, o);
if (m && m->sq_concat)
return m->sq_concat(s, o);
return type_error("object can't be concatenated");
}
DL_EXPORT(PyObject *)
PySequence_InPlaceRepeat(PyObject *o, int count)
{
PySequenceMethods *m;
if (o == NULL)
return null_error();
// XXX:CW32
m = (PySequenceMethods *)o->ob_type->tp_as_sequence;
if (m && HASINPLACE(o) && m->sq_inplace_repeat)
return m->sq_inplace_repeat(o, count);
if (m && m->sq_repeat)
return m->sq_repeat(o, count);
return type_error("object can't be repeated");
}
DL_EXPORT(PyObject *)
PySequence_GetItem(PyObject *s, int i)
{
PySequenceMethods *m;
if (s == NULL)
return null_error();
// XXX:CW32
m = (PySequenceMethods *)s->ob_type->tp_as_sequence;
if (m && m->sq_item) {
if (i < 0) {
if (m->sq_length) {
int l = (*m->sq_length)(s);
if (l < 0)
return NULL;
i += l;
}
}
return m->sq_item(s, i);
}
return type_error("unindexable object");
}
static PyObject *
sliceobj_from_intint(int i, int j)
{
PyObject *start, *end, *slice;
start = PyInt_FromLong((long)i);
if (!start)
return NULL;
end = PyInt_FromLong((long)j);
if (!end) {
Py_DECREF(start);
return NULL;
}
slice = PySlice_New(start, end, NULL);
Py_DECREF(start);
Py_DECREF(end);
return slice;
}
DL_EXPORT(PyObject *)
PySequence_GetSlice(PyObject *s, int i1, int i2)
{
PySequenceMethods *m;
PyMappingMethods *mp;
if (!s) return null_error();
// XXX:CW32
m = (PySequenceMethods *)s->ob_type->tp_as_sequence;
if (m && m->sq_slice) {
if (i1 < 0 || i2 < 0) {
if (m->sq_length) {
int l = (*m->sq_length)(s);
if (l < 0)
return NULL;
if (i1 < 0)
i1 += l;
if (i2 < 0)
i2 += l;
}
}
return m->sq_slice(s, i1, i2);
}
// XXX:CW32
else if ((mp = (PyMappingMethods *)s->ob_type->tp_as_mapping) && mp->mp_subscript) {
PyObject *res;
PyObject *slice = sliceobj_from_intint(i1, i2);
if (!slice)
return NULL;
res = mp->mp_subscript(s, slice);
Py_DECREF(slice);
return res;
}
return type_error("unsliceable object");
}
DL_EXPORT(int)
PySequence_SetItem(PyObject *s, int i, PyObject *o)
{
PySequenceMethods *m;
if (s == NULL) {
null_error();
return -1;
}
// XXX:CW32
m = (PySequenceMethods *)s->ob_type->tp_as_sequence;
if (m && m->sq_ass_item) {
if (i < 0) {
if (m->sq_length) {
int l = (*m->sq_length)(s);
if (l < 0)
return -1;
i += l;
}
}
return m->sq_ass_item(s, i, o);
}
type_error("object doesn't support item assignment");
return -1;
}
DL_EXPORT(int)
PySequence_DelItem(PyObject *s, int i)
{
PySequenceMethods *m;
if (s == NULL) {
null_error();
return -1;
}
// XXX:CW32
m = (PySequenceMethods *)s->ob_type->tp_as_sequence;
if (m && m->sq_ass_item) {
if (i < 0) {
if (m->sq_length) {
int l = (*m->sq_length)(s);
if (l < 0)
return -1;
i += l;
}
}
return m->sq_ass_item(s, i, (PyObject *)NULL);
}
type_error("object doesn't support item deletion");
return -1;
}
DL_EXPORT(int)
PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o)
{
PySequenceMethods *m;
PyMappingMethods *mp;
if (s == NULL) {
null_error();
return -1;
}
// XXX:CW32
m = (PySequenceMethods *)s->ob_type->tp_as_sequence;
if (m && m->sq_ass_slice) {
if (i1 < 0 || i2 < 0) {
if (m->sq_length) {
int l = (*m->sq_length)(s);
if (l < 0)
return -1;
if (i1 < 0)
i1 += l;
if (i2 < 0)
i2 += l;
}
}
return m->sq_ass_slice(s, i1, i2, o);
}
// XXX:CW32
else if ((mp = (PyMappingMethods *)s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
int res;
PyObject *slice = sliceobj_from_intint(i1, i2);
if (!slice)
return -1;
res = mp->mp_ass_subscript(s, slice, o);
Py_DECREF(slice);
return res;
}
type_error("object doesn't support slice assignment");
return -1;
}
DL_EXPORT(int)
PySequence_DelSlice(PyObject *s, int i1, int i2)
{
PySequenceMethods *m;
if (s == NULL) {
null_error();
return -1;
}
// XXX:CW32
m = (PySequenceMethods *)s->ob_type->tp_as_sequence;
if (m && m->sq_ass_slice) {
if (i1 < 0 || i2 < 0) {
if (m->sq_length) {
int l = (*m->sq_length)(s);
if (l < 0)
return -1;
if (i1 < 0)
i1 += l;
if (i2 < 0)
i2 += l;
}
}
return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
}
type_error("object doesn't support slice deletion");
return -1;
}
DL_EXPORT(PyObject *)
PySequence_Tuple(PyObject *v)
{
PyObject *it; /* iter(v) */
int n; /* guess for result tuple size */
PyObject *result;
int j;
if (v == NULL)
return null_error();
/* Special-case the common tuple and list cases, for efficiency. */
if (PyTuple_CheckExact(v)) {
/* Note that we can't know whether it's safe to return
a tuple *subclass* instance as-is, hence the restriction
to exact tuples here. In contrast, lists always make
a copy, so there's no need for exactness below. */
Py_INCREF(v);
return v;
}
if (PyList_Check(v))
return PyList_AsTuple(v);
/* Get iterator. */
it = PyObject_GetIter(v);
if (it == NULL)
return NULL;
/* Guess result size and allocate space. */
n = PySequence_Size(v);
if (n < 0) {
PyErr_Clear();
n = 10; /* arbitrary */
}
result = PyTuple_New(n);
if (result == NULL)
goto Fail;
/* Fill the tuple. */
for (j = 0; ; ++j) {
PyObject *item = PyIter_Next(it);
if (item == NULL) {
if (PyErr_Occurred())
goto Fail;
break;
}
if (j >= n) {
if (n < 500)
n += 10;
else
n += 100;
if (_PyTuple_Resize(&result, n) != 0) {
Py_DECREF(item);
goto Fail;
}
}
PyTuple_SET_ITEM(result, j, item);
}
/* Cut tuple back if guess was too large. */
if (j < n &&
_PyTuple_Resize(&result, j) != 0)
goto Fail;
Py_DECREF(it);
return result;
Fail:
Py_XDECREF(result);
Py_DECREF(it);
return NULL;
}
DL_EXPORT(PyObject *)
PySequence_List(PyObject *v)
{
PyObject *it; /* iter(v) */
PyObject *result; /* result list */
int n; /* guess for result list size */
int i;
if (v == NULL)
return null_error();
/* Special-case list(a_list), for speed. */
if (PyList_Check(v))
return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
/* Get iterator. There may be some low-level efficiency to be gained
* by caching the tp_iternext slot instead of using PyIter_Next()
* later, but premature optimization is the root etc.
*/
it = PyObject_GetIter(v);
if (it == NULL)
return NULL;
/* Guess a result list size. */
n = -1; /* unknown */
if (PySequence_Check(v) &&
v->ob_type->tp_as_sequence->sq_length) {
n = PySequence_Size(v);
if (n < 0)
PyErr_Clear();
}
if (n < 0)
n = 8; /* arbitrary */
result = PyList_New(n);
if (result == NULL) {
Py_DECREF(it);
return NULL;
}
/* Run iterator to exhaustion. */
for (i = 0; ; i++) {
PyObject *item = PyIter_Next(it);
if (item == NULL) {
if (PyErr_Occurred()) {
Py_DECREF(result);
result = NULL;
}
break;
}
if (i < n)
PyList_SET_ITEM(result, i, item); /* steals ref */
else {
int status = PyList_Append(result, item);
Py_DECREF(item); /* append creates a new ref */
if (status < 0) {
Py_DECREF(result);
result = NULL;
break;
}
}
}
/* Cut back result list if initial guess was too large. */
if (i < n && result != NULL) {
if (PyList_SetSlice(result, i, n, (PyObject *)NULL) != 0) {
Py_DECREF(result);
result = NULL;
}
}
Py_DECREF(it);
return result;
}
DL_EXPORT(PyObject *)
PySequence_Fast(PyObject *v, const char *m)
{
if (v == NULL)
return null_error();
if (PyList_Check(v) || PyTuple_Check(v)) {
Py_INCREF(v);
return v;
}
v = PySequence_Tuple(v);
if (v == NULL && PyErr_ExceptionMatches(PyExc_TypeError))
return type_error(m);
return v;
}
/* Iterate over seq. Result depends on the operation:
PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq;
set ValueError and return -1 if none found; also return -1 on error.
Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
*/
DL_EXPORT(int)
_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
{
int n;
int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
PyObject *it; /* iter(seq) */
if (seq == NULL || obj == NULL) {
null_error();
return -1;
}
it = PyObject_GetIter(seq);
if (it == NULL) {
type_error("iterable argument required");
return -1;
}
n = wrapped = 0;
for (;;) {
int cmp;
PyObject *item = PyIter_Next(it);
if (item == NULL) {
if (PyErr_Occurred())
goto Fail;
break;
}
cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
Py_DECREF(item);
if (cmp < 0)
goto Fail;
if (cmp > 0) {
switch (operation) {
case PY_ITERSEARCH_COUNT:
++n;
if (n <= 0) {
PyErr_SetString(PyExc_OverflowError,
"count exceeds C int size");
goto Fail;
}
break;
case PY_ITERSEARCH_INDEX:
if (wrapped) {
PyErr_SetString(PyExc_OverflowError,
"index exceeds C int size");
goto Fail;
}
goto Done;
case PY_ITERSEARCH_CONTAINS:
n = 1;
goto Done;
default:
assert(!"unknown operation");
}
}
if (operation == PY_ITERSEARCH_INDEX) {
++n;
if (n <= 0)
wrapped = 1;
}
}
if (operation != PY_ITERSEARCH_INDEX)
goto Done;
PyErr_SetString(PyExc_ValueError,
"sequence.index(x): x not in sequence");
/* fall into failure code */
Fail:
n = -1;
/* fall through */
Done:
Py_DECREF(it);
return n;
}
/* Return # of times o appears in s. */
DL_EXPORT(int)
PySequence_Count(PyObject *s, PyObject *o)
{
return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
}
/* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
* Use sq_contains if possible, else defer to _PySequence_IterSearch().
*/
DL_EXPORT(int)
PySequence_Contains(PyObject *seq, PyObject *ob)
{
if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
// XXX:CW32
PySequenceMethods *sqm = (PySequenceMethods *)seq->ob_type->tp_as_sequence;
if (sqm != NULL && sqm->sq_contains != NULL)
return (*sqm->sq_contains)(seq, ob);
}
return _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
}
/* Backwards compatibility */
#undef PySequence_In
DL_EXPORT(int)
PySequence_In(PyObject *w, PyObject *v)
{
return PySequence_Contains(w, v);
}
DL_EXPORT(int)
PySequence_Index(PyObject *s, PyObject *o)
{
return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -