Kaydet (Commit) 1fc4b776 authored tarafından Neal Norwitz's avatar Neal Norwitz

Change some sequnce APIs to use Py_ssize_t.

üst 8c49c828
......@@ -1042,7 +1042,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
/* Return a pointer to the underlying item array for
an object retured by PySequence_Fast */
PyAPI_FUNC(int) PySequence_Count(PyObject *o, PyObject *value);
PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
/*
Return the number of occurrences on value on o, that is,
......@@ -1060,8 +1060,8 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
#define PY_ITERSEARCH_COUNT 1
#define PY_ITERSEARCH_INDEX 2
#define PY_ITERSEARCH_CONTAINS 3
PyAPI_FUNC(int) _PySequence_IterSearch(PyObject *seq, PyObject *obj,
int operation);
PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
PyObject *obj, int operation);
/*
Iterate over seq. Result depends on the operation:
PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
......@@ -1086,7 +1086,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
is equivalent to the Python expression: value in o.
*/
PyAPI_FUNC(int) PySequence_Index(PyObject *o, PyObject *value);
PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
/*
Return the first index for which o[i]=value. On error,
......
......@@ -1577,10 +1577,10 @@ PySequence_Fast(PyObject *v, const char *m)
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.
*/
int
Py_ssize_t
_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
{
int n;
Py_ssize_t n;
int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
PyObject *it; /* iter(seq) */
......@@ -1614,6 +1614,7 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
case PY_ITERSEARCH_COUNT:
++n;
if (n <= 0) {
/* XXX(nnorwitz): int means ssize_t */
PyErr_SetString(PyExc_OverflowError,
"count exceeds C int size");
goto Fail;
......@@ -1622,6 +1623,7 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
case PY_ITERSEARCH_INDEX:
if (wrapped) {
/* XXX(nnorwitz): int means ssize_t */
PyErr_SetString(PyExc_OverflowError,
"index exceeds C int size");
goto Fail;
......@@ -1660,7 +1662,7 @@ Done:
}
/* Return # of times o appears in s. */
int
Py_ssize_t
PySequence_Count(PyObject *s, PyObject *o)
{
return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
......@@ -1672,12 +1674,14 @@ PySequence_Count(PyObject *s, PyObject *o)
int
PySequence_Contains(PyObject *seq, PyObject *ob)
{
Py_ssize_t result;
if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
PySequenceMethods *sqm = 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);
result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
}
/* Backwards compatibility */
......@@ -1688,7 +1692,7 @@ PySequence_In(PyObject *w, PyObject *v)
return PySequence_Contains(w, v);
}
int
Py_ssize_t
PySequence_Index(PyObject *s, PyObject *o)
{
return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment