Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.

This commit is contained in:
Raymond Hettinger 2008-07-24 00:08:18 +00:00
parent 8c81fdad33
commit 33fcf9db74
1 changed files with 24 additions and 23 deletions

View File

@ -52,20 +52,20 @@ typedef struct BLOCK {
} block; } block;
#define MAXFREEBLOCKS 10 #define MAXFREEBLOCKS 10
static int numfreeblocks = 0; static Py_ssize_t numfreeblocks = 0;
static block *freeblocks[MAXFREEBLOCKS]; static block *freeblocks[MAXFREEBLOCKS];
static block * static block *
newblock(block *leftlink, block *rightlink, int len) { newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
block *b; block *b;
/* To prevent len from overflowing INT_MAX on 64-bit machines, we /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
* refuse to allocate new blocks if the current len is dangerously * refuse to allocate new blocks if the current len is dangerously
* close. There is some extra margin to prevent spurious arithmetic * close. There is some extra margin to prevent spurious arithmetic
* overflows at various places. The following check ensures that * overflows at various places. The following check ensures that
* the blocks allocated to the deque, in the worst case, can only * the blocks allocated to the deque, in the worst case, can only
* have INT_MAX-2 entries in total. * have PY_SSIZE_T_MAX-2 entries in total.
*/ */
if (len >= INT_MAX - 2*BLOCKLEN) { if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"cannot add more blocks to the deque"); "cannot add more blocks to the deque");
return NULL; return NULL;
@ -100,10 +100,10 @@ typedef struct {
PyObject_HEAD PyObject_HEAD
block *leftblock; block *leftblock;
block *rightblock; block *rightblock;
int leftindex; /* in range(BLOCKLEN) */ Py_ssize_t leftindex; /* in range(BLOCKLEN) */
int rightindex; /* in range(BLOCKLEN) */ Py_ssize_t rightindex; /* in range(BLOCKLEN) */
int len; Py_ssize_t len;
int maxlen; Py_ssize_t maxlen;
long state; /* incremented whenever the indices move */ long state; /* incremented whenever the indices move */
PyObject *weakreflist; /* List of weak references */ PyObject *weakreflist; /* List of weak references */
} dequeobject; } dequeobject;
@ -355,7 +355,7 @@ PyDoc_STRVAR(extendleft_doc,
static int static int
_deque_rotate(dequeobject *deque, Py_ssize_t n) _deque_rotate(dequeobject *deque, Py_ssize_t n)
{ {
int i, len=deque->len, halflen=(len+1)>>1; Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
PyObject *item, *rv; PyObject *item, *rv;
if (len == 0) if (len == 0)
@ -392,7 +392,7 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n)
static PyObject * static PyObject *
deque_rotate(dequeobject *deque, PyObject *args) deque_rotate(dequeobject *deque, PyObject *args)
{ {
int n=1; Py_ssize_t n=1;
if (!PyArg_ParseTuple(args, "|i:rotate", &n)) if (!PyArg_ParseTuple(args, "|i:rotate", &n))
return NULL; return NULL;
@ -462,11 +462,11 @@ deque_clear(dequeobject *deque)
} }
static PyObject * static PyObject *
deque_item(dequeobject *deque, int i) deque_item(dequeobject *deque, Py_ssize_t i)
{ {
block *b; block *b;
PyObject *item; PyObject *item;
int n, index=i; Py_ssize_t n, index=i;
if (i < 0 || i >= deque->len) { if (i < 0 || i >= deque->len) {
PyErr_SetString(PyExc_IndexError, PyErr_SetString(PyExc_IndexError,
@ -591,11 +591,11 @@ deque_traverse(dequeobject *deque, visitproc visit, void *arg)
{ {
block *b; block *b;
PyObject *item; PyObject *item;
int index; Py_ssize_t index;
int indexlo = deque->leftindex; Py_ssize_t indexlo = deque->leftindex;
for (b = deque->leftblock; b != NULL; b = b->rightlink) { for (b = deque->leftblock; b != NULL; b = b->rightlink) {
const int indexhi = b == deque->rightblock ? const Py_ssize_t indexhi = b == deque->rightblock ?
deque->rightindex : deque->rightindex :
BLOCKLEN - 1; BLOCKLEN - 1;
@ -637,12 +637,12 @@ deque_reduce(dequeobject *deque)
if (deque->maxlen == -1) if (deque->maxlen == -1)
result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist); result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
else else
result = Py_BuildValue("O(Oi)", Py_TYPE(deque), aslist, deque->maxlen); result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
} else { } else {
if (deque->maxlen == -1) if (deque->maxlen == -1)
result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict); result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
else else
result = Py_BuildValue("O(Oi)O", Py_TYPE(deque), aslist, deque->maxlen, dict); result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
} }
Py_XDECREF(dict); Py_XDECREF(dict);
Py_DECREF(aslist); Py_DECREF(aslist);
@ -742,7 +742,8 @@ static PyObject *
deque_richcompare(PyObject *v, PyObject *w, int op) deque_richcompare(PyObject *v, PyObject *w, int op)
{ {
PyObject *it1=NULL, *it2=NULL, *x, *y; PyObject *it1=NULL, *it2=NULL, *x, *y;
int b, vs, ws, cmp=-1; Py_ssize_t vs, ws;
int b, cmp=-1;
if (!PyObject_TypeCheck(v, &deque_type) || if (!PyObject_TypeCheck(v, &deque_type) ||
!PyObject_TypeCheck(w, &deque_type)) { !PyObject_TypeCheck(w, &deque_type)) {
@ -821,13 +822,13 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
{ {
PyObject *iterable = NULL; PyObject *iterable = NULL;
PyObject *maxlenobj = NULL; PyObject *maxlenobj = NULL;
int maxlen = -1; Py_ssize_t maxlen = -1;
char *kwlist[] = {"iterable", "maxlen", 0}; char *kwlist[] = {"iterable", "maxlen", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj)) if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
return -1; return -1;
if (maxlenobj != NULL && maxlenobj != Py_None) { if (maxlenobj != NULL && maxlenobj != Py_None) {
maxlen = PyInt_AsLong(maxlenobj); maxlen = PyInt_AsSsize_t(maxlenobj);
if (maxlen == -1 && PyErr_Occurred()) if (maxlen == -1 && PyErr_Occurred())
return -1; return -1;
if (maxlen < 0) { if (maxlen < 0) {
@ -943,11 +944,11 @@ static PyTypeObject deque_type = {
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
int index; Py_ssize_t index;
block *b; block *b;
dequeobject *deque; dequeobject *deque;
long state; /* state when the iterator is created */ long state; /* state when the iterator is created */
int counter; /* number of items remaining for iteration */ Py_ssize_t counter; /* number of items remaining for iteration */
} dequeiterobject; } dequeiterobject;
static PyTypeObject dequeiter_type; static PyTypeObject dequeiter_type;