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