[Python-checkins] commit of r41782 - python/branches/ssize_t/Objects/listobject.c

martin.v.loewis python-checkins at python.org
Wed Dec 21 00:57:26 CET 2005


Author: martin.v.loewis
Date: Wed Dec 21 00:57:25 2005
New Revision: 41782

Modified:
   python/branches/ssize_t/Objects/listobject.c
Log:
Use Py_ssize_t throughout.


Modified: python/branches/ssize_t/Objects/listobject.c
==============================================================================
--- python/branches/ssize_t/Objects/listobject.c	(original)
+++ python/branches/ssize_t/Objects/listobject.c	Wed Dec 21 00:57:25 2005
@@ -26,7 +26,7 @@
 {
 	PyObject **items;
 	size_t new_allocated;
-	int allocated = self->allocated;
+	Py_ssize_t allocated = self->allocated;
 
 	/* Bypass realloc() when a previous overallocation is large enough
 	   to accommodate the newsize.  If the newsize falls lower than half
@@ -175,7 +175,7 @@
 static int
 ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
 {
-	int i, n = self->ob_size;
+	Py_ssize_t i, n = self->ob_size;
 	PyObject **items;
 	if (v == NULL) {
 		PyErr_BadInternalCall();
@@ -218,7 +218,7 @@
 static int
 app1(PyListObject *self, PyObject *v)
 {
-	int n = PyList_GET_SIZE(self);
+	Py_ssize_t n = PyList_GET_SIZE(self);
 
 	assert (v != NULL);
 	if (n == INT_MAX) {
@@ -249,7 +249,7 @@
 static void
 list_dealloc(PyListObject *op)
 {
-	int i;
+	Py_ssize_t i;
 	PyObject_GC_UnTrack(op);
 	Py_TRASHCAN_SAFE_BEGIN(op)
 	if (op->ob_item != NULL) {
@@ -273,7 +273,7 @@
 static int
 list_print(PyListObject *op, FILE *fp, int flags)
 {
-	int i;
+	Py_ssize_t i;
 
 	i = Py_ReprEnter((PyObject*)op);
 	if (i != 0) {
@@ -299,7 +299,7 @@
 static PyObject *
 list_repr(PyListObject *v)
 {
-	int i;
+	Py_ssize_t i;
 	PyObject *s, *temp;
 	PyObject *pieces = NULL, *result = NULL;
 
@@ -372,7 +372,8 @@
 static int
 list_contains(PyListObject *a, PyObject *el)
 {
-	int i, cmp;
+	Py_ssize_t i;
+	int cmp;
 
 	for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
 		cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
@@ -399,7 +400,7 @@
 {
 	PyListObject *np;
 	PyObject **src, **dest;
-	int i, len;
+	Py_ssize_t i, len;
 	if (ilow < 0)
 		ilow = 0;
 	else if (ilow > a->ob_size)
@@ -436,8 +437,8 @@
 static PyObject *
 list_concat(PyListObject *a, PyObject *bb)
 {
-	int size;
-	int i;
+	Py_ssize_t size;
+	Py_ssize_t i;
 	PyObject **src, **dest;
 	PyListObject *np;
 	if (!PyList_Check(bb)) {
@@ -475,8 +476,8 @@
 static PyObject *
 list_repeat(PyListObject *a, Py_ssize_t n)
 {
-	int i, j;
-	int size;
+	Py_ssize_t i, j;
+	Py_ssize_t size;
 	PyListObject *np;
 	PyObject **p, **items;
 	PyObject *elem;
@@ -515,7 +516,7 @@
 static int
 list_clear(PyListObject *a)
 {
-	int i;
+	Py_ssize_t i;
 	PyObject **item = a->ob_item;
 	if (item != NULL) {
 		/* Because XDECREF can recursively invoke operations on
@@ -555,10 +556,10 @@
 	PyObject **item;
 	PyObject **vitem = NULL;
 	PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
-	int n; /* # of elements in replacement list */
-	int norig; /* # of elements in list getting replaced */
-	int d; /* Change in size */
-	int k;
+	Py_ssize_t n; /* # of elements in replacement list */
+	Py_ssize_t norig; /* # of elements in list getting replaced */
+	Py_ssize_t d; /* Change in size */
+	Py_ssize_t k;
 	size_t s;
 	int result = -1;	/* guilty until proved innocent */
 #define b ((PyListObject *)v)
@@ -653,7 +654,7 @@
 list_inplace_repeat(PyListObject *self, Py_ssize_t n)
 {
 	PyObject **items;
-	int size, i, j, p;
+	Py_ssize_t size, i, j, p;
 
 
 	size = PyList_GET_SIZE(self);
@@ -705,7 +706,7 @@
 static PyObject *
 listinsert(PyListObject *self, PyObject *args)
 {
-	int i;
+	Py_ssize_t i;
 	PyObject *v;
 	if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
 		return NULL;
@@ -726,10 +727,10 @@
 listextend(PyListObject *self, PyObject *b)
 {
 	PyObject *it;      /* iter(v) */
-	int m;		   /* size of self */
-	int n;		   /* guess for size of b */
-	int mn;		   /* m + n */
-	int i;
+	Py_ssize_t m;		   /* size of self */
+	Py_ssize_t n;		   /* guess for size of b */
+	Py_ssize_t mn;		   /* m + n */
+	Py_ssize_t i;
 	PyObject *(*iternext)(PyObject *);
 
 	/* Special cases:
@@ -858,7 +859,7 @@
 static PyObject *
 listpop(PyListObject *self, PyObject *args)
 {
-	int i = -1;
+	Py_ssize_t i = -1;
 	PyObject *v, *arg = NULL;
 	int status;
 
@@ -866,7 +867,7 @@
 		return NULL;
 	if (arg != NULL) {
 		if (PyInt_Check(arg))
-			i = (int)(PyInt_AS_LONG((PyIntObject*) arg));
+			i = PyInt_AS_LONG((PyIntObject*) arg);
 		else if (!PyArg_ParseTuple(args, "|i:pop", &i))
    			return NULL;
 	}
@@ -929,7 +930,7 @@
 {
 	PyObject *res;
 	PyObject *args;
-	int i;
+	Py_ssize_t i;
 
 	assert(compare != NULL);
 	/* Call the user's comparison function and translate the 3-way
@@ -988,7 +989,7 @@
 binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
      /* compare -- comparison function object, or NULL for default */
 {
-	register int k;
+	register Py_ssize_t k;
 	register PyObject **l, **p, **r;
 	register PyObject *pivot;
 
@@ -1053,8 +1054,8 @@
 static int
 count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending)
 {
-	int k;
-	int n;
+	Py_ssize_t k;
+	Py_ssize_t n;
 
 	assert(lo < hi);
 	*descending = 0;
@@ -1105,12 +1106,12 @@
 
 Returns -1 on error.  See listsort.txt for info on the method.
 */
-static int
-gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
+static Py_ssize_t
+gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
 {
-	int ofs;
-	int lastofs;
-	int k;
+	Py_ssize_t ofs;
+	Py_ssize_t lastofs;
+	Py_ssize_t k;
 
 	assert(key && a && n > 0 && hint >= 0 && hint < n);
 
@@ -1121,7 +1122,7 @@
 		/* a[hint] < key -- gallop right, until
 		 * a[hint + lastofs] < key <= a[hint + ofs]
 		 */
-		const int maxofs = n - hint;	/* &a[n-1] is highest */
+		const Py_ssize_t maxofs = n - hint;	/* &a[n-1] is highest */
 		while (ofs < maxofs) {
 			IFLT(a[ofs], key) {
 				lastofs = ofs;
@@ -1142,7 +1143,7 @@
 		/* key <= a[hint] -- gallop left, until
 		 * a[hint - ofs] < key <= a[hint - lastofs]
 		 */
-		const int maxofs = hint + 1;	/* &a[0] is lowest */
+		const Py_ssize_t maxofs = hint + 1;	/* &a[0] is lowest */
 		while (ofs < maxofs) {
 			IFLT(*(a-ofs), key)
 				break;
@@ -1168,7 +1169,7 @@
 	 */
 	++lastofs;
 	while (lastofs < ofs) {
-		int m = lastofs + ((ofs - lastofs) >> 1);
+		Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
 
 		IFLT(a[m], key)
 			lastofs = m+1;	/* a[m] < key */
@@ -1196,12 +1197,12 @@
 we're sticking to "<" comparisons that it's much harder to follow if
 written as one routine with yet another "left or right?" flag.
 */
-static int
-gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
+static Py_ssize_t
+gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
 {
-	int ofs;
-	int lastofs;
-	int k;
+	Py_ssize_t ofs;
+	Py_ssize_t lastofs;
+	Py_ssize_t k;
 
 	assert(key && a && n > 0 && hint >= 0 && hint < n);
 
@@ -1212,7 +1213,7 @@
 		/* key < a[hint] -- gallop left, until
 		 * a[hint - ofs] <= key < a[hint - lastofs]
 		 */
-		const int maxofs = hint + 1;	/* &a[0] is lowest */
+		const Py_ssize_t maxofs = hint + 1;	/* &a[0] is lowest */
 		while (ofs < maxofs) {
 			IFLT(key, *(a-ofs)) {
 				lastofs = ofs;
@@ -1234,7 +1235,7 @@
 		/* a[hint] <= key -- gallop right, until
 		 * a[hint + lastofs] <= key < a[hint + ofs]
 		*/
-		const int maxofs = n - hint;	/* &a[n-1] is highest */
+		const Py_ssize_t maxofs = n - hint;	/* &a[n-1] is highest */
 		while (ofs < maxofs) {
 			IFLT(key, a[ofs])
 				break;
@@ -1259,7 +1260,7 @@
 	 */
 	++lastofs;
 	while (lastofs < ofs) {
-		int m = lastofs + ((ofs - lastofs) >> 1);
+		Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
 
 		IFLT(key, a[m])
 			ofs = m;	/* key < a[m] */
@@ -1294,7 +1295,7 @@
  */
 struct s_slice {
 	PyObject **base;
-	int len;
+	Py_ssize_t len;
 };
 
 typedef struct s_MergeState {
@@ -1305,13 +1306,13 @@
 	 * to MIN_GALLOP.  merge_lo and merge_hi tend to nudge it higher for
 	 * random data, and lower for highly structured data.
 	 */
-	int min_gallop;
+	Py_ssize_t min_gallop;
 
 	/* 'a' is temp storage to help with merges.  It contains room for
 	 * alloced entries.
 	 */
 	PyObject **a;	/* may point to temparray below */
-	int alloced;
+	Py_ssize_t alloced;
 
 	/* A stack of n pending runs yet to be merged.  Run #i starts at
 	 * address base[i] and extends for len[i] elements.  It's always
@@ -1359,7 +1360,7 @@
  * Returns 0 on success and -1 if the memory can't be gotten.
  */
 static int
-merge_getmem(MergeState *ms, int need)
+merge_getmem(MergeState *ms, Py_ssize_t need)
 {
 	assert(ms != NULL);
 	if (need <= ms->alloced)
@@ -1389,7 +1390,7 @@
 static int
 merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
 {
-	int k;
+	Py_ssize_t k;
 	PyObject *compare;
 	PyObject **dest;
 	int result = -1;	/* guilty until proved innocent */
@@ -1411,8 +1412,8 @@
 
 	compare = ms->compare;
 	for (;;) {
-		int acount = 0;	/* # of times A won in a row */
-		int bcount = 0;	/* # of times B won in a row */
+		Py_ssize_t acount = 0;	/* # of times A won in a row */
+		Py_ssize_t bcount = 0;	/* # of times B won in a row */
 
 		/* Do the straightforward thing until (if ever) one run
 		 * appears to win consistently.
@@ -1518,15 +1519,15 @@
  * Return 0 if successful, -1 if error.
  */
 static int
-merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
+merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t nb)
 {
-	int k;
+	Py_ssize_t k;
 	PyObject *compare;
 	PyObject **dest;
 	int result = -1;	/* guilty until proved innocent */
 	PyObject **basea;
 	PyObject **baseb;
-	int min_gallop = ms->min_gallop;
+	Py_ssize_t min_gallop = ms->min_gallop;
 
 	assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
 	if (MERGE_GETMEM(ms, nb) < 0)
@@ -1547,8 +1548,8 @@
 
 	compare = ms->compare;
 	for (;;) {
-		int acount = 0;	/* # of times A won in a row */
-		int bcount = 0;	/* # of times B won in a row */
+		Py_ssize_t acount = 0;	/* # of times A won in a row */
+		Py_ssize_t bcount = 0;	/* # of times B won in a row */
 
 		/* Do the straightforward thing until (if ever) one run
 		 * appears to win consistently.
@@ -1655,11 +1656,11 @@
  * Returns 0 on success, -1 on error.
  */
 static int
-merge_at(MergeState *ms, int i)
+merge_at(MergeState *ms, Py_ssize_t i)
 {
 	PyObject **pa, **pb;
-	int na, nb;
-	int k;
+	Py_ssize_t na, nb;
+	Py_ssize_t k;
 	PyObject *compare;
 
 	assert(ms != NULL);
@@ -1728,7 +1729,7 @@
 
 	assert(ms);
 	while (ms->n > 1) {
-		int n = ms->n - 2;
+		Py_ssize_t n = ms->n - 2;
 		if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
 		    	if (p[n-1].len < p[n+1].len)
 		    		--n;
@@ -1757,7 +1758,7 @@
 
 	assert(ms);
 	while (ms->n > 1) {
-		int n = ms->n - 2;
+		Py_ssize_t n = ms->n - 2;
 		if (n > 0 && p[n-1].len < p[n+1].len)
 			--n;
 		if (merge_at(ms, n) < 0)
@@ -1776,10 +1777,10 @@
  *
  * See listsort.txt for more info.
  */
-static int
-merge_compute_minrun(int n)
+static Py_ssize_t
+merge_compute_minrun(Py_ssize_t n)
 {
-	int r = 0;	/* becomes 1 if any 1 bits are shifted off */
+	Py_ssize_t r = 0;	/* becomes 1 if any 1 bits are shifted off */
 
 	assert(n >= 0);
 	while (n >= 64) {
@@ -1805,7 +1806,7 @@
 static PyTypeObject sortwrapper_type;
 
 static PyObject *
-sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
+sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, Py_ssize_t op)
 {
 	if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
 		PyErr_SetString(PyExc_TypeError,
@@ -1972,16 +1973,16 @@
 {
 	MergeState ms;
 	PyObject **lo, **hi;
-	int nremaining;
-	int minrun;
-	int saved_ob_size, saved_allocated;
+	Py_ssize_t nremaining;
+	Py_ssize_t minrun;
+	Py_ssize_t saved_ob_size, saved_allocated;
 	PyObject **saved_ob_item;
 	PyObject **final_ob_item;
 	PyObject *compare = NULL;
 	PyObject *result = NULL;	/* guilty until proved innocent */
 	int reverse = 0;
 	PyObject *keyfunc = NULL;
-	int i;
+	Py_ssize_t i;
 	PyObject *key, *value, *kvpair;
 	static const char *kwlist[] = {"cmp", "key", "reverse", 0};
 
@@ -2055,7 +2056,7 @@
 	minrun = merge_compute_minrun(nremaining);
 	do {
 		int descending;
-		int n;
+		Py_ssize_t n;
 
 		/* Identify next run. */
 		n = count_run(lo, hi, compare, &descending);
@@ -2065,7 +2066,7 @@
 			reverse_slice(lo, lo + n);
 		/* If short, extend to min(minrun, nremaining). */
 		if (n < minrun) {
-			const int force = nremaining <= minrun ?
+			const Py_ssize_t force = nremaining <= minrun ?
 	 			  	  nremaining : minrun;
 			if (binarysort(lo, lo + force, lo + n, compare) < 0)
 				goto fail;
@@ -2177,7 +2178,7 @@
 {
 	PyObject *w;
 	PyObject **p;
-	int n;
+	Py_ssize_t n;
 	if (v == NULL || !PyList_Check(v)) {
 		PyErr_BadInternalCall();
 		return NULL;
@@ -2200,7 +2201,7 @@
 static PyObject *
 listindex(PyListObject *self, PyObject *args)
 {
-	int i, start=0, stop=self->ob_size;
+	Py_ssize_t i, start=0, stop=self->ob_size;
 	PyObject *v;
 
 	if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
@@ -2220,7 +2221,7 @@
 	for (i = start; i < stop && i < self->ob_size; i++) {
 		int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
 		if (cmp > 0)
-			return PyInt_FromLong((long)i);
+			return PyInt_FromSsize_t(i);
 		else if (cmp < 0)
 			return NULL;
 	}
@@ -2231,8 +2232,8 @@
 static PyObject *
 listcount(PyListObject *self, PyObject *v)
 {
-	int count = 0;
-	int i;
+	Py_ssize_t count = 0;
+	Py_ssize_t i;
 
 	for (i = 0; i < self->ob_size; i++) {
 		int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
@@ -2241,13 +2242,13 @@
 		else if (cmp < 0)
 			return NULL;
 	}
-	return PyInt_FromLong((long)count);
+	return PyInt_FromSsize_t(count);
 }
 
 static PyObject *
 listremove(PyListObject *self, PyObject *v)
 {
-	int i;
+	Py_ssize_t i;
 
 	for (i = 0; i < self->ob_size; i++) {
 		int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
@@ -2267,7 +2268,7 @@
 static int
 list_traverse(PyListObject *o, visitproc visit, void *arg)
 {
-	int i, err;
+	Py_ssize_t i, err;
 	PyObject *x;
 
 	for (i = o->ob_size; --i >= 0; ) {
@@ -2285,7 +2286,7 @@
 list_richcompare(PyObject *v, PyObject *w, int op)
 {
 	PyListObject *vl, *wl;
-	int i;
+	Py_ssize_t i;
 
 	if (!PyList_Check(v) || !PyList_Check(w)) {
 		Py_INCREF(Py_NotImplemented);
@@ -2318,8 +2319,8 @@
 
 	if (i >= vl->ob_size || i >= wl->ob_size) {
 		/* No more items to compare -- compare sizes */
-		int vs = vl->ob_size;
-		int ws = wl->ob_size;
+		Py_ssize_t vs = vl->ob_size;
+		Py_ssize_t ws = wl->ob_size;
 		int cmp;
 		PyObject *res;
 		switch (op) {
@@ -2535,7 +2536,7 @@
 		if (value == NULL) {
 			/* delete slice */
 			PyObject **garbage;
-			int cur, i;
+			Py_ssize_t cur, i;
 
 			if (slicelength <= 0)
 				return 0;
@@ -2554,7 +2555,7 @@
 			for (cur = start, i = 0;
 			     cur < stop;
 			     cur += step, i++) {
-				int lim = step;
+				Py_ssize_t lim = step;
 
 				garbage[i] = PyList_GET_ITEM(self, cur);
 
@@ -2586,7 +2587,7 @@
 		else {
 			/* assign slice */
 			PyObject **garbage, *ins, *seq, **seqitems, **selfitems;
-			int cur, i;
+			Py_ssize_t cur, i;
 
 			/* protect against a[::-1] = a */
 			if (self == (PyListObject*)value) {
@@ -2768,11 +2769,11 @@
 static PyObject *
 listiter_len(listiterobject *it)
 {
-	int len;
+	Py_ssize_t len;
 	if (it->it_seq) {
 		len = PyList_GET_SIZE(it->it_seq) - it->it_index;
 		if (len >= 0)
-			return PyInt_FromLong((long)len);
+			return PyInt_FromSsize_t(len);
 	}
 	return PyInt_FromLong(0);
 }
@@ -2884,7 +2885,7 @@
 static int
 listreviter_len(listreviterobject *it)
 {
-	int len = it->it_index + 1;
+	Py_ssize_t len = it->it_index + 1;
 	if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
 		return 0;
 	return len;


More information about the Python-checkins mailing list