Use Py_ssize_t for counts and sizes.

This commit is contained in:
Martin v. Löwis 2006-02-16 14:30:23 +00:00
parent 97c65a8068
commit ad0a4629be
12 changed files with 67 additions and 66 deletions

View File

@ -6,10 +6,10 @@ Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru).
#include "Python.h"
static int
internal_bisect_right(PyObject *list, PyObject *item, int lo, int hi)
internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
{
PyObject *litem;
int mid, res;
Py_ssize_t mid, res;
if (hi == -1) {
hi = PySequence_Size(list);

View File

@ -9,10 +9,11 @@ annotated by Fran
#include "Python.h"
static int
_siftdown(PyListObject *heap, int startpos, int pos)
_siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
{
PyObject *newitem, *parent;
int cmp, parentpos;
int cmp;
Py_ssize_t parentpos;
assert(PyList_Check(heap));
if (pos >= PyList_GET_SIZE(heap)) {
@ -45,9 +46,9 @@ _siftdown(PyListObject *heap, int startpos, int pos)
}
static int
_siftup(PyListObject *heap, int pos)
_siftup(PyListObject *heap, Py_ssize_t pos)
{
int startpos, endpos, childpos, rightpos;
Py_ssize_t startpos, endpos, childpos, rightpos;
int cmp;
PyObject *newitem, *tmp;
@ -123,7 +124,7 @@ static PyObject *
heappop(PyObject *self, PyObject *heap)
{
PyObject *lastelt, *returnitem;
int n;
Py_ssize_t n;
if (!PyList_Check(heap)) {
PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
@ -197,7 +198,7 @@ this routine unless written as part of a conditional replacement:\n\n\
static PyObject *
heapify(PyObject *self, PyObject *heap)
{
int i, n;
Py_ssize_t i, n;
if (!PyList_Check(heap)) {
PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
@ -300,10 +301,11 @@ PyDoc_STRVAR(nlargest_doc,
Equivalent to: sorted(iterable, reverse=True)[:n]\n");
static int
_siftdownmax(PyListObject *heap, int startpos, int pos)
_siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
{
PyObject *newitem, *parent;
int cmp, parentpos;
int cmp;
Py_ssize_t parentpos;
assert(PyList_Check(heap));
if (pos >= PyList_GET_SIZE(heap)) {
@ -336,9 +338,9 @@ _siftdownmax(PyListObject *heap, int startpos, int pos)
}
static int
_siftupmax(PyListObject *heap, int pos)
_siftupmax(PyListObject *heap, Py_ssize_t pos)
{
int startpos, endpos, childpos, rightpos;
Py_ssize_t startpos, endpos, childpos, rightpos;
int cmp;
PyObject *newitem, *tmp;
@ -389,7 +391,7 @@ static PyObject *
nsmallest(PyObject *self, PyObject *args)
{
PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
int i, n;
Py_ssize_t i, n;
if (!PyArg_ParseTuple(args, "iO:nsmallest", &n, &iterable))
return NULL;

View File

@ -688,11 +688,11 @@ array_repeat(arrayobject *a, Py_ssize_t n)
}
static int
array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v)
array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
{
char *item;
int n; /* Size of replacement array */
int d; /* Change in size */
Py_ssize_t n; /* Size of replacement array */
Py_ssize_t d; /* Change in size */
#define b ((arrayobject *)v)
if (v == NULL)
n = 0;
@ -907,10 +907,7 @@ array_count(arrayobject *self, PyObject *v)
else if (cmp < 0)
return NULL;
}
if (i < LONG_MAX)
return PyInt_FromLong((long)count);
else
return PyLong_FromLong(count);
return PyInt_FromSsize_t(count);
}
PyDoc_STRVAR(count_doc,
@ -987,9 +984,9 @@ Remove the first occurence of x in the array.");
static PyObject *
array_pop(arrayobject *self, PyObject *args)
{
int i = -1;
Py_ssize_t i = -1;
PyObject *v;
if (!PyArg_ParseTuple(args, "|i:pop", &i))
if (!PyArg_ParseTuple(args, "|n:pop", &i))
return NULL;
if (self->ob_size == 0) {
/* Special-case most common failure cause */
@ -1196,7 +1193,7 @@ static PyObject *
array_fromfile(arrayobject *self, PyObject *args)
{
PyObject *f;
int n;
Py_ssize_t n;
FILE *fp;
if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
return NULL;
@ -1207,9 +1204,9 @@ array_fromfile(arrayobject *self, PyObject *args)
}
if (n > 0) {
char *item = self->ob_item;
int itemsize = self->ob_descr->itemsize;
Py_ssize_t itemsize = self->ob_descr->itemsize;
size_t nread;
int newlength;
Py_ssize_t newlength;
size_t newbytes;
/* Be careful here about overflow */
if ((newlength = self->ob_size + n) <= 0 ||
@ -1577,13 +1574,13 @@ static PyObject*
array_subscr(arrayobject* self, PyObject* item)
{
if (PyInt_Check(item)) {
long i = PyInt_AS_LONG(item);
Py_ssize_t i = PyInt_AS_LONG(item);
if (i < 0)
i += self->ob_size;
return array_item(self, i);
}
else if (PyLong_Check(item)) {
long i = PyLong_AsLong(item);
Py_ssize_t i = PyInt_AsSsize_t(item);
if (i == -1 && PyErr_Occurred())
return NULL;
if (i < 0)
@ -1631,13 +1628,13 @@ static int
array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
{
if (PyInt_Check(item)) {
long i = PyInt_AS_LONG(item);
Py_ssize_t i = PyInt_AS_LONG(item);
if (i < 0)
i += self->ob_size;
return array_ass_item(self, i, value);
}
else if (PyLong_Check(item)) {
long i = PyLong_AsLong(item);
Py_ssize_t i = PyInt_AsSsize_t(item);
if (i == -1 && PyErr_Occurred())
return -1;
if (i < 0)

View File

@ -129,7 +129,7 @@ IO_cgetval(PyObject *self) {
static PyObject *
IO_getval(IOobject *self, PyObject *args) {
PyObject *use_pos=Py_None;
int s;
Py_ssize_t s;
UNLESS (IO__opencheck(self)) return NULL;
UNLESS (PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
@ -156,7 +156,7 @@ PyDoc_STRVAR(IO_read__doc__,
static int
IO_cread(PyObject *self, char **output, Py_ssize_t n) {
int l;
Py_ssize_t l;
UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
@ -279,7 +279,7 @@ IO_tell(IOobject *self, PyObject *unused) {
UNLESS (IO__opencheck(self)) return NULL;
return PyInt_FromLong(self->pos);
return PyInt_FromSsize_t(self->pos);
}
PyDoc_STRVAR(IO_truncate__doc__,

View File

@ -489,7 +489,7 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
{
PyObject *old_value;
block *b;
int n, len=deque->len, halflen=(len+1)>>1, index=i;
Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
if (i < 0 || i >= len) {
PyErr_SetString(PyExc_IndexError,

View File

@ -1054,7 +1054,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *seq;
long start=0, stop=-1, step=1;
PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL;
int numargs;
Py_ssize_t numargs;
isliceobject *lz;
if (!_PyArg_NoKeywords("islice()", kwds))
@ -1378,7 +1378,7 @@ imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *it, *iters, *func;
imapobject *lz;
int numargs, i;
Py_ssize_t numargs, i;
if (!_PyArg_NoKeywords("imap()", kwds))
return NULL;
@ -1466,7 +1466,7 @@ imap_next(imapobject *lz)
PyObject *val;
PyObject *argtuple;
PyObject *result;
int numargs, i;
Py_ssize_t numargs, i;
numargs = PyTuple_Size(lz->iters);
argtuple = PyTuple_New(numargs);
@ -1547,7 +1547,7 @@ static PyTypeObject imap_type = {
typedef struct {
PyObject_HEAD
long tuplesize;
Py_ssize_t tuplesize;
long iternum; /* which iterator is active */
PyObject *ittuple; /* tuple of iterators */
} chainobject;
@ -1558,7 +1558,7 @@ static PyObject *
chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
chainobject *lz;
int tuplesize = PySequence_Length(args);
Py_ssize_t tuplesize = PySequence_Length(args);
int i;
PyObject *ittuple;
@ -2074,7 +2074,7 @@ static PyTypeObject count_type = {
typedef struct {
PyObject_HEAD
long tuplesize;
Py_ssize_t tuplesize;
PyObject *ittuple; /* tuple of iterators */
PyObject *result;
} izipobject;
@ -2088,7 +2088,7 @@ izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
int i;
PyObject *ittuple; /* tuple of iterators */
PyObject *result;
int tuplesize = PySequence_Length(args);
Py_ssize_t tuplesize = PySequence_Length(args);
if (!_PyArg_NoKeywords("izip()", kwds))
return NULL;
@ -2160,7 +2160,7 @@ static PyObject *
izip_next(izipobject *lz)
{
int i;
long tuplesize = lz->tuplesize;
Py_ssize_t tuplesize = lz->tuplesize;
PyObject *result = lz->result;
PyObject *it;
PyObject *item;

View File

@ -220,11 +220,11 @@ static PyObject *
mmap_read_method(mmap_object *self,
PyObject *args)
{
long num_bytes;
Py_ssize_t num_bytes;
PyObject *result;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "l:read", &num_bytes))
if (!PyArg_ParseTuple(args, "n:read", &num_bytes))
return(NULL);
/* silently 'adjust' out-of-range requests */
@ -240,7 +240,7 @@ static PyObject *
mmap_find_method(mmap_object *self,
PyObject *args)
{
long start = self->pos;
Py_ssize_t start = self->pos;
char *needle;
int len;
@ -468,10 +468,10 @@ mmap_tell_method(mmap_object *self, PyObject *args)
static PyObject *
mmap_flush_method(mmap_object *self, PyObject *args)
{
unsigned long offset = 0;
unsigned long size = self->size;
Py_ssize_t offset = 0;
Py_ssize_t size = self->size;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple (args, "|kk:flush", &offset, &size)) {
if (!PyArg_ParseTuple (args, "|nn:flush", &offset, &size)) {
return NULL;
} else if ((offset + size) > self->size) {
PyErr_SetString (PyExc_ValueError,
@ -1092,8 +1092,8 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
m_obj->map_handle = CreateFileMapping (m_obj->file_handle,
NULL,
flProtect,
0,
m_obj->size,
(DWORD)(m_obj->size >> 32),
(DWORD)(m_obj->size & 0xFFFFFFFF),
m_obj->tagname);
if (m_obj->map_handle != NULL) {
m_obj->data = (char *) MapViewOfFile (m_obj->map_handle,

View File

@ -296,7 +296,7 @@ spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
typedef struct {
PyObject_HEAD
int nitems;
Py_ssize_t nitems;
PyObject *item;
} itemgetterobject;
@ -307,7 +307,7 @@ itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
itemgetterobject *ig;
PyObject *item;
int nitems;
Py_ssize_t nitems;
if (!_PyArg_NoKeywords("itemgetter()", kwds))
return NULL;
@ -352,7 +352,7 @@ static PyObject *
itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
{
PyObject *obj, *result;
int i, nitems=ig->nitems;
Py_ssize_t i, nitems=ig->nitems;
if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
return NULL;
@ -435,7 +435,7 @@ static PyTypeObject itemgetter_type = {
typedef struct {
PyObject_HEAD
int nattrs;
Py_ssize_t nattrs;
PyObject *attr;
} attrgetterobject;
@ -446,7 +446,7 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
attrgetterobject *ag;
PyObject *attr;
int nattrs;
Py_ssize_t nattrs;
if (!_PyArg_NoKeywords("attrgetter()", kwds))
return NULL;
@ -491,7 +491,7 @@ static PyObject *
attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
{
PyObject *obj, *result;
int i, nattrs=ag->nattrs;
Py_ssize_t i, nattrs=ag->nattrs;
if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
return NULL;

View File

@ -782,7 +782,7 @@ build_node_tree(PyObject *tuple)
res = NULL;
}
if (res && encoding) {
int len;
Py_ssize_t len;
len = PyString_GET_SIZE(encoding) + 1;
res->n_str = (char *)PyMem_MALLOC(len);
if (res->n_str != NULL)

View File

@ -166,8 +166,8 @@ strop_joinfields(PyObject *self, PyObject *args)
{
PyObject *seq;
char *sep = NULL;
int seqlen, seplen = 0;
int i, reslen = 0, slen = 0, sz = 100;
Py_ssize_t seqlen, seplen = 0;
Py_ssize_t i, reslen = 0, slen = 0, sz = 100;
PyObject *res = NULL;
char* p = NULL;
ssizeargfunc getitemfunc;
@ -921,10 +921,11 @@ static PyObject *
strop_translate(PyObject *self, PyObject *args)
{
register char *input, *table, *output;
register int i, c, changed = 0;
Py_ssize_t i;
int c, changed = 0;
PyObject *input_obj;
char *table1, *output_start, *del_table=NULL;
int inlen, tablen, dellen = 0;
Py_ssize_t inlen, tablen, dellen = 0;
PyObject *result;
int trans_table[256];

View File

@ -1031,7 +1031,7 @@ struct_pack(PyObject *self, PyObject *args)
PyObject *format, *result, *v;
char *fmt;
int size, num;
int i, n;
Py_ssize_t i, n;
char *s, *res, *restart, *nres;
char c;
@ -1097,7 +1097,7 @@ struct_pack(PyObject *self, PyObject *args)
goto fail;
if (c == 's') {
/* num is string size, not repeat count */
int n;
Py_ssize_t n;
if (!PyString_Check(v)) {
PyErr_SetString(StructError,
"argument for 's' must be a string");
@ -1116,7 +1116,7 @@ struct_pack(PyObject *self, PyObject *args)
else if (c == 'p') {
/* num is string size + 1,
to fit in the count byte */
int n;
Py_ssize_t n;
num--; /* now num is max string size */
if (!PyString_Check(v)) {
PyErr_SetString(StructError,
@ -1133,7 +1133,8 @@ struct_pack(PyObject *self, PyObject *args)
memset(res+1+n, '\0', num-n);
if (n > 255)
n = 255;
*res++ = n; /* store the length byte */
/* store the length byte */
*res++ = Py_SAFE_DOWNCAST(n, Py_ssize_t, char);
res += num;
break;
}

View File

@ -408,7 +408,7 @@ zipimporter_get_data(PyObject *obj, PyObject *args)
char *p, buf[MAXPATHLEN + 1];
#endif
PyObject *toc_entry;
int len;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "s:zipimporter.get_data", &path))
return NULL;
@ -910,7 +910,7 @@ unmarshal_code(char *pathname, PyObject *data, time_t mtime)
{
PyObject *code;
char *buf = PyString_AsString(data);
int size = PyString_Size(data);
Py_ssize_t size = PyString_Size(data);
if (size <= 9) {
PyErr_SetString(ZipImportError,