Skip to content

Commit 452728f

Browse files
committed
Use Py_ssize_t for counts and sizes.
1 parent c1b0566 commit 452728f

12 files changed

Lines changed: 67 additions & 66 deletions

Modules/_bisectmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru).
66
#include "Python.h"
77

88
static int
9-
internal_bisect_right(PyObject *list, PyObject *item, int lo, int hi)
9+
internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
1010
{
1111
PyObject *litem;
12-
int mid, res;
12+
Py_ssize_t mid, res;
1313

1414
if (hi == -1) {
1515
hi = PySequence_Size(list);

Modules/_heapqmodule.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ which was written by Kevin O'Connor, augmented by Tim Peters,
99
#include "Python.h"
1010

1111
static int
12-
_siftdown(PyListObject *heap, int startpos, int pos)
12+
_siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
1313
{
1414
PyObject *newitem, *parent;
15-
int cmp, parentpos;
15+
int cmp;
16+
Py_ssize_t parentpos;
1617

1718
assert(PyList_Check(heap));
1819
if (pos >= PyList_GET_SIZE(heap)) {
@@ -45,9 +46,9 @@ _siftdown(PyListObject *heap, int startpos, int pos)
4546
}
4647

4748
static int
48-
_siftup(PyListObject *heap, int pos)
49+
_siftup(PyListObject *heap, Py_ssize_t pos)
4950
{
50-
int startpos, endpos, childpos, rightpos;
51+
Py_ssize_t startpos, endpos, childpos, rightpos;
5152
int cmp;
5253
PyObject *newitem, *tmp;
5354

@@ -123,7 +124,7 @@ static PyObject *
123124
heappop(PyObject *self, PyObject *heap)
124125
{
125126
PyObject *lastelt, *returnitem;
126-
int n;
127+
Py_ssize_t n;
127128

128129
if (!PyList_Check(heap)) {
129130
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\
197198
static PyObject *
198199
heapify(PyObject *self, PyObject *heap)
199200
{
200-
int i, n;
201+
Py_ssize_t i, n;
201202

202203
if (!PyList_Check(heap)) {
203204
PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
@@ -300,10 +301,11 @@ PyDoc_STRVAR(nlargest_doc,
300301
Equivalent to: sorted(iterable, reverse=True)[:n]\n");
301302

302303
static int
303-
_siftdownmax(PyListObject *heap, int startpos, int pos)
304+
_siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
304305
{
305306
PyObject *newitem, *parent;
306-
int cmp, parentpos;
307+
int cmp;
308+
Py_ssize_t parentpos;
307309

308310
assert(PyList_Check(heap));
309311
if (pos >= PyList_GET_SIZE(heap)) {
@@ -336,9 +338,9 @@ _siftdownmax(PyListObject *heap, int startpos, int pos)
336338
}
337339

338340
static int
339-
_siftupmax(PyListObject *heap, int pos)
341+
_siftupmax(PyListObject *heap, Py_ssize_t pos)
340342
{
341-
int startpos, endpos, childpos, rightpos;
343+
Py_ssize_t startpos, endpos, childpos, rightpos;
342344
int cmp;
343345
PyObject *newitem, *tmp;
344346

@@ -389,7 +391,7 @@ static PyObject *
389391
nsmallest(PyObject *self, PyObject *args)
390392
{
391393
PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
392-
int i, n;
394+
Py_ssize_t i, n;
393395

394396
if (!PyArg_ParseTuple(args, "iO:nsmallest", &n, &iterable))
395397
return NULL;

Modules/arraymodule.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,11 @@ array_repeat(arrayobject *a, Py_ssize_t n)
688688
}
689689

690690
static int
691-
array_ass_slice(arrayobject *a, int ilow, int ihigh, PyObject *v)
691+
array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
692692
{
693693
char *item;
694-
int n; /* Size of replacement array */
695-
int d; /* Change in size */
694+
Py_ssize_t n; /* Size of replacement array */
695+
Py_ssize_t d; /* Change in size */
696696
#define b ((arrayobject *)v)
697697
if (v == NULL)
698698
n = 0;
@@ -907,10 +907,7 @@ array_count(arrayobject *self, PyObject *v)
907907
else if (cmp < 0)
908908
return NULL;
909909
}
910-
if (i < LONG_MAX)
911-
return PyInt_FromLong((long)count);
912-
else
913-
return PyLong_FromLong(count);
910+
return PyInt_FromSsize_t(count);
914911
}
915912

916913
PyDoc_STRVAR(count_doc,
@@ -987,9 +984,9 @@ Remove the first occurence of x in the array.");
987984
static PyObject *
988985
array_pop(arrayobject *self, PyObject *args)
989986
{
990-
int i = -1;
987+
Py_ssize_t i = -1;
991988
PyObject *v;
992-
if (!PyArg_ParseTuple(args, "|i:pop", &i))
989+
if (!PyArg_ParseTuple(args, "|n:pop", &i))
993990
return NULL;
994991
if (self->ob_size == 0) {
995992
/* Special-case most common failure cause */
@@ -1196,7 +1193,7 @@ static PyObject *
11961193
array_fromfile(arrayobject *self, PyObject *args)
11971194
{
11981195
PyObject *f;
1199-
int n;
1196+
Py_ssize_t n;
12001197
FILE *fp;
12011198
if (!PyArg_ParseTuple(args, "Oi:fromfile", &f, &n))
12021199
return NULL;
@@ -1207,9 +1204,9 @@ array_fromfile(arrayobject *self, PyObject *args)
12071204
}
12081205
if (n > 0) {
12091206
char *item = self->ob_item;
1210-
int itemsize = self->ob_descr->itemsize;
1207+
Py_ssize_t itemsize = self->ob_descr->itemsize;
12111208
size_t nread;
1212-
int newlength;
1209+
Py_ssize_t newlength;
12131210
size_t newbytes;
12141211
/* Be careful here about overflow */
12151212
if ((newlength = self->ob_size + n) <= 0 ||
@@ -1577,13 +1574,13 @@ static PyObject*
15771574
array_subscr(arrayobject* self, PyObject* item)
15781575
{
15791576
if (PyInt_Check(item)) {
1580-
long i = PyInt_AS_LONG(item);
1577+
Py_ssize_t i = PyInt_AS_LONG(item);
15811578
if (i < 0)
15821579
i += self->ob_size;
15831580
return array_item(self, i);
15841581
}
15851582
else if (PyLong_Check(item)) {
1586-
long i = PyLong_AsLong(item);
1583+
Py_ssize_t i = PyInt_AsSsize_t(item);
15871584
if (i == -1 && PyErr_Occurred())
15881585
return NULL;
15891586
if (i < 0)
@@ -1631,13 +1628,13 @@ static int
16311628
array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
16321629
{
16331630
if (PyInt_Check(item)) {
1634-
long i = PyInt_AS_LONG(item);
1631+
Py_ssize_t i = PyInt_AS_LONG(item);
16351632
if (i < 0)
16361633
i += self->ob_size;
16371634
return array_ass_item(self, i, value);
16381635
}
16391636
else if (PyLong_Check(item)) {
1640-
long i = PyLong_AsLong(item);
1637+
Py_ssize_t i = PyInt_AsSsize_t(item);
16411638
if (i == -1 && PyErr_Occurred())
16421639
return -1;
16431640
if (i < 0)

Modules/cStringIO.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ IO_cgetval(PyObject *self) {
129129
static PyObject *
130130
IO_getval(IOobject *self, PyObject *args) {
131131
PyObject *use_pos=Py_None;
132-
int s;
132+
Py_ssize_t s;
133133

134134
UNLESS (IO__opencheck(self)) return NULL;
135135
UNLESS (PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
@@ -156,7 +156,7 @@ PyDoc_STRVAR(IO_read__doc__,
156156

157157
static int
158158
IO_cread(PyObject *self, char **output, Py_ssize_t n) {
159-
int l;
159+
Py_ssize_t l;
160160

161161
UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
162162
l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
@@ -279,7 +279,7 @@ IO_tell(IOobject *self, PyObject *unused) {
279279

280280
UNLESS (IO__opencheck(self)) return NULL;
281281

282-
return PyInt_FromLong(self->pos);
282+
return PyInt_FromSsize_t(self->pos);
283283
}
284284

285285
PyDoc_STRVAR(IO_truncate__doc__,

Modules/collectionsmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
489489
{
490490
PyObject *old_value;
491491
block *b;
492-
int n, len=deque->len, halflen=(len+1)>>1, index=i;
492+
Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
493493

494494
if (i < 0 || i >= len) {
495495
PyErr_SetString(PyExc_IndexError,

Modules/itertoolsmodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10541054
PyObject *seq;
10551055
long start=0, stop=-1, step=1;
10561056
PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL;
1057-
int numargs;
1057+
Py_ssize_t numargs;
10581058
isliceobject *lz;
10591059

10601060
if (!_PyArg_NoKeywords("islice()", kwds))
@@ -1378,7 +1378,7 @@ imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13781378
{
13791379
PyObject *it, *iters, *func;
13801380
imapobject *lz;
1381-
int numargs, i;
1381+
Py_ssize_t numargs, i;
13821382

13831383
if (!_PyArg_NoKeywords("imap()", kwds))
13841384
return NULL;
@@ -1466,7 +1466,7 @@ imap_next(imapobject *lz)
14661466
PyObject *val;
14671467
PyObject *argtuple;
14681468
PyObject *result;
1469-
int numargs, i;
1469+
Py_ssize_t numargs, i;
14701470

14711471
numargs = PyTuple_Size(lz->iters);
14721472
argtuple = PyTuple_New(numargs);
@@ -1547,7 +1547,7 @@ static PyTypeObject imap_type = {
15471547

15481548
typedef struct {
15491549
PyObject_HEAD
1550-
long tuplesize;
1550+
Py_ssize_t tuplesize;
15511551
long iternum; /* which iterator is active */
15521552
PyObject *ittuple; /* tuple of iterators */
15531553
} chainobject;
@@ -1558,7 +1558,7 @@ static PyObject *
15581558
chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15591559
{
15601560
chainobject *lz;
1561-
int tuplesize = PySequence_Length(args);
1561+
Py_ssize_t tuplesize = PySequence_Length(args);
15621562
int i;
15631563
PyObject *ittuple;
15641564

@@ -2074,7 +2074,7 @@ static PyTypeObject count_type = {
20742074

20752075
typedef struct {
20762076
PyObject_HEAD
2077-
long tuplesize;
2077+
Py_ssize_t tuplesize;
20782078
PyObject *ittuple; /* tuple of iterators */
20792079
PyObject *result;
20802080
} izipobject;
@@ -2088,7 +2088,7 @@ izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20882088
int i;
20892089
PyObject *ittuple; /* tuple of iterators */
20902090
PyObject *result;
2091-
int tuplesize = PySequence_Length(args);
2091+
Py_ssize_t tuplesize = PySequence_Length(args);
20922092

20932093
if (!_PyArg_NoKeywords("izip()", kwds))
20942094
return NULL;
@@ -2160,7 +2160,7 @@ static PyObject *
21602160
izip_next(izipobject *lz)
21612161
{
21622162
int i;
2163-
long tuplesize = lz->tuplesize;
2163+
Py_ssize_t tuplesize = lz->tuplesize;
21642164
PyObject *result = lz->result;
21652165
PyObject *it;
21662166
PyObject *item;

Modules/mmapmodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ static PyObject *
220220
mmap_read_method(mmap_object *self,
221221
PyObject *args)
222222
{
223-
long num_bytes;
223+
Py_ssize_t num_bytes;
224224
PyObject *result;
225225

226226
CHECK_VALID(NULL);
227-
if (!PyArg_ParseTuple(args, "l:read", &num_bytes))
227+
if (!PyArg_ParseTuple(args, "n:read", &num_bytes))
228228
return(NULL);
229229

230230
/* silently 'adjust' out-of-range requests */
@@ -240,7 +240,7 @@ static PyObject *
240240
mmap_find_method(mmap_object *self,
241241
PyObject *args)
242242
{
243-
long start = self->pos;
243+
Py_ssize_t start = self->pos;
244244
char *needle;
245245
int len;
246246

@@ -468,10 +468,10 @@ mmap_tell_method(mmap_object *self, PyObject *args)
468468
static PyObject *
469469
mmap_flush_method(mmap_object *self, PyObject *args)
470470
{
471-
unsigned long offset = 0;
472-
unsigned long size = self->size;
471+
Py_ssize_t offset = 0;
472+
Py_ssize_t size = self->size;
473473
CHECK_VALID(NULL);
474-
if (!PyArg_ParseTuple (args, "|kk:flush", &offset, &size)) {
474+
if (!PyArg_ParseTuple (args, "|nn:flush", &offset, &size)) {
475475
return NULL;
476476
} else if ((offset + size) > self->size) {
477477
PyErr_SetString (PyExc_ValueError,
@@ -1092,8 +1092,8 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
10921092
m_obj->map_handle = CreateFileMapping (m_obj->file_handle,
10931093
NULL,
10941094
flProtect,
1095-
0,
1096-
m_obj->size,
1095+
(DWORD)(m_obj->size >> 32),
1096+
(DWORD)(m_obj->size & 0xFFFFFFFF),
10971097
m_obj->tagname);
10981098
if (m_obj->map_handle != NULL) {
10991099
m_obj->data = (char *) MapViewOfFile (m_obj->map_handle,

Modules/operator.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
296296

297297
typedef struct {
298298
PyObject_HEAD
299-
int nitems;
299+
Py_ssize_t nitems;
300300
PyObject *item;
301301
} itemgetterobject;
302302

@@ -307,7 +307,7 @@ itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
307307
{
308308
itemgetterobject *ig;
309309
PyObject *item;
310-
int nitems;
310+
Py_ssize_t nitems;
311311

312312
if (!_PyArg_NoKeywords("itemgetter()", kwds))
313313
return NULL;
@@ -352,7 +352,7 @@ static PyObject *
352352
itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
353353
{
354354
PyObject *obj, *result;
355-
int i, nitems=ig->nitems;
355+
Py_ssize_t i, nitems=ig->nitems;
356356

357357
if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
358358
return NULL;
@@ -435,7 +435,7 @@ static PyTypeObject itemgetter_type = {
435435

436436
typedef struct {
437437
PyObject_HEAD
438-
int nattrs;
438+
Py_ssize_t nattrs;
439439
PyObject *attr;
440440
} attrgetterobject;
441441

@@ -446,7 +446,7 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
446446
{
447447
attrgetterobject *ag;
448448
PyObject *attr;
449-
int nattrs;
449+
Py_ssize_t nattrs;
450450

451451
if (!_PyArg_NoKeywords("attrgetter()", kwds))
452452
return NULL;
@@ -491,7 +491,7 @@ static PyObject *
491491
attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
492492
{
493493
PyObject *obj, *result;
494-
int i, nattrs=ag->nattrs;
494+
Py_ssize_t i, nattrs=ag->nattrs;
495495

496496
if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
497497
return NULL;

0 commit comments

Comments
 (0)