Skip to content

Commit eb079f1

Browse files
committed
Use Py_ssize_t for counts and sizes.
Convert Py_ssize_t using PyInt_FromSsize_t
1 parent ad0a462 commit eb079f1

10 files changed

Lines changed: 33 additions & 31 deletions

File tree

Objects/abstract.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ PySequence_Repeat(PyObject *o, Py_ssize_t count)
11701170
to nb_multiply if o appears to be a sequence. */
11711171
if (PySequence_Check(o)) {
11721172
PyObject *n, *result;
1173-
n = PyInt_FromLong(count);
1173+
n = PyInt_FromSsize_t(count);
11741174
if (n == NULL)
11751175
return NULL;
11761176
result = binary_op1(o, n, NB_SLOT(nb_multiply));
@@ -1222,7 +1222,7 @@ PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
12221222

12231223
if (PySequence_Check(o)) {
12241224
PyObject *n, *result;
1225-
n = PyInt_FromLong(count);
1225+
n = PyInt_FromSsize_t(count);
12261226
if (n == NULL)
12271227
return NULL;
12281228
result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),

Objects/enumobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ PyTypeObject PyEnum_Type = {
159159

160160
typedef struct {
161161
PyObject_HEAD
162-
long index;
162+
Py_ssize_t index;
163163
PyObject* seq;
164164
} reversedobject;
165165

166166
static PyObject *
167167
reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
168168
{
169-
long n;
169+
Py_ssize_t n;
170170
PyObject *seq;
171171
reversedobject *ro;
172172

@@ -249,7 +249,7 @@ reversed_len(reversedobject *ro)
249249
if (seqsize == -1)
250250
return NULL;
251251
position = ro->index + 1;
252-
return PyInt_FromLong((seqsize < position) ? 0 : position);
252+
return PyInt_FromSsize_t((seqsize < position) ? 0 : position);
253253
}
254254

255255
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");

Objects/iterobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ iter_len(seqiterobject *it)
8282
return NULL;
8383
len = seqsize - it->it_index;
8484
if (len >= 0)
85-
return PyInt_FromLong(len);
85+
return PyInt_FromSsize_t(len);
8686
}
8787
return PyInt_FromLong(0);
8888
}

Objects/listobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,13 +2504,13 @@ static int
25042504
list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
25052505
{
25062506
if (PyInt_Check(item)) {
2507-
long i = PyInt_AS_LONG(item);
2507+
Py_ssize_t i = PyInt_AS_LONG(item);
25082508
if (i < 0)
25092509
i += PyList_GET_SIZE(self);
25102510
return list_ass_item(self, i, value);
25112511
}
25122512
else if (PyLong_Check(item)) {
2513-
long i = PyLong_AsLong(item);
2513+
Py_ssize_t i = PyInt_AsSsize_t(item);
25142514
if (i == -1 && PyErr_Occurred())
25152515
return -1;
25162516
if (i < 0)
@@ -2818,7 +2818,7 @@ PyTypeObject PyListIter_Type = {
28182818

28192819
typedef struct {
28202820
PyObject_HEAD
2821-
long it_index;
2821+
Py_ssize_t it_index;
28222822
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
28232823
} listreviterobject;
28242824

@@ -2860,7 +2860,7 @@ static PyObject *
28602860
listreviter_next(listreviterobject *it)
28612861
{
28622862
PyObject *item;
2863-
long index = it->it_index;
2863+
Py_ssize_t index = it->it_index;
28642864
PyListObject *seq = it->it_seq;
28652865

28662866
if (index>=0 && index < PyList_GET_SIZE(seq)) {

Objects/methodobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
6565
PyCFunctionObject* f = (PyCFunctionObject*)func;
6666
PyCFunction meth = PyCFunction_GET_FUNCTION(func);
6767
PyObject *self = PyCFunction_GET_SELF(func);
68-
long size;
68+
Py_ssize_t size;
6969

7070
switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) {
7171
case METH_VARARGS:
@@ -81,7 +81,7 @@ PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
8181
if (size == 0)
8282
return (*meth)(self, NULL);
8383
PyErr_Format(PyExc_TypeError,
84-
"%.200s() takes no arguments (%ld given)",
84+
"%.200s() takes no arguments (%zd given)",
8585
f->m_ml->ml_name, size);
8686
return NULL;
8787
}
@@ -92,7 +92,7 @@ PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw)
9292
if (size == 1)
9393
return (*meth)(self, PyTuple_GET_ITEM(arg, 0));
9494
PyErr_Format(PyExc_TypeError,
95-
"%.200s() takes exactly one argument (%ld given)",
95+
"%.200s() takes exactly one argument (%zd given)",
9696
f->m_ml->ml_name, size);
9797
return NULL;
9898
}

Objects/stringobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ string_contains(PyObject *a, PyObject *el)
10301030
const char *sub = PyString_AS_STRING(el);
10311031
char *last;
10321032
Py_ssize_t len_sub = PyString_GET_SIZE(el);
1033-
int shortsub;
1033+
Py_ssize_t shortsub;
10341034
char firstchar, lastchar;
10351035

10361036
if (!PyString_CheckExact(el)) {
@@ -2942,11 +2942,11 @@ PyDoc_STRVAR(zfill__doc__,
29422942
static PyObject *
29432943
string_zfill(PyStringObject *self, PyObject *args)
29442944
{
2945-
long fill;
2945+
Py_ssize_t fill;
29462946
PyObject *s;
29472947
char *p;
29482948

2949-
int width;
2949+
long width;
29502950
if (!PyArg_ParseTuple(args, "l:zfill", &width))
29512951
return NULL;
29522952

@@ -3886,7 +3886,7 @@ PyObject *
38863886
PyString_Format(PyObject *format, PyObject *args)
38873887
{
38883888
char *fmt, *res;
3889-
int arglen, argidx;
3889+
Py_ssize_t arglen, argidx;
38903890
Py_ssize_t reslen, rescnt, fmtcnt;
38913891
int args_owned = 0;
38923892
PyObject *result, *orig_args;
@@ -4294,7 +4294,7 @@ PyString_Format(PyObject *format, PyObject *args)
42944294
/* Fiddle args right (remove the first argidx arguments) */
42954295
if (PyTuple_Check(orig_args) && argidx > 0) {
42964296
PyObject *v;
4297-
int n = PyTuple_GET_SIZE(orig_args) - argidx;
4297+
Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx;
42984298
v = PyTuple_New(n);
42994299
if (v == NULL)
43004300
goto error;

Objects/structseq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ structseq_reduce(PyStructSequence* self)
247247
PyObject* tup;
248248
PyObject* dict;
249249
PyObject* result;
250-
long n_fields, n_visible_fields, n_unnamed_fields;
250+
Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields;
251251
int i;
252252

253253
n_fields = REAL_SIZE(self);

Objects/tupleobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ tuplehash(PyTupleObject *v)
278278
if (y == -1)
279279
return -1;
280280
x = (x ^ y) * mult;
281-
mult += 82520L + len + len;
281+
/* the cast might truncate len; that doesn't change hash stability */
282+
mult += (long)(82520L + len + len);
282283
}
283284
x += 97531L;
284285
if (x == -1)
@@ -850,10 +851,10 @@ tupleiter_next(tupleiterobject *it)
850851
static PyObject *
851852
tupleiter_len(tupleiterobject *it)
852853
{
853-
long len = 0;
854+
Py_ssize_t len = 0;
854855
if (it->it_seq)
855856
len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
856-
return PyInt_FromLong(len);
857+
return PyInt_FromSsize_t(len);
857858
}
858859

859860
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");

Objects/typeobject.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,11 +1561,11 @@ valid_identifier(PyObject *s)
15611561
/* Replace Unicode objects in slots. */
15621562

15631563
static PyObject *
1564-
_unicode_to_string(PyObject *slots, int nslots)
1564+
_unicode_to_string(PyObject *slots, Py_ssize_t nslots)
15651565
{
15661566
PyObject *tmp = slots;
15671567
PyObject *o, *o1;
1568-
int i;
1568+
Py_ssize_t i;
15691569
ssizessizeargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
15701570
for (i = 0; i < nslots; i++) {
15711571
if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
@@ -2428,7 +2428,7 @@ static int
24282428
same_slots_added(PyTypeObject *a, PyTypeObject *b)
24292429
{
24302430
PyTypeObject *base = a->tp_base;
2431-
int size;
2431+
Py_ssize_t size;
24322432

24332433
if (base != b->tp_base)
24342434
return 0;
@@ -2904,7 +2904,7 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp)
29042904
static void
29052905
inherit_special(PyTypeObject *type, PyTypeObject *base)
29062906
{
2907-
int oldsize, newsize;
2907+
Py_ssize_t oldsize, newsize;
29082908

29092909
/* Special flag magic */
29102910
if (!type->tp_as_buffer && base->tp_as_buffer) {
@@ -3316,7 +3316,8 @@ PyType_Ready(PyTypeObject *type)
33163316
static int
33173317
add_subclass(PyTypeObject *base, PyTypeObject *type)
33183318
{
3319-
int i;
3319+
Py_ssize_t i;
3320+
int result;
33203321
PyObject *list, *ref, *new;
33213322

33223323
list = base->tp_subclasses;
@@ -3334,9 +3335,9 @@ add_subclass(PyTypeObject *base, PyTypeObject *type)
33343335
if (PyWeakref_GET_OBJECT(ref) == Py_None)
33353336
return PyList_SetItem(list, i, new);
33363337
}
3337-
i = PyList_Append(list, new);
3338+
result = PyList_Append(list, new);
33383339
Py_DECREF(new);
3339-
return i;
3340+
return result;
33403341
}
33413342

33423343
static void
@@ -4160,7 +4161,7 @@ slot_sq_item(PyObject *self, Py_ssize_t i)
41604161
return NULL;
41614162
}
41624163
}
4163-
ival = PyInt_FromLong(i);
4164+
ival = PyInt_FromSsize_t(i);
41644165
if (ival != NULL) {
41654166
args = PyTuple_New(1);
41664167
if (args != NULL) {

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6541,7 +6541,7 @@ unicode_buffer_getsegcount(PyUnicodeObject *self,
65416541
return 1;
65426542
}
65436543

6544-
static int
6544+
static Py_ssize_t
65456545
unicode_buffer_getcharbuf(PyUnicodeObject *self,
65466546
Py_ssize_t index,
65476547
const void **ptr)

0 commit comments

Comments
 (0)