diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2006-02-15 17:27:45 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2006-02-15 17:27:45 (GMT) |
commit | 18e165558b24d29e7e0ca501842b9236589b012a (patch) | |
tree | 841678b5dc1aff3aa48701fee33a6ba7be00a72b /Objects | |
parent | 44829297348d9121a03fc7df2fac557b583cc7fa (diff) | |
download | cpython-18e165558b24d29e7e0ca501842b9236589b012a.zip cpython-18e165558b24d29e7e0ca501842b9236589b012a.tar.gz cpython-18e165558b24d29e7e0ca501842b9236589b012a.tar.bz2 |
Merge ssize_t branch.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 86 | ||||
-rw-r--r-- | Objects/bufferobject.c | 124 | ||||
-rw-r--r-- | Objects/classobject.c | 48 | ||||
-rw-r--r-- | Objects/codeobject.c | 6 | ||||
-rw-r--r-- | Objects/complexobject.c | 4 | ||||
-rw-r--r-- | Objects/descrobject.c | 8 | ||||
-rw-r--r-- | Objects/dictobject.c | 32 | ||||
-rw-r--r-- | Objects/enumobject.c | 2 | ||||
-rw-r--r-- | Objects/fileobject.c | 33 | ||||
-rw-r--r-- | Objects/floatobject.c | 4 | ||||
-rw-r--r-- | Objects/frameobject.c | 22 | ||||
-rw-r--r-- | Objects/funcobject.c | 16 | ||||
-rw-r--r-- | Objects/intobject.c | 71 | ||||
-rw-r--r-- | Objects/iterobject.c | 2 | ||||
-rw-r--r-- | Objects/listobject.c | 274 | ||||
-rw-r--r-- | Objects/longobject.c | 188 | ||||
-rw-r--r-- | Objects/methodobject.c | 6 | ||||
-rw-r--r-- | Objects/moduleobject.c | 2 | ||||
-rw-r--r-- | Objects/object.c | 23 | ||||
-rw-r--r-- | Objects/obmalloc.c | 11 | ||||
-rw-r--r-- | Objects/rangeobject.c | 14 | ||||
-rw-r--r-- | Objects/setobject.c | 37 | ||||
-rw-r--r-- | Objects/sliceobject.c | 13 | ||||
-rw-r--r-- | Objects/stringobject.c | 313 | ||||
-rw-r--r-- | Objects/structseq.c | 28 | ||||
-rw-r--r-- | Objects/tupleobject.c | 92 | ||||
-rw-r--r-- | Objects/typeobject.c | 164 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 586 | ||||
-rw-r--r-- | Objects/weakrefobject.c | 18 |
29 files changed, 1205 insertions, 1022 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index b794974..3fe7a7e 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -56,7 +56,7 @@ PyObject_Type(PyObject *o) return v; } -int +Py_ssize_t PyObject_Size(PyObject *o) { PySequenceMethods *m; @@ -74,17 +74,17 @@ PyObject_Size(PyObject *o) } #undef PyObject_Length -int +Py_ssize_t PyObject_Length(PyObject *o) { return PyObject_Size(o); } #define PyObject_Length PyObject_Size -int +Py_ssize_t _PyObject_LengthHint(PyObject *o) { - int rv = PyObject_Size(o); + Py_ssize_t rv = PyObject_Size(o); if (rv != -1) return rv; if (PyErr_ExceptionMatches(PyExc_TypeError) || @@ -94,7 +94,7 @@ _PyObject_LengthHint(PyObject *o) PyErr_Fetch(&err_type, &err_value, &err_tb); ro = PyObject_CallMethod(o, "__length_hint__", NULL); if (ro != NULL) { - rv = (int)PyInt_AsLong(ro); + rv = PyInt_AsLong(ro); Py_DECREF(ro); Py_XDECREF(err_type); Py_XDECREF(err_value); @@ -218,11 +218,11 @@ PyObject_DelItemString(PyObject *o, char *key) int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, - int *buffer_len) + Py_ssize_t *buffer_len) { PyBufferProcs *pb; const char *pp; - int len; + Py_ssize_t len; if (obj == NULL || buffer == NULL || buffer_len == NULL) { null_error(); @@ -264,11 +264,11 @@ PyObject_CheckReadBuffer(PyObject *obj) int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, - int *buffer_len) + Py_ssize_t *buffer_len) { PyBufferProcs *pb; void *pp; - int len; + Py_ssize_t len; if (obj == NULL || buffer == NULL || buffer_len == NULL) { null_error(); @@ -297,11 +297,11 @@ int PyObject_AsReadBuffer(PyObject *obj, int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, - int *buffer_len) + Py_ssize_t *buffer_len) { PyBufferProcs *pb; void*pp; - int len; + Py_ssize_t len; if (obj == NULL || buffer == NULL || buffer_len == NULL) { null_error(); @@ -645,7 +645,7 @@ PyNumber_Add(PyObject *v, PyObject *w) } static PyObject * -sequence_repeat(intargfunc repeatfunc, PyObject *seq, PyObject *n) +sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n) { long count; if (PyInt_Check(n)) { @@ -839,7 +839,7 @@ PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply), NB_SLOT(nb_multiply)); if (result == Py_NotImplemented) { - intargfunc f = NULL; + ssizeargfunc f = NULL; PySequenceMethods *mv = v->ob_type->tp_as_sequence; PySequenceMethods *mw = w->ob_type->tp_as_sequence; Py_DECREF(result); @@ -943,7 +943,7 @@ PyNumber_Absolute(PyObject *o) /* Add a check for embedded NULL-bytes in the argument. */ static PyObject * -int_from_string(const char *s, int len) +int_from_string(const char *s, Py_ssize_t len) { char *end; PyObject *x; @@ -965,7 +965,7 @@ PyNumber_Int(PyObject *o) { PyNumberMethods *m; const char *buffer; - int buffer_len; + Py_ssize_t buffer_len; if (o == NULL) return null_error(); @@ -1006,7 +1006,7 @@ PyNumber_Int(PyObject *o) /* Add a check for embedded NULL-bytes in the argument. */ static PyObject * -long_from_string(const char *s, int len) +long_from_string(const char *s, Py_ssize_t len) { char *end; PyObject *x; @@ -1028,7 +1028,7 @@ PyNumber_Long(PyObject *o) { PyNumberMethods *m; const char *buffer; - int buffer_len; + Py_ssize_t buffer_len; if (o == NULL) return null_error(); @@ -1103,7 +1103,7 @@ PySequence_Check(PyObject *s) s->ob_type->tp_as_sequence->sq_item != NULL; } -int +Py_ssize_t PySequence_Size(PyObject *s) { PySequenceMethods *m; @@ -1122,7 +1122,7 @@ PySequence_Size(PyObject *s) } #undef PySequence_Length -int +Py_ssize_t PySequence_Length(PyObject *s) { return PySequence_Size(s); @@ -1154,7 +1154,7 @@ PySequence_Concat(PyObject *s, PyObject *o) } PyObject * -PySequence_Repeat(PyObject *o, int count) +PySequence_Repeat(PyObject *o, Py_ssize_t count) { PySequenceMethods *m; @@ -1207,7 +1207,7 @@ PySequence_InPlaceConcat(PyObject *s, PyObject *o) } PyObject * -PySequence_InPlaceRepeat(PyObject *o, int count) +PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) { PySequenceMethods *m; @@ -1236,7 +1236,7 @@ PySequence_InPlaceRepeat(PyObject *o, int count) } PyObject * -PySequence_GetItem(PyObject *s, int i) +PySequence_GetItem(PyObject *s, Py_ssize_t i) { PySequenceMethods *m; @@ -1247,7 +1247,7 @@ PySequence_GetItem(PyObject *s, int i) if (m && m->sq_item) { if (i < 0) { if (m->sq_length) { - int l = (*m->sq_length)(s); + Py_ssize_t l = (*m->sq_length)(s); if (l < 0) return NULL; i += l; @@ -1260,7 +1260,7 @@ PySequence_GetItem(PyObject *s, int i) } static PyObject * -sliceobj_from_intint(int i, int j) +sliceobj_from_intint(Py_ssize_t i, Py_ssize_t j) { PyObject *start, *end, *slice; start = PyInt_FromLong((long)i); @@ -1278,7 +1278,7 @@ sliceobj_from_intint(int i, int j) } PyObject * -PySequence_GetSlice(PyObject *s, int i1, int i2) +PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2) { PySequenceMethods *m; PyMappingMethods *mp; @@ -1289,7 +1289,7 @@ PySequence_GetSlice(PyObject *s, int i1, int i2) if (m && m->sq_slice) { if (i1 < 0 || i2 < 0) { if (m->sq_length) { - int l = (*m->sq_length)(s); + Py_ssize_t l = (*m->sq_length)(s); if (l < 0) return NULL; if (i1 < 0) @@ -1313,7 +1313,7 @@ PySequence_GetSlice(PyObject *s, int i1, int i2) } int -PySequence_SetItem(PyObject *s, int i, PyObject *o) +PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o) { PySequenceMethods *m; @@ -1326,7 +1326,7 @@ PySequence_SetItem(PyObject *s, int i, PyObject *o) if (m && m->sq_ass_item) { if (i < 0) { if (m->sq_length) { - int l = (*m->sq_length)(s); + Py_ssize_t l = (*m->sq_length)(s); if (l < 0) return -1; i += l; @@ -1340,7 +1340,7 @@ PySequence_SetItem(PyObject *s, int i, PyObject *o) } int -PySequence_DelItem(PyObject *s, int i) +PySequence_DelItem(PyObject *s, Py_ssize_t i) { PySequenceMethods *m; @@ -1353,7 +1353,7 @@ PySequence_DelItem(PyObject *s, int i) if (m && m->sq_ass_item) { if (i < 0) { if (m->sq_length) { - int l = (*m->sq_length)(s); + Py_ssize_t l = (*m->sq_length)(s); if (l < 0) return -1; i += l; @@ -1367,7 +1367,7 @@ PySequence_DelItem(PyObject *s, int i) } int -PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o) +PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o) { PySequenceMethods *m; PyMappingMethods *mp; @@ -1381,7 +1381,7 @@ PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o) if (m && m->sq_ass_slice) { if (i1 < 0 || i2 < 0) { if (m->sq_length) { - int l = (*m->sq_length)(s); + Py_ssize_t l = (*m->sq_length)(s); if (l < 0) return -1; if (i1 < 0) @@ -1406,7 +1406,7 @@ PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o) } int -PySequence_DelSlice(PyObject *s, int i1, int i2) +PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2) { PySequenceMethods *m; @@ -1419,7 +1419,7 @@ PySequence_DelSlice(PyObject *s, int i1, int i2) if (m && m->sq_ass_slice) { if (i1 < 0 || i2 < 0) { if (m->sq_length) { - int l = (*m->sq_length)(s); + Py_ssize_t l = (*m->sq_length)(s); if (l < 0) return -1; if (i1 < 0) @@ -1438,9 +1438,9 @@ PyObject * PySequence_Tuple(PyObject *v) { PyObject *it; /* iter(v) */ - int n; /* guess for result tuple size */ + Py_ssize_t n; /* guess for result tuple size */ PyObject *result; - int j; + Py_ssize_t j; if (v == NULL) return null_error(); @@ -1486,7 +1486,7 @@ PySequence_Tuple(PyObject *v) break; } if (j >= n) { - int oldn = n; + Py_ssize_t oldn = n; /* The over-allocation strategy can grow a bit faster than for lists because unlike lists the over-allocation isn't permanent -- we reclaim @@ -1708,7 +1708,7 @@ PyMapping_Check(PyObject *o) o->ob_type->tp_as_sequence->sq_slice); } -int +Py_ssize_t PyMapping_Size(PyObject *o) { PyMappingMethods *m; @@ -1727,7 +1727,7 @@ PyMapping_Size(PyObject *o) } #undef PyMapping_Length -int +Py_ssize_t PyMapping_Length(PyObject *o) { return PyMapping_Size(o); @@ -2053,7 +2053,7 @@ static int abstract_issubclass(PyObject *derived, PyObject *cls) { PyObject *bases; - int i, n; + Py_ssize_t i, n; int r = 0; @@ -2137,7 +2137,7 @@ recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth) } } else if (PyTuple_Check(cls)) { - int i, n; + Py_ssize_t i, n; if (!recursion_depth) { PyErr_SetString(PyExc_RuntimeError, @@ -2191,8 +2191,8 @@ recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth) return -1; if (PyTuple_Check(cls)) { - int i; - int n = PyTuple_GET_SIZE(cls); + Py_ssize_t i; + Py_ssize_t n = PyTuple_GET_SIZE(cls); if (!recursion_depth) { PyErr_SetString(PyExc_RuntimeError, diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c index 56748e6..4a9e3ae 100644 --- a/Objects/bufferobject.c +++ b/Objects/bufferobject.c @@ -8,15 +8,15 @@ typedef struct { PyObject_HEAD PyObject *b_base; void *b_ptr; - int b_size; - int b_offset; + Py_ssize_t b_size; + Py_ssize_t b_offset; int b_readonly; long b_hash; } PyBufferObject; static int -get_buf(PyBufferObject *self, void **ptr, int *size) +get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size) { if (self->b_base == NULL) { assert (ptr != NULL); @@ -24,8 +24,8 @@ get_buf(PyBufferObject *self, void **ptr, int *size) *size = self->b_size; } else { - int count, offset; - getreadbufferproc proc; + Py_ssize_t count, offset; + readbufferproc proc; PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer; if ((*bp->bf_getsegcount)(self->b_base, NULL) != 1) { PyErr_SetString(PyExc_TypeError, @@ -35,7 +35,7 @@ get_buf(PyBufferObject *self, void **ptr, int *size) if (self->b_readonly) proc = bp->bf_getreadbuffer; else - proc = (getreadbufferproc)bp->bf_getwritebuffer; + proc = (readbufferproc)bp->bf_getwritebuffer; if ((count = (*proc)(self->b_base, 0, ptr)) < 0) return 0; /* apply constraints to the start/end */ @@ -56,7 +56,7 @@ get_buf(PyBufferObject *self, void **ptr, int *size) static PyObject * -buffer_from_memory(PyObject *base, int size, int offset, void *ptr, +buffer_from_memory(PyObject *base, Py_ssize_t size, Py_ssize_t offset, void *ptr, int readonly) { PyBufferObject * b; @@ -88,7 +88,7 @@ buffer_from_memory(PyObject *base, int size, int offset, void *ptr, } static PyObject * -buffer_from_object(PyObject *base, int size, int offset, int readonly) +buffer_from_object(PyObject *base, Py_ssize_t size, Py_ssize_t offset, int readonly) { if (offset < 0) { PyErr_SetString(PyExc_ValueError, @@ -99,7 +99,7 @@ buffer_from_object(PyObject *base, int size, int offset, int readonly) /* another buffer, refer to the base object */ PyBufferObject *b = (PyBufferObject *)base; if (b->b_size != Py_END_OF_BUFFER) { - int base_size = b->b_size - offset; + Py_ssize_t base_size = b->b_size - offset; if (base_size < 0) base_size = 0; if (size == Py_END_OF_BUFFER || size > base_size) @@ -113,7 +113,7 @@ buffer_from_object(PyObject *base, int size, int offset, int readonly) PyObject * -PyBuffer_FromObject(PyObject *base, int offset, int size) +PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size) { PyBufferProcs *pb = base->ob_type->tp_as_buffer; @@ -129,7 +129,7 @@ PyBuffer_FromObject(PyObject *base, int offset, int size) } PyObject * -PyBuffer_FromReadWriteObject(PyObject *base, int offset, int size) +PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size) { PyBufferProcs *pb = base->ob_type->tp_as_buffer; @@ -145,19 +145,19 @@ PyBuffer_FromReadWriteObject(PyObject *base, int offset, int size) } PyObject * -PyBuffer_FromMemory(void *ptr, int size) +PyBuffer_FromMemory(void *ptr, Py_ssize_t size) { return buffer_from_memory(NULL, size, 0, ptr, 1); } PyObject * -PyBuffer_FromReadWriteMemory(void *ptr, int size) +PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size) { return buffer_from_memory(NULL, size, 0, ptr, 0); } PyObject * -PyBuffer_New(int size) +PyBuffer_New(Py_ssize_t size) { PyObject *o; PyBufferObject * b; @@ -167,6 +167,7 @@ PyBuffer_New(int size) "size must be zero or positive"); return NULL; } + /* XXX: check for overflow in multiply */ /* Inline PyObject_New */ o = PyObject_MALLOC(sizeof(*b) + size); if ( o == NULL ) @@ -189,13 +190,13 @@ static PyObject * buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw) { PyObject *ob; - int offset = 0; - int size = Py_END_OF_BUFFER; + Py_ssize_t offset = 0; + Py_ssize_t size = Py_END_OF_BUFFER; if (!_PyArg_NoKeywords("buffer()", kw)) return NULL; - if (!PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size)) + if (!PyArg_ParseTuple(args, "O|ll:buffer", &ob, &offset, &size)) return NULL; return PyBuffer_FromObject(ob, offset, size); } @@ -220,7 +221,8 @@ static int buffer_compare(PyBufferObject *self, PyBufferObject *other) { void *p1, *p2; - int len_self, len_other, min_len, cmp; + Py_ssize_t len_self, len_other, min_len; + int cmp; if (!get_buf(self, &p1, &len_self)) return -1; @@ -238,17 +240,17 @@ buffer_compare(PyBufferObject *self, PyBufferObject *other) static PyObject * buffer_repr(PyBufferObject *self) { - char *status = self->b_readonly ? "read-only" : "read-write"; + const char *status = self->b_readonly ? "read-only" : "read-write"; if ( self->b_base == NULL ) - return PyString_FromFormat("<%s buffer ptr %p, size %d at %p>", + return PyString_FromFormat("<%s buffer ptr %p, size %ld at %p>", status, self->b_ptr, self->b_size, self); else return PyString_FromFormat( - "<%s buffer for %p, size %d, offset %d at %p>", + "<%s buffer for %p, size %ld, offset %ld at %p>", status, self->b_base, self->b_size, @@ -260,8 +262,8 @@ static long buffer_hash(PyBufferObject *self) { void *ptr; - int size; - register int len; + Py_ssize_t size; + register Py_ssize_t len; register unsigned char *p; register long x; @@ -300,7 +302,7 @@ static PyObject * buffer_str(PyBufferObject *self) { void *ptr; - int size; + Py_ssize_t size; if (!get_buf(self, &ptr, &size)) return NULL; return PyString_FromStringAndSize(ptr, size); @@ -308,11 +310,11 @@ buffer_str(PyBufferObject *self) /* Sequence methods */ -static int +static Py_ssize_t buffer_length(PyBufferObject *self) { void *ptr; - int size; + Py_ssize_t size; if (!get_buf(self, &ptr, &size)) return -1; return size; @@ -325,7 +327,7 @@ buffer_concat(PyBufferObject *self, PyObject *other) void *ptr1, *ptr2; char *p; PyObject *ob; - int size, count; + Py_ssize_t size, count; if ( pb == NULL || pb->bf_getreadbuffer == NULL || @@ -369,12 +371,12 @@ buffer_concat(PyBufferObject *self, PyObject *other) } static PyObject * -buffer_repeat(PyBufferObject *self, int count) +buffer_repeat(PyBufferObject *self, Py_ssize_t count) { PyObject *ob; register char *p; void *ptr; - int size; + Py_ssize_t size; if ( count < 0 ) count = 0; @@ -398,10 +400,10 @@ buffer_repeat(PyBufferObject *self, int count) } static PyObject * -buffer_item(PyBufferObject *self, int idx) +buffer_item(PyBufferObject *self, Py_ssize_t idx) { void *ptr; - int size; + Py_ssize_t size; if (!get_buf(self, &ptr, &size)) return NULL; if ( idx < 0 || idx >= size ) { @@ -412,10 +414,10 @@ buffer_item(PyBufferObject *self, int idx) } static PyObject * -buffer_slice(PyBufferObject *self, int left, int right) +buffer_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right) { void *ptr; - int size; + Py_ssize_t size; if (!get_buf(self, &ptr, &size)) return NULL; if ( left < 0 ) @@ -431,12 +433,12 @@ buffer_slice(PyBufferObject *self, int left, int right) } static int -buffer_ass_item(PyBufferObject *self, int idx, PyObject *other) +buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other) { PyBufferProcs *pb; void *ptr1, *ptr2; - int size; - int count; + Py_ssize_t size; + Py_ssize_t count; if ( self->b_readonly ) { PyErr_SetString(PyExc_TypeError, @@ -482,13 +484,13 @@ buffer_ass_item(PyBufferObject *self, int idx, PyObject *other) } static int -buffer_ass_slice(PyBufferObject *self, int left, int right, PyObject *other) +buffer_ass_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right, PyObject *other) { PyBufferProcs *pb; void *ptr1, *ptr2; - int size; - int slice_len; - int count; + Py_ssize_t size; + Py_ssize_t slice_len; + Py_ssize_t count; if ( self->b_readonly ) { PyErr_SetString(PyExc_TypeError, @@ -541,10 +543,10 @@ buffer_ass_slice(PyBufferObject *self, int left, int right, PyObject *other) /* Buffer methods */ -static int -buffer_getreadbuf(PyBufferObject *self, int idx, void **pp) +static Py_ssize_t +buffer_getreadbuf(PyBufferObject *self, Py_ssize_t idx, void **pp) { - int size; + Py_ssize_t size; if ( idx != 0 ) { PyErr_SetString(PyExc_SystemError, "accessing non-existent buffer segment"); @@ -555,8 +557,8 @@ buffer_getreadbuf(PyBufferObject *self, int idx, void **pp) return size; } -static int -buffer_getwritebuf(PyBufferObject *self, int idx, void **pp) +static Py_ssize_t +buffer_getwritebuf(PyBufferObject *self, Py_ssize_t idx, void **pp) { if ( self->b_readonly ) { @@ -566,11 +568,11 @@ buffer_getwritebuf(PyBufferObject *self, int idx, void **pp) return buffer_getreadbuf(self, idx, pp); } -static int -buffer_getsegcount(PyBufferObject *self, int *lenp) +static Py_ssize_t +buffer_getsegcount(PyBufferObject *self, Py_ssize_t *lenp) { void *ptr; - int size; + Py_ssize_t size; if (!get_buf(self, &ptr, &size)) return -1; if (lenp) @@ -578,11 +580,11 @@ buffer_getsegcount(PyBufferObject *self, int *lenp) return 1; } -static int -buffer_getcharbuf(PyBufferObject *self, int idx, const char **pp) +static Py_ssize_t +buffer_getcharbuf(PyBufferObject *self, Py_ssize_t idx, const char **pp) { void *ptr; - int size; + Py_ssize_t size; if ( idx != 0 ) { PyErr_SetString(PyExc_SystemError, "accessing non-existent buffer segment"); @@ -596,20 +598,20 @@ buffer_getcharbuf(PyBufferObject *self, int idx, const char **pp) static PySequenceMethods buffer_as_sequence = { - (inquiry)buffer_length, /*sq_length*/ + (lenfunc)buffer_length, /*sq_length*/ (binaryfunc)buffer_concat, /*sq_concat*/ - (intargfunc)buffer_repeat, /*sq_repeat*/ - (intargfunc)buffer_item, /*sq_item*/ - (intintargfunc)buffer_slice, /*sq_slice*/ - (intobjargproc)buffer_ass_item, /*sq_ass_item*/ - (intintobjargproc)buffer_ass_slice, /*sq_ass_slice*/ + (ssizeargfunc)buffer_repeat, /*sq_repeat*/ + (ssizeargfunc)buffer_item, /*sq_item*/ + (ssizessizeargfunc)buffer_slice, /*sq_slice*/ + (ssizeobjargproc)buffer_ass_item, /*sq_ass_item*/ + (ssizessizeobjargproc)buffer_ass_slice, /*sq_ass_slice*/ }; static PyBufferProcs buffer_as_buffer = { - (getreadbufferproc)buffer_getreadbuf, - (getwritebufferproc)buffer_getwritebuf, - (getsegcountproc)buffer_getsegcount, - (getcharbufferproc)buffer_getcharbuf, + (readbufferproc)buffer_getreadbuf, + (writebufferproc)buffer_getwritebuf, + (segcountproc)buffer_getsegcount, + (charbufferproc)buffer_getcharbuf, }; PyTypeObject PyBuffer_Type = { diff --git a/Objects/classobject.c b/Objects/classobject.c index c8057e2..7d975ec 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -68,7 +68,7 @@ PyClass_New(PyObject *bases, PyObject *dict, PyObject *name) return NULL; } else { - int i, n; + Py_ssize_t i, n; PyObject *base; if (!PyTuple_Check(bases)) { PyErr_SetString(PyExc_TypeError, @@ -185,7 +185,7 @@ class_dealloc(PyClassObject *op) static PyObject * class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass) { - int i, n; + Py_ssize_t i, n; PyObject *value = PyDict_GetItem(cp->cl_dict, name); if (value != NULL) { *pclass = cp; @@ -281,7 +281,7 @@ set_dict(PyClassObject *c, PyObject *v) static char * set_bases(PyClassObject *c, PyObject *v) { - int i, n; + Py_ssize_t i, n; if (v == NULL || !PyTuple_Check(v)) return "__bases__ must be a tuple object"; @@ -483,7 +483,7 @@ PyTypeObject PyClass_Type = { int PyClass_IsSubclass(PyObject *class, PyObject *base) { - int i, n; + Py_ssize_t i, n; PyClassObject *cp; if (class == base) return 1; @@ -996,12 +996,12 @@ instance_traverse(PyInstanceObject *o, visitproc visit, void *arg) static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr; static PyObject *iterstr, *nextstr; -static int +static Py_ssize_t instance_length(PyInstanceObject *inst) { PyObject *func; PyObject *res; - int outcome; + Py_ssize_t outcome; if (lenstr == NULL) lenstr = PyString_InternFromString("__len__"); @@ -1013,9 +1013,13 @@ instance_length(PyInstanceObject *inst) if (res == NULL) return -1; if (PyInt_Check(res)) { - long temp = PyInt_AsLong(res); - outcome = (int)temp; -#if SIZEOF_INT < SIZEOF_LONG + Py_ssize_t temp = PyInt_AsSsize_t(res); + if (temp == -1 && PyErr_Occurred()) { + Py_DECREF(res); + return -1; + } + outcome = (Py_ssize_t)temp; +#if SIZEOF_SIZE_T < SIZEOF_LONG /* Overflow check -- range of PyInt is more than C int */ if (outcome != temp) { PyErr_SetString(PyExc_OverflowError, @@ -1097,13 +1101,13 @@ instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value) } static PyMappingMethods instance_as_mapping = { - (inquiry)instance_length, /* mp_length */ + (lenfunc)instance_length, /* mp_length */ (binaryfunc)instance_subscript, /* mp_subscript */ (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */ }; static PyObject * -instance_item(PyInstanceObject *inst, int i) +instance_item(PyInstanceObject *inst, Py_ssize_t i) { PyObject *func, *arg, *res; @@ -1112,7 +1116,7 @@ instance_item(PyInstanceObject *inst, int i) func = instance_getattr(inst, getitemstr); if (func == NULL) return NULL; - arg = Py_BuildValue("(i)", i); + arg = Py_BuildValue("(n)", i); if (arg == NULL) { Py_DECREF(func); return NULL; @@ -1124,7 +1128,7 @@ instance_item(PyInstanceObject *inst, int i) } static PyObject * -sliceobj_from_intint(int i, int j) +sliceobj_from_intint(Py_ssize_t i, Py_ssize_t j) { PyObject *start, *end, *res; @@ -1145,7 +1149,7 @@ sliceobj_from_intint(int i, int j) static PyObject * -instance_slice(PyInstanceObject *inst, int i, int j) +instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j) { PyObject *func, *arg, *res; static PyObject *getslicestr; @@ -1179,7 +1183,7 @@ instance_slice(PyInstanceObject *inst, int i, int j) } static int -instance_ass_item(PyInstanceObject *inst, int i, PyObject *item) +instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item) { PyObject *func, *arg, *res; @@ -1213,7 +1217,7 @@ instance_ass_item(PyInstanceObject *inst, int i, PyObject *item) } static int -instance_ass_slice(PyInstanceObject *inst, int i, int j, PyObject *value) +instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value) { PyObject *func, *arg, *res; static PyObject *setslicestr, *delslicestr; @@ -1322,13 +1326,13 @@ instance_contains(PyInstanceObject *inst, PyObject *member) static PySequenceMethods instance_as_sequence = { - (inquiry)instance_length, /* sq_length */ + (lenfunc)instance_length, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ - (intargfunc)instance_item, /* sq_item */ - (intintargfunc)instance_slice, /* sq_slice */ - (intobjargproc)instance_ass_item, /* sq_ass_item */ - (intintobjargproc)instance_ass_slice, /* sq_ass_slice */ + (ssizeargfunc)instance_item, /* sq_item */ + (ssizessizeargfunc)instance_slice, /* sq_slice */ + (ssizeobjargproc)instance_ass_item, /* sq_ass_item */ + (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */ (objobjproc)instance_contains, /* sq_contains */ }; @@ -2430,7 +2434,7 @@ instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw) Py_INCREF(arg); } else { - int argcount = PyTuple_Size(arg); + Py_ssize_t argcount = PyTuple_Size(arg); PyObject *newarg = PyTuple_New(argcount + 1); int i; if (newarg == NULL) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index c5ddfd5..f832911 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -28,7 +28,7 @@ all_name_chars(unsigned char *s) static void intern_strings(PyObject *tuple) { - int i; + Py_ssize_t i; for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { PyObject *v = PyTuple_GET_ITEM(tuple, i); @@ -48,7 +48,7 @@ PyCode_New(int argcount, int nlocals, int stacksize, int flags, PyObject *lnotab) { PyCodeObject *co; - int i; + Py_ssize_t i; /* Check argument types */ if (argcount < 0 || nlocals < 0 || code == NULL || @@ -135,7 +135,7 @@ validate_and_copy_tuple(PyObject *tup) { PyObject *newtuple; PyObject *item; - int i, len; + Py_ssize_t i, len; len = PyTuple_GET_SIZE(tup); newtuple = PyTuple_New(len); diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 08c8c89..f29a90f 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -680,7 +680,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v) #ifdef Py_USING_UNICODE char s_buffer[256]; #endif - int len; + Py_ssize_t len; if (PyString_Check(v)) { s = PyString_AS_STRING(v); @@ -699,7 +699,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v) NULL)) return NULL; s = s_buffer; - len = (int)strlen(s); + len = strlen(s); } #endif else if (PyObject_AsCharBuffer(v, &s, &len)) { diff --git a/Objects/descrobject.c b/Objects/descrobject.c index f763832..6972a69 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -209,7 +209,7 @@ getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value) static PyObject * methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds) { - int argc; + Py_ssize_t argc; PyObject *self, *func, *result; /* Make sure that the first argument is acceptable as 'self' */ @@ -267,7 +267,7 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args, static PyObject * wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds) { - int argc; + Py_ssize_t argc; PyObject *self, *func, *result; /* Make sure that the first argument is acceptable as 'self' */ @@ -669,7 +669,7 @@ typedef struct { PyObject *dict; } proxyobject; -static int +static Py_ssize_t proxy_len(proxyobject *pp) { return PyObject_Size(pp->dict); @@ -682,7 +682,7 @@ proxy_getitem(proxyobject *pp, PyObject *key) } static PyMappingMethods proxy_as_mapping = { - (inquiry)proxy_len, /* mp_length */ + (lenfunc)proxy_len, /* mp_length */ (binaryfunc)proxy_getitem, /* mp_subscript */ 0, /* mp_ass_subscript */ }; diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 1deb26c..bacb705 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -217,8 +217,8 @@ reported by this function, and outstanding exceptions are maintained. static dictentry * lookdict(dictobject *mp, PyObject *key, register long hash) { - register int i; - register unsigned int perturb; + register Py_ssize_t i; + register size_t perturb; register dictentry *freeslot; register unsigned int mask = mp->ma_mask; dictentry *ep0 = mp->ma_table; @@ -328,8 +328,8 @@ Done: static dictentry * lookdict_string(dictobject *mp, PyObject *key, register long hash) { - register int i; - register unsigned int perturb; + register Py_ssize_t i; + register size_t perturb; register dictentry *freeslot; register unsigned int mask = mp->ma_mask; dictentry *ep0 = mp->ma_table; @@ -690,9 +690,10 @@ PyDict_Clear(PyObject *op) * delete keys), via PyDict_SetItem(). */ int -PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue) +PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue) { - register int i, mask; + register Py_ssize_t i; + register int mask; register dictentry *ep; if (!PyDict_Check(op)) @@ -786,7 +787,7 @@ dict_print(register dictobject *mp, register FILE *fp, register int flags) static PyObject * dict_repr(dictobject *mp) { - int i; + Py_ssize_t i; PyObject *s, *temp, *colon = NULL; PyObject *pieces = NULL, *result = NULL; PyObject *key, *value; @@ -862,7 +863,7 @@ Done: return result; } -static int +static Py_ssize_t dict_length(dictobject *mp) { return mp->ma_used; @@ -898,7 +899,7 @@ dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w) } static PyMappingMethods dict_as_mapping = { - (inquiry)dict_length, /*mp_length*/ + (lenfunc)dict_length, /*mp_length*/ (binaryfunc)dict_subscript, /*mp_subscript*/ (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ }; @@ -1109,7 +1110,7 @@ int PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) { PyObject *it; /* iter(seq2) */ - int i; /* index into seq2 of current element */ + int i; /* index into seq2 of current element */ PyObject *item; /* seq2[i] */ PyObject *fast; /* item as a 2-tuple or 2-list */ @@ -1123,7 +1124,7 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) for (i = 0; ; ++i) { PyObject *key, *value; - int n; + Py_ssize_t n; fast = NULL; item = PyIter_Next(it); @@ -1147,7 +1148,7 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) if (n != 2) { PyErr_Format(PyExc_ValueError, "dictionary update sequence element #%d " - "has length %d; 2 is required", + "has length %ld; 2 is required", i, n); goto Fail; } @@ -1300,7 +1301,7 @@ PyDict_Copy(PyObject *o) return NULL; } -int +Py_ssize_t PyDict_Size(PyObject *mp) { if (mp == NULL || !PyDict_Check(mp)) { @@ -1708,7 +1709,8 @@ dict_popitem(dictobject *mp) static int dict_traverse(PyObject *op, visitproc visit, void *arg) { - int i = 0, err; + Py_ssize_t i = 0; + int err; PyObject *pk; PyObject *pv; @@ -2057,7 +2059,7 @@ dictiter_dealloc(dictiterobject *di) static PyObject * dictiter_len(dictiterobject *di) { - int len = 0; + long len = 0; if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) len = di->len; return PyInt_FromLong(len); diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 7a5d1a1..e6e5bc5 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -241,7 +241,7 @@ PyDoc_STRVAR(reversed_doc, static PyObject * reversed_len(reversedobject *ro) { - int position, seqsize; + Py_ssize_t position, seqsize; if (ro->seq == NULL) return PyInt_FromLong(0); diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 6faabc3..e4fffdd 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -1,5 +1,6 @@ /* File object implementation */ +#define PY_SSIZE_T_CLEAN #include "Python.h" #include "structmember.h" @@ -869,8 +870,8 @@ static PyObject * file_readinto(PyFileObject *f, PyObject *args) { char *ptr; - int ntodo; - size_t ndone, nnow; + Py_ssize_t ntodo; + Py_ssize_t ndone, nnow; if (f->f_fp == NULL) return err_closed(); @@ -988,7 +989,8 @@ getline_via_fgets(FILE *fp) pvend = buf + total_v_size; nfree = pvend - pvfree; memset(pvfree, '\n', nfree); - p = fgets(pvfree, nfree, fp); + assert(nfree < INT_MAX); /* Should be atmost MAXBUFSIZE */ + p = fgets(pvfree, (int)nfree, fp); Py_END_ALLOW_THREADS if (p == NULL) { @@ -1062,7 +1064,8 @@ getline_via_fgets(FILE *fp) pvend = BUF(v) + total_v_size; nfree = pvend - pvfree; memset(pvfree, '\n', nfree); - p = fgets(pvfree, nfree, fp); + assert(nfree < INT_MAX); + p = fgets(pvfree, (int)nfree, fp); Py_END_ALLOW_THREADS if (p == NULL) { @@ -1271,7 +1274,7 @@ PyFile_GetLine(PyObject *f, int n) if (n < 0 && result != NULL && PyString_Check(result)) { char *s = PyString_AS_STRING(result); - int len = PyString_GET_SIZE(result); + Py_ssize_t len = PyString_GET_SIZE(result); if (len == 0) { Py_DECREF(result); result = NULL; @@ -1292,7 +1295,7 @@ PyFile_GetLine(PyObject *f, int n) #ifdef Py_USING_UNICODE if (n < 0 && result != NULL && PyUnicode_Check(result)) { Py_UNICODE *s = PyUnicode_AS_UNICODE(result); - int len = PyUnicode_GET_SIZE(result); + Py_ssize_t len = PyUnicode_GET_SIZE(result); if (len == 0) { Py_DECREF(result); result = NULL; @@ -1468,7 +1471,7 @@ static PyObject * file_write(PyFileObject *f, PyObject *args) { char *s; - int n, n2; + Py_ssize_t n, n2; if (f->f_fp == NULL) return err_closed(); if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n)) @@ -1494,7 +1497,8 @@ file_writelines(PyFileObject *f, PyObject *seq) PyObject *list, *line; PyObject *it; /* iter(seq) */ PyObject *result; - int i, j, index, len, nwritten, islist; + int index, islist; + Py_ssize_t i, j, nwritten, len; assert(seq != NULL); if (f->f_fp == NULL) @@ -1552,7 +1556,6 @@ file_writelines(PyFileObject *f, PyObject *seq) PyObject *v = PyList_GET_ITEM(list, i); if (!PyString_Check(v)) { const char *buffer; - int len; if (((f->f_binary && PyObject_AsReadBuffer(v, (const void**)&buffer, @@ -1789,7 +1792,7 @@ drop_readahead(PyFileObject *f) static int readahead(PyFileObject *f, int bufsize) { - int chunksize; + Py_ssize_t chunksize; if (f->f_buf != NULL) { if( (f->f_bufend - f->f_bufptr) >= 1) @@ -1829,7 +1832,7 @@ readahead_get_line_skip(PyFileObject *f, int skip, int bufsize) PyStringObject* s; char *bufptr; char *buf; - int len; + Py_ssize_t len; if (f->f_buf == NULL) if (readahead(f, bufsize) < 0) @@ -1855,8 +1858,9 @@ readahead_get_line_skip(PyFileObject *f, int skip, int bufsize) bufptr = f->f_bufptr; buf = f->f_buf; f->f_buf = NULL; /* Force new readahead buffer */ + assert(skip+len < INT_MAX); s = readahead_get_line_skip( - f, skip+len, bufsize + (bufsize>>2) ); + f, (int)(skip+len), bufsize + (bufsize>>2) ); if (s == NULL) { PyMem_Free(buf); return NULL; @@ -2061,7 +2065,7 @@ PyTypeObject PyFile_Type = { int PyFile_SoftSpace(PyObject *f, int newflag) { - int oldflag = 0; + long oldflag = 0; if (f == NULL) { /* Do nothing */ } @@ -2077,6 +2081,7 @@ PyFile_SoftSpace(PyObject *f, int newflag) else { if (PyInt_Check(v)) oldflag = PyInt_AsLong(v); + assert(oldflag < INT_MAX); Py_DECREF(v); } v = PyInt_FromLong((long)newflag); @@ -2088,7 +2093,7 @@ PyFile_SoftSpace(PyObject *f, int newflag) Py_DECREF(v); } } - return oldflag; + return (int)oldflag; } /* Interfaces to write objects/strings to file-like objects */ diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 86c2ba3..e1e063b 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -87,7 +87,7 @@ PyFloat_FromString(PyObject *v, char **pend) #ifdef Py_USING_UNICODE char s_buffer[256]; /* for objects convertible to a char buffer */ #endif - int len; + Py_ssize_t len; if (pend) *pend = NULL; @@ -108,7 +108,7 @@ PyFloat_FromString(PyObject *v, char **pend) NULL)) return NULL; s = s_buffer; - len = (int)strlen(s); + len = strlen(s); } #endif else if (PyObject_AsCharBuffer(v, &s, &len)) { diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 3535544..6e3f297 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -71,9 +71,9 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno) int new_lasti = 0; /* The new value of f_lasti */ int new_iblock = 0; /* The new value of f_iblock */ char *code = NULL; /* The bytecode for the frame... */ - int code_len = 0; /* ...and its length */ + Py_ssize_t code_len = 0; /* ...and its length */ char *lnotab = NULL; /* Iterating over co_lnotab */ - int lnotab_len = 0; /* (ditto) */ + Py_ssize_t lnotab_len = 0; /* (ditto) */ int offset = 0; /* (ditto) */ int line = 0; /* (ditto) */ int addr = 0; /* (ditto) */ @@ -540,7 +540,7 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals, PyFrameObject *back = tstate->frame; PyFrameObject *f; PyObject *builtins; - int extras, ncells, nfrees, i; + Py_ssize_t extras, ncells, nfrees, i; #ifdef Py_DEBUG if (code == NULL || globals == NULL || !PyDict_Check(globals) || @@ -678,10 +678,10 @@ PyFrame_BlockPop(PyFrameObject *f) /* Convert between "fast" version of locals and dictionary version */ static void -map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values, - int deref) +map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values, + Py_ssize_t deref) { - int j; + Py_ssize_t j; for (j = nmap; --j >= 0; ) { PyObject *key = PyTuple_GET_ITEM(map, j); PyObject *value = values[j]; @@ -699,10 +699,10 @@ map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values, } static void -dict_to_map(PyObject *map, int nmap, PyObject *dict, PyObject **values, - int deref, int clear) +dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values, + Py_ssize_t deref, int clear) { - int j; + Py_ssize_t j; for (j = nmap; --j >= 0; ) { PyObject *key = PyTuple_GET_ITEM(map, j); PyObject *value = PyObject_GetItem(dict, key); @@ -733,7 +733,7 @@ PyFrame_FastToLocals(PyFrameObject *f) PyObject *locals, *map; PyObject **fast; PyObject *error_type, *error_value, *error_traceback; - int j; + Py_ssize_t j; if (f == NULL) return; locals = f->f_locals; @@ -776,7 +776,7 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear) PyObject *locals, *map; PyObject **fast; PyObject *error_type, *error_value, *error_traceback; - int j; + Py_ssize_t j; if (f == NULL) return; locals = f->f_locals; diff --git a/Objects/funcobject.c b/Objects/funcobject.c index d0e3a25..4abf51a 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -232,7 +232,7 @@ static int func_set_code(PyFunctionObject *op, PyObject *value) { PyObject *tmp; - int nfree, nclosure; + Py_ssize_t nfree, nclosure; if (restricted()) return -1; @@ -248,8 +248,8 @@ func_set_code(PyFunctionObject *op, PyObject *value) PyTuple_GET_SIZE(op->func_closure)); if (nclosure != nfree) { PyErr_Format(PyExc_ValueError, - "%s() requires a code object with %d free vars," - " not %d", + "%s() requires a code object with %ld free vars," + " not %ld", PyString_AsString(op->func_name), nclosure, nfree); return -1; @@ -363,7 +363,7 @@ func_new(PyTypeObject* type, PyObject* args, PyObject* kw) PyObject *defaults = Py_None; PyObject *closure = Py_None; PyFunctionObject *newfunc; - int nfree, nclosure; + Py_ssize_t nfree, nclosure; static const char *kwlist[] = {"code", "globals", "name", "argdefs", "closure", 0}; @@ -401,11 +401,11 @@ func_new(PyTypeObject* type, PyObject* args, PyObject* kw) nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); if (nfree != nclosure) return PyErr_Format(PyExc_ValueError, - "%s requires closure of length %d, not %d", + "%s requires closure of length %ld, not %ld", PyString_AS_STRING(code->co_name), nfree, nclosure); if (nclosure) { - int i; + Py_ssize_t i; for (i = 0; i < nclosure; i++) { PyObject *o = PyTuple_GET_ITEM(closure, i); if (!PyCell_Check(o)) { @@ -516,7 +516,7 @@ function_call(PyObject *func, PyObject *arg, PyObject *kw) PyObject *result; PyObject *argdefs; PyObject **d, **k; - int nk, nd; + Py_ssize_t nk, nd; argdefs = PyFunction_GET_DEFAULTS(func); if (argdefs != NULL && PyTuple_Check(argdefs)) { @@ -529,7 +529,7 @@ function_call(PyObject *func, PyObject *arg, PyObject *kw) } if (kw != NULL && PyDict_Check(kw)) { - int pos, i; + Py_ssize_t pos, i; nk = PyDict_Size(kw); k = PyMem_NEW(PyObject *, 2*nk); if (k == NULL) { diff --git a/Objects/intobject.c b/Objects/intobject.c index 5a0b259..d1b9599 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -108,6 +108,22 @@ PyInt_FromLong(long ival) return (PyObject *) v; } +PyObject * +PyInt_FromSize_t(size_t ival) +{ + if (ival <= LONG_MAX) + return PyInt_FromLong((long)ival); + return _PyLong_FromSize_t(ival); +} + +PyObject * +PyInt_FromSsize_t(Py_ssize_t ival) +{ + if (ival >= LONG_MIN && ival <= LONG_MAX) + return PyInt_FromLong((long)ival); + return _PyLong_FromSsize_t(ival); +} + static void int_dealloc(PyIntObject *v) { @@ -169,6 +185,59 @@ PyInt_AsLong(register PyObject *op) return val; } +Py_ssize_t +PyInt_AsSsize_t(register PyObject *op) +{ + PyNumberMethods *nb; + PyIntObject *io; + Py_ssize_t val; + if (op && !PyInt_CheckExact(op) && PyLong_Check(op)) + return _PyLong_AsSsize_t(op); +#if SIZEOF_SIZE_T==SIZEOF_LONG + return PyInt_AsLong(op); +#else + + if (op && PyInt_Check(op)) + return PyInt_AS_LONG((PyIntObject*) op); + + if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || + (nb->nb_int == NULL && nb->nb_long == 0)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return -1; + } + + if (nb->nb_long != 0) { + io = (PyIntObject*) (*nb->nb_long) (op); + } else { + io = (PyIntObject*) (*nb->nb_int) (op); + } + if (io == NULL) + return -1; + if (!PyInt_Check(io)) { + if (PyLong_Check(io)) { + /* got a long? => retry int conversion */ + val = _PyLong_AsSsize_t((PyObject *)io); + Py_DECREF(io); + if ((val == -1) && PyErr_Occurred()) + return -1; + return val; + } + else + { + Py_DECREF(io); + PyErr_SetString(PyExc_TypeError, + "nb_int should return int object"); + return -1; + } + } + + val = PyInt_AS_LONG(io); + Py_DECREF(io); + + return val; +#endif +} + unsigned long PyInt_AsUnsignedLongMask(register PyObject *op) { @@ -302,7 +371,7 @@ PyInt_FromString(char *s, char **pend, int base) #ifdef Py_USING_UNICODE PyObject * -PyInt_FromUnicode(Py_UNICODE *s, int length, int base) +PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base) { PyObject *result; char *buffer = PyMem_MALLOC(length+1); diff --git a/Objects/iterobject.c b/Objects/iterobject.c index 90dc3f0..6f7c57e 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -74,7 +74,7 @@ iter_iternext(PyObject *iterator) static PyObject * iter_len(seqiterobject *it) { - int seqsize, len; + Py_ssize_t seqsize, len; if (it->it_seq) { seqsize = PySequence_Size(it->it_seq); diff --git a/Objects/listobject.c b/Objects/listobject.c index 8ba317a..41f7390 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -22,11 +22,11 @@ * than ob_size on entry. */ static int -list_resize(PyListObject *self, int newsize) +list_resize(PyListObject *self, Py_ssize_t newsize) { 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 @@ -82,7 +82,7 @@ PyList_Fini(void) } PyObject * -PyList_New(int size) +PyList_New(Py_ssize_t size) { PyListObject *op; size_t nbytes; @@ -118,7 +118,7 @@ PyList_New(int size) return (PyObject *) op; } -int +Py_ssize_t PyList_Size(PyObject *op) { if (!PyList_Check(op)) { @@ -132,7 +132,7 @@ PyList_Size(PyObject *op) static PyObject *indexerr = NULL; PyObject * -PyList_GetItem(PyObject *op, int i) +PyList_GetItem(PyObject *op, Py_ssize_t i) { if (!PyList_Check(op)) { PyErr_BadInternalCall(); @@ -149,7 +149,7 @@ PyList_GetItem(PyObject *op, int i) } int -PyList_SetItem(register PyObject *op, register int i, +PyList_SetItem(register PyObject *op, register Py_ssize_t i, register PyObject *newitem) { register PyObject *olditem; @@ -173,9 +173,9 @@ PyList_SetItem(register PyObject *op, register int i, } static int -ins1(PyListObject *self, int where, PyObject *v) +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(); @@ -206,7 +206,7 @@ ins1(PyListObject *self, int where, PyObject *v) } int -PyList_Insert(PyObject *op, int where, PyObject *newitem) +PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem) { if (!PyList_Check(op)) { PyErr_BadInternalCall(); @@ -218,7 +218,7 @@ PyList_Insert(PyObject *op, int where, PyObject *newitem) 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 @@ PyList_Append(PyObject *op, PyObject *newitem) 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,12 +273,13 @@ list_dealloc(PyListObject *op) static int list_print(PyListObject *op, FILE *fp, int flags) { - int i; + int rc; + Py_ssize_t i; - i = Py_ReprEnter((PyObject*)op); - if (i != 0) { - if (i < 0) - return i; + rc = Py_ReprEnter((PyObject*)op); + if (rc != 0) { + if (rc < 0) + return rc; fprintf(fp, "[...]"); return 0; } @@ -299,7 +300,7 @@ list_print(PyListObject *op, FILE *fp, int flags) static PyObject * list_repr(PyListObject *v) { - int i; + Py_ssize_t i; PyObject *s, *temp; PyObject *pieces = NULL, *result = NULL; @@ -363,7 +364,7 @@ Done: return result; } -static int +static Py_ssize_t list_length(PyListObject *a) { return a->ob_size; @@ -372,7 +373,8 @@ list_length(PyListObject *a) 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), @@ -381,7 +383,7 @@ list_contains(PyListObject *a, PyObject *el) } static PyObject * -list_item(PyListObject *a, int i) +list_item(PyListObject *a, Py_ssize_t i) { if (i < 0 || i >= a->ob_size) { if (indexerr == NULL) @@ -395,11 +397,11 @@ list_item(PyListObject *a, int i) } static PyObject * -list_slice(PyListObject *a, int ilow, int ihigh) +list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) { PyListObject *np; PyObject **src, **dest; - int i, len; + Py_ssize_t i, len; if (ilow < 0) ilow = 0; else if (ilow > a->ob_size) @@ -424,7 +426,7 @@ list_slice(PyListObject *a, int ilow, int ihigh) } PyObject * -PyList_GetSlice(PyObject *a, int ilow, int ihigh) +PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) { if (!PyList_Check(a)) { PyErr_BadInternalCall(); @@ -436,8 +438,8 @@ PyList_GetSlice(PyObject *a, int ilow, int ihigh) 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)) { @@ -473,10 +475,10 @@ list_concat(PyListObject *a, PyObject *bb) } static PyObject * -list_repeat(PyListObject *a, int n) +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 +517,7 @@ list_repeat(PyListObject *a, int n) 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 @@ -542,7 +544,7 @@ list_clear(PyListObject *a) * guaranteed the call cannot fail. */ static int -list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) +list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) { /* Because [X]DECREF can recursively invoke list operations on this list, we must postpone all [X]DECREF activity until @@ -555,10 +557,10 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) 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) @@ -640,7 +642,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) } int -PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v) +PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) { if (!PyList_Check(a)) { PyErr_BadInternalCall(); @@ -650,10 +652,10 @@ PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v) } static PyObject * -list_inplace_repeat(PyListObject *self, int n) +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); @@ -685,7 +687,7 @@ list_inplace_repeat(PyListObject *self, int n) } static int -list_ass_item(PyListObject *a, int i, PyObject *v) +list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v) { PyObject *old_value; if (i < 0 || i >= a->ob_size) { @@ -705,9 +707,9 @@ list_ass_item(PyListObject *a, int i, PyObject *v) static PyObject * listinsert(PyListObject *self, PyObject *args) { - int i; + Py_ssize_t i; PyObject *v; - if (!PyArg_ParseTuple(args, "iO:insert", &i, &v)) + if (!PyArg_ParseTuple(args, "nO:insert", &i, &v)) return NULL; if (ins1(self, i, v) == 0) Py_RETURN_NONE; @@ -726,10 +728,10 @@ static PyObject * 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 +860,7 @@ list_inplace_concat(PyListObject *self, PyObject *other) static PyObject * listpop(PyListObject *self, PyObject *args) { - int i = -1; + Py_ssize_t i = -1; PyObject *v, *arg = NULL; int status; @@ -866,8 +868,8 @@ listpop(PyListObject *self, PyObject *args) return NULL; if (arg != NULL) { if (PyInt_Check(arg)) - i = (int)(PyInt_AS_LONG((PyIntObject*) arg)); - else if (!PyArg_ParseTuple(args, "|i:pop", &i)) + i = PyInt_AS_LONG((PyIntObject*) arg); + else if (!PyArg_ParseTuple(args, "|n:pop", &i)) return NULL; } if (self->ob_size == 0) { @@ -929,7 +931,7 @@ islt(PyObject *x, PyObject *y, PyObject *compare) { 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 +990,7 @@ static int 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; @@ -1050,11 +1052,11 @@ elements to get out of order). Returns -1 in case of error. */ -static int +static Py_ssize_t 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 +1107,12 @@ key, and the last n-k should follow key. 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 +1123,7 @@ gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) /* 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 +1144,7 @@ gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) /* 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 +1170,7 @@ gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) */ ++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 +1198,12 @@ The code duplication is massive, but this is enough different given that 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 +1214,7 @@ gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) /* 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 +1236,7 @@ gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) /* 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 +1261,7 @@ gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) */ ++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 +1296,7 @@ fail: */ struct s_slice { PyObject **base; - int len; + Py_ssize_t len; }; typedef struct s_MergeState { @@ -1305,13 +1307,13 @@ typedef struct s_MergeState { * 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 +1361,7 @@ merge_freemem(MergeState *ms) * 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) @@ -1386,14 +1388,15 @@ merge_getmem(MergeState *ms, int need) * merge, and should have na <= nb. See listsort.txt for more info. * Return 0 if successful, -1 if error. */ -static int -merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb) +static Py_ssize_t +merge_lo(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 */ - 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, na) < 0) @@ -1411,8 +1414,8 @@ merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb) 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. @@ -1517,16 +1520,16 @@ CopyB: * merge, and should have na >= nb. See listsort.txt for more info. * Return 0 if successful, -1 if error. */ -static int -merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb) +static Py_ssize_t +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 +1550,8 @@ merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb) 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. @@ -1654,12 +1657,12 @@ CopyA: /* Merge the two runs at stack indices i and i+1. * Returns 0 on success, -1 on error. */ -static int -merge_at(MergeState *ms, int i) +static Py_ssize_t +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 +1731,7 @@ merge_collapse(MergeState *ms) 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 +1760,7 @@ merge_force_collapse(MergeState *ms) 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 +1779,10 @@ merge_force_collapse(MergeState *ms) * * 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) { @@ -1972,16 +1975,16 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds) { 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 +2058,7 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds) 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 +2068,7 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds) 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 +2180,7 @@ PyList_AsTuple(PyObject *v) { PyObject *w; PyObject **p; - int n; + Py_ssize_t n; if (v == NULL || !PyList_Check(v)) { PyErr_BadInternalCall(); return NULL; @@ -2200,7 +2203,7 @@ PyList_AsTuple(PyObject *v) 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 +2223,7 @@ listindex(PyListObject *self, PyObject *args) 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 +2234,8 @@ listindex(PyListObject *self, PyObject *args) 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 +2244,13 @@ listcount(PyListObject *self, PyObject *v) 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,13 +2270,13 @@ listremove(PyListObject *self, PyObject *v) static int list_traverse(PyListObject *o, visitproc visit, void *arg) { - int i, err; + Py_ssize_t i; PyObject *x; for (i = o->ob_size; --i >= 0; ) { x = o->ob_item[i]; if (x != NULL) { - err = visit(x, arg); + int err = visit(x, arg); if (err) return err; } @@ -2285,7 +2288,7 @@ static PyObject * 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 +2321,8 @@ list_richcompare(PyObject *v, PyObject *w, int op) 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) { @@ -2433,16 +2436,16 @@ static PyMethodDef list_methods[] = { }; static PySequenceMethods list_as_sequence = { - (inquiry)list_length, /* sq_length */ + (lenfunc)list_length, /* sq_length */ (binaryfunc)list_concat, /* sq_concat */ - (intargfunc)list_repeat, /* sq_repeat */ - (intargfunc)list_item, /* sq_item */ - (intintargfunc)list_slice, /* sq_slice */ - (intobjargproc)list_ass_item, /* sq_ass_item */ - (intintobjargproc)list_ass_slice, /* sq_ass_slice */ + (ssizeargfunc)list_repeat, /* sq_repeat */ + (ssizeargfunc)list_item, /* sq_item */ + (ssizessizeargfunc)list_slice, /* sq_slice */ + (ssizeobjargproc)list_ass_item, /* sq_ass_item */ + (ssizessizeobjargproc)list_ass_slice, /* sq_ass_slice */ (objobjproc)list_contains, /* sq_contains */ (binaryfunc)list_inplace_concat, /* sq_inplace_concat */ - (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */ + (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */ }; PyDoc_STRVAR(list_doc, @@ -2452,14 +2455,8 @@ PyDoc_STRVAR(list_doc, static PyObject * list_subscript(PyListObject* self, PyObject* item) { - if (PyInt_Check(item)) { - long i = PyInt_AS_LONG(item); - if (i < 0) - i += PyList_GET_SIZE(self); - return list_item(self, i); - } - else if (PyLong_Check(item)) { - long i = PyLong_AsLong(item); + if (PyInt_Check(item) || PyLong_Check(item)) { + Py_ssize_t i = PyInt_AsSsize_t(item); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) @@ -2467,7 +2464,7 @@ list_subscript(PyListObject* self, PyObject* item) return list_item(self, i); } else if (PySlice_Check(item)) { - int start, stop, step, slicelength, cur, i; + Py_ssize_t start, stop, step, slicelength, cur, i; PyObject* result; PyObject* it; PyObject **src, **dest; @@ -2521,7 +2518,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) return list_ass_item(self, i, value); } else if (PySlice_Check(item)) { - int start, stop, step, slicelength; + Py_ssize_t start, stop, step, slicelength; if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, &start, &stop, &step, &slicelength) < 0) { @@ -2535,7 +2532,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) if (value == NULL) { /* delete slice */ PyObject **garbage; - int cur, i; + Py_ssize_t cur, i; if (slicelength <= 0) return 0; @@ -2554,7 +2551,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) 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 +2583,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) 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) { @@ -2601,8 +2598,9 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) } if (PySequence_Fast_GET_SIZE(seq) != slicelength) { + /* XXX can we use %zd here? */ PyErr_Format(PyExc_ValueError, - "attempt to assign sequence of size %d to extended slice of size %d", + "attempt to assign sequence of size %ld to extended slice of size %ld", PySequence_Fast_GET_SIZE(seq), slicelength); Py_DECREF(seq); @@ -2645,7 +2643,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) } static PyMappingMethods list_as_mapping = { - (inquiry)list_length, + (lenfunc)list_length, (binaryfunc)list_subscript, (objobjargproc)list_ass_subscript }; @@ -2767,11 +2765,11 @@ listiter_next(listiterobject *it) 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); } @@ -2880,17 +2878,17 @@ listreviter_next(listreviterobject *it) return NULL; } -static int +static Py_ssize_t 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; } static PySequenceMethods listreviter_as_sequence = { - (inquiry)listreviter_len, /* sq_length */ + (lenfunc)listreviter_len, /* sq_length */ 0, /* sq_concat */ }; diff --git a/Objects/longobject.c b/Objects/longobject.c index 32f7958..17aab35 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -50,8 +50,8 @@ static PyObject *long_format(PyObject *aa, int base, int addL); static PyLongObject * long_normalize(register PyLongObject *v) { - int j = ABS(v->ob_size); - register int i = j; + Py_ssize_t j = ABS(v->ob_size); + Py_ssize_t i = j; while (i > 0 && v->ob_digit[i-1] == 0) --i; @@ -64,8 +64,13 @@ long_normalize(register PyLongObject *v) Return NULL and set exception if we run out of memory. */ PyLongObject * -_PyLong_New(int size) +_PyLong_New(Py_ssize_t size) { + if (size > INT_MAX) { + /* XXX: Fix this check when ob_size becomes ssize_t */ + PyErr_NoMemory(); + return NULL; + } return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); } @@ -73,7 +78,7 @@ PyObject * _PyLong_Copy(PyLongObject *src) { PyLongObject *result; - int i; + Py_ssize_t i; assert(src != NULL); i = src->ob_size; @@ -198,7 +203,8 @@ PyLong_AsLong(PyObject *vv) /* This version by Tim Peters */ register PyLongObject *v; unsigned long x, prev; - int i, sign; + Py_ssize_t i; + int sign; if (vv == NULL || !PyLong_Check(vv)) { if (vv != NULL && PyInt_Check(vv)) @@ -235,6 +241,50 @@ PyLong_AsLong(PyObject *vv) return -1; } +/* Get a Py_ssize_t from a long int object. + Returns -1 and sets an error condition if overflow occurs. */ + +Py_ssize_t +_PyLong_AsSsize_t(PyObject *vv) +{ + register PyLongObject *v; + size_t x, prev; + Py_ssize_t i; + int sign; + + if (vv == NULL || !PyLong_Check(vv)) { + PyErr_BadInternalCall(); + return -1; + } + v = (PyLongObject *)vv; + i = v->ob_size; + sign = 1; + x = 0; + if (i < 0) { + sign = -1; + i = -(i); + } + while (--i >= 0) { + prev = x; + x = (x << SHIFT) + v->ob_digit[i]; + if ((x >> SHIFT) != prev) + goto overflow; + } + /* Haven't lost any bits, but if the sign bit is set we're in + * trouble *unless* this is the min negative number. So, + * trouble iff sign bit set && (positive || some bit set other + * than the sign bit). + */ + if ((Py_ssize_t)x < 0 && (sign > 0 || (x << 1) != 0)) + goto overflow; + return (Py_ssize_t)x * sign; + + overflow: + PyErr_SetString(PyExc_OverflowError, + "long int too large to convert to int"); + return -1; +} + /* Get a C unsigned long int from a long int object. Returns -1 and sets an error condition if overflow occurs. */ @@ -243,7 +293,7 @@ PyLong_AsUnsignedLong(PyObject *vv) { register PyLongObject *v; unsigned long x, prev; - int i; + Py_ssize_t i; if (vv == NULL || !PyLong_Check(vv)) { if (vv != NULL && PyInt_Check(vv)) { @@ -286,7 +336,8 @@ PyLong_AsUnsignedLongMask(PyObject *vv) { register PyLongObject *v; unsigned long x; - int i, sign; + Py_ssize_t i; + int sign; if (vv == NULL || !PyLong_Check(vv)) { if (vv != NULL && PyInt_Check(vv)) @@ -324,7 +375,7 @@ _PyLong_NumBits(PyObject *vv) { PyLongObject *v = (PyLongObject *)vv; size_t result = 0; - int ndigits; + Py_ssize_t ndigits; assert(v != NULL); assert(PyLong_Check(v)); @@ -334,7 +385,7 @@ _PyLong_NumBits(PyObject *vv) digit msd = v->ob_digit[ndigits - 1]; result = (ndigits - 1) * SHIFT; - if (result / SHIFT != (size_t)ndigits - 1) + if (result / SHIFT != ndigits - 1) goto Overflow; do { ++result; @@ -464,7 +515,7 @@ _PyLong_AsByteArray(PyLongObject* v, int little_endian, int is_signed) { int i; /* index into v->ob_digit */ - int ndigits; /* |v->ob_size| */ + Py_ssize_t ndigits; /* |v->ob_size| */ twodigits accum; /* sliding register */ unsigned int accumbits; /* # bits in accum */ int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ @@ -612,7 +663,8 @@ _PyLong_AsScaledDouble(PyObject *vv, int *exponent) PyLongObject *v; double x; const double multiplier = (double)(1L << SHIFT); - int i, sign; + Py_ssize_t i; + int sign; int nbitsneeded; if (vv == NULL || !PyLong_Check(vv)) { @@ -772,6 +824,30 @@ PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); } +/* Create a new long int object from a C Py_ssize_t. */ + +PyObject * +_PyLong_FromSsize_t(Py_ssize_t ival) +{ + Py_ssize_t bytes = ival; + int one = 1; + return _PyLong_FromByteArray( + (unsigned char *)&bytes, + SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0); +} + +/* Create a new long int object from a C size_t. */ + +PyObject * +_PyLong_FromSize_t(size_t ival) +{ + size_t bytes = ival; + int one = 1; + return _PyLong_FromByteArray( + (unsigned char *)&bytes, + SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0); +} + /* Get a C PY_LONG_LONG int from a long int object. Return -1 and set an error if overflow occurs. */ @@ -859,7 +935,8 @@ PyLong_AsUnsignedLongLongMask(PyObject *vv) { register PyLongObject *v; unsigned PY_LONG_LONG x; - int i, sign; + Py_ssize_t i; + int sign; if (vv == NULL || !PyLong_Check(vv)) { PyErr_BadInternalCall(); @@ -920,7 +997,7 @@ convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) { * x[m-1], and the remaining carry (0 or 1) is returned. */ static digit -v_iadd(digit *x, int m, digit *y, int n) +v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) { int i; digit carry = 0; @@ -946,7 +1023,7 @@ v_iadd(digit *x, int m, digit *y, int n) * far as x[m-1], and the remaining borrow (0 or 1) is returned. */ static digit -v_isub(digit *x, int m, digit *y, int n) +v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) { int i; digit borrow = 0; @@ -980,10 +1057,10 @@ mul1(PyLongObject *a, wdigit n) static PyLongObject * muladd1(PyLongObject *a, wdigit n, wdigit extra) { - int size_a = ABS(a->ob_size); + Py_ssize_t size_a = ABS(a->ob_size); PyLongObject *z = _PyLong_New(size_a+1); twodigits carry = extra; - int i; + Py_ssize_t i; if (z == NULL) return NULL; @@ -1003,7 +1080,7 @@ muladd1(PyLongObject *a, wdigit n, wdigit extra) immutable. */ static digit -inplace_divrem1(digit *pout, digit *pin, int size, digit n) +inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) { twodigits rem = 0; @@ -1026,7 +1103,7 @@ inplace_divrem1(digit *pout, digit *pin, int size, digit n) static PyLongObject * divrem1(PyLongObject *a, digit n, digit *prem) { - const int size = ABS(a->ob_size); + const Py_ssize_t size = ABS(a->ob_size); PyLongObject *z; assert(n > 0 && n <= MASK); @@ -1046,8 +1123,8 @@ long_format(PyObject *aa, int base, int addL) { register PyLongObject *a = (PyLongObject *)aa; PyStringObject *str; - int i; - const int size_a = ABS(a->ob_size); + Py_ssize_t i; + const Py_ssize_t size_a = ABS(a->ob_size); char *p; int bits; char sign = '\0'; @@ -1107,7 +1184,7 @@ long_format(PyObject *aa, int base, int addL) /* Not 0, and base not a power of 2. Divide repeatedly by base, but for speed use the highest power of base that fits in a digit. */ - int size = size_a; + Py_ssize_t size = size_a; digit *pin = a->ob_digit; PyLongObject *scratch; /* powbasw <- largest power of base that fits in a digit. */ @@ -1200,7 +1277,7 @@ long_from_binary_base(char **str, int base) char *p = *str; char *start = p; int bits_per_char; - int n; + Py_ssize_t n; PyLongObject *z; twodigits accum; int bits_in_accum; @@ -1261,7 +1338,7 @@ long_from_binary_base(char **str, int base) bits_in_accum += bits_per_char; if (bits_in_accum >= SHIFT) { *pdigit++ = (digit)(accum & MASK); - assert(pdigit - z->ob_digit <= n); + assert(pdigit - z->ob_digit <= (int)n); accum >>= SHIFT; bits_in_accum -= SHIFT; assert(bits_in_accum < SHIFT); @@ -1270,7 +1347,7 @@ long_from_binary_base(char **str, int base) if (bits_in_accum) { assert(bits_in_accum <= SHIFT); *pdigit++ = (digit)accum; - assert(pdigit - z->ob_digit <= n); + assert(pdigit - z->ob_digit <= (int)n); } while (pdigit - z->ob_digit < n) *pdigit++ = 0; @@ -1356,7 +1433,7 @@ PyLong_FromString(char *str, char **pend, int base) #ifdef Py_USING_UNICODE PyObject * -PyLong_FromUnicode(Py_UNICODE *u, int length, int base) +PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) { PyObject *result; char *buffer = PyMem_MALLOC(length+1); @@ -1387,7 +1464,7 @@ static int long_divrem(PyLongObject *a, PyLongObject *b, PyLongObject **pdiv, PyLongObject **prem) { - int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); + Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); PyLongObject *z; if (size_b == 0) { @@ -1433,12 +1510,12 @@ long_divrem(PyLongObject *a, PyLongObject *b, static PyLongObject * x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) { - int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size); + Py_ssize_t size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size); digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1)); PyLongObject *v = mul1(v1, d); PyLongObject *w = mul1(w1, d); PyLongObject *a; - int j, k; + Py_ssize_t j, k; if (v == NULL || w == NULL) { Py_XDECREF(v); @@ -1550,7 +1627,7 @@ long_str(PyObject *v) static int long_compare(PyLongObject *a, PyLongObject *b) { - int sign; + Py_ssize_t sign; if (a->ob_size != b->ob_size) { if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0) @@ -1559,7 +1636,7 @@ long_compare(PyLongObject *a, PyLongObject *b) sign = a->ob_size - b->ob_size; } else { - int i = ABS(a->ob_size); + Py_ssize_t i = ABS(a->ob_size); while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) ; if (i < 0) @@ -1577,7 +1654,8 @@ static long long_hash(PyLongObject *v) { long x; - int i, sign; + Py_ssize_t i; + int sign; /* This is designed so that Python ints and longs with the same value hash to the same value, otherwise comparisons @@ -1608,7 +1686,7 @@ long_hash(PyLongObject *v) static PyLongObject * x_add(PyLongObject *a, PyLongObject *b) { - int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); + Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); PyLongObject *z; int i; digit carry = 0; @@ -1616,7 +1694,7 @@ x_add(PyLongObject *a, PyLongObject *b) /* Ensure a is the larger of the two: */ if (size_a < size_b) { { PyLongObject *temp = a; a = b; b = temp; } - { int size_temp = size_a; + { Py_ssize_t size_temp = size_a; size_a = size_b; size_b = size_temp; } } @@ -1642,9 +1720,9 @@ x_add(PyLongObject *a, PyLongObject *b) static PyLongObject * x_sub(PyLongObject *a, PyLongObject *b) { - int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); + Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); PyLongObject *z; - int i; + Py_ssize_t i; int sign = 1; digit borrow = 0; @@ -1652,7 +1730,7 @@ x_sub(PyLongObject *a, PyLongObject *b) if (size_a < size_b) { sign = -1; { PyLongObject *temp = a; a = b; b = temp; } - { int size_temp = size_a; + { Py_ssize_t size_temp = size_a; size_a = size_b; size_b = size_temp; } } @@ -1752,9 +1830,9 @@ static PyLongObject * x_mul(PyLongObject *a, PyLongObject *b) { PyLongObject *z; - int size_a = ABS(a->ob_size); - int size_b = ABS(b->ob_size); - int i; + Py_ssize_t size_a = ABS(a->ob_size); + Py_ssize_t size_b = ABS(b->ob_size); + Py_ssize_t i; z = _PyLong_New(size_a + size_b); if (z == NULL) @@ -1840,11 +1918,11 @@ x_mul(PyLongObject *a, PyLongObject *b) Returns 0 on success, -1 on failure. */ static int -kmul_split(PyLongObject *n, int size, PyLongObject **high, PyLongObject **low) +kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low) { PyLongObject *hi, *lo; - int size_lo, size_hi; - const int size_n = ABS(n->ob_size); + Py_ssize_t size_lo, size_hi; + const Py_ssize_t size_n = ABS(n->ob_size); size_lo = MIN(size_n, size); size_hi = size_n - size_lo; @@ -1873,16 +1951,16 @@ static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); static PyLongObject * k_mul(PyLongObject *a, PyLongObject *b) { - int asize = ABS(a->ob_size); - int bsize = ABS(b->ob_size); + Py_ssize_t asize = ABS(a->ob_size); + Py_ssize_t bsize = ABS(b->ob_size); PyLongObject *ah = NULL; PyLongObject *al = NULL; PyLongObject *bh = NULL; PyLongObject *bl = NULL; PyLongObject *ret = NULL; PyLongObject *t1, *t2, *t3; - int shift; /* the number of digits we split off */ - int i; + Py_ssize_t shift; /* the number of digits we split off */ + Py_ssize_t i; /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl @@ -2094,9 +2172,9 @@ ah*bh and al*bl too. static PyLongObject * k_lopsided_mul(PyLongObject *a, PyLongObject *b) { - const int asize = ABS(a->ob_size); - int bsize = ABS(b->ob_size); - int nbdone; /* # of b digits already multiplied */ + const Py_ssize_t asize = ABS(a->ob_size); + Py_ssize_t bsize = ABS(b->ob_size); + Py_ssize_t nbdone; /* # of b digits already multiplied */ PyLongObject *ret; PyLongObject *bslice = NULL; @@ -2117,7 +2195,7 @@ k_lopsided_mul(PyLongObject *a, PyLongObject *b) nbdone = 0; while (bsize > 0) { PyLongObject *product; - const int nbtouse = MIN(bsize, asize); + const Py_ssize_t nbtouse = MIN(bsize, asize); /* Multiply the next slice of b by a. */ memcpy(bslice->ob_digit, b->ob_digit + nbdone, @@ -2353,7 +2431,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x) int negativeOutput = 0; /* if x<0 return negative output */ PyLongObject *z = NULL; /* accumulated result */ - int i, j, k; /* counters */ + Py_ssize_t i, j, k; /* counters */ PyLongObject *temp = NULL; /* 5-ary values. If the exponent is large enough, table is @@ -2597,7 +2675,7 @@ long_rshift(PyLongObject *v, PyLongObject *w) PyLongObject *a, *b; PyLongObject *z = NULL; long shiftby; - int newsize, wordshift, loshift, hishift, i, j; + Py_ssize_t newsize, wordshift, loshift, hishift, i, j; digit lomask, himask; CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); @@ -2664,7 +2742,7 @@ long_lshift(PyObject *v, PyObject *w) PyLongObject *a, *b; PyLongObject *z = NULL; long shiftby; - int oldsize, newsize, wordshift, remshift, i, j; + Py_ssize_t oldsize, newsize, wordshift, remshift, i, j; twodigits accum; CONVERT_BINOP(v, w, &a, &b); @@ -2723,7 +2801,7 @@ long_bitwise(PyLongObject *a, { digit maska, maskb; /* 0 or MASK */ int negz; - int size_a, size_b, size_z; + Py_ssize_t size_a, size_b, size_z; PyLongObject *z; int i; digit diga, digb; @@ -2965,7 +3043,7 @@ static PyObject * long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyLongObject *tmp, *new; - int i, n; + Py_ssize_t i, n; assert(PyType_IsSubtype(type, &PyLong_Type)); tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds); diff --git a/Objects/methodobject.c b/Objects/methodobject.c index 3a205f5..5e920a2 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -65,7 +65,7 @@ PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) PyCFunctionObject* f = (PyCFunctionObject*)func; PyCFunction meth = PyCFunction_GET_FUNCTION(func); PyObject *self = PyCFunction_GET_SELF(func); - int size; + long size; switch (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST)) { case METH_VARARGS: @@ -81,7 +81,7 @@ PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) if (size == 0) return (*meth)(self, NULL); PyErr_Format(PyExc_TypeError, - "%.200s() takes no arguments (%d given)", + "%.200s() takes no arguments (%ld given)", f->m_ml->ml_name, size); return NULL; } @@ -92,7 +92,7 @@ PyCFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) if (size == 1) return (*meth)(self, PyTuple_GET_ITEM(arg, 0)); PyErr_Format(PyExc_TypeError, - "%.200s() takes exactly one argument (%d given)", + "%.200s() takes exactly one argument (%ld given)", f->m_ml->ml_name, size); return NULL; } diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 76a4ab3..dfcf39c 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -104,7 +104,7 @@ _PyModule_Clear(PyObject *m) None, rather than deleting them from the dictionary, to avoid rehashing the dictionary (to some extent). */ - int pos; + Py_ssize_t pos; PyObject *key, *value; PyObject *d; diff --git a/Objects/object.c b/Objects/object.c index 6d6d078..2a1f45a 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -170,7 +170,7 @@ PyObject_Init(PyObject *op, PyTypeObject *tp) } PyVarObject * -PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size) +PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) { if (op == NULL) return (PyVarObject *) PyErr_NoMemory(); @@ -192,7 +192,7 @@ _PyObject_New(PyTypeObject *tp) } PyVarObject * -_PyObject_NewVar(PyTypeObject *tp, int nitems) +_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) { PyVarObject *op; const size_t size = _PyObject_VAR_SIZE(tp, nitems); @@ -1175,7 +1175,7 @@ _PyObject_GetDictPtr(PyObject *obj) if (dictoffset == 0) return NULL; if (dictoffset < 0) { - int tsize; + Py_ssize_t tsize; size_t size; tsize = ((PyVarObject *)obj)->ob_size; @@ -1237,7 +1237,7 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name) /* Inline _PyType_Lookup */ { - int i, n; + Py_ssize_t i, n; PyObject *mro, *base, *dict; /* Look in tp_dict of types in MRO */ @@ -1278,7 +1278,7 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name) if (dictoffset != 0) { PyObject *dict; if (dictoffset < 0) { - int tsize; + Py_ssize_t tsize; size_t size; tsize = ((PyVarObject *)obj)->ob_size; @@ -1414,7 +1414,7 @@ PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) int PyObject_IsTrue(PyObject *v) { - int res; + Py_ssize_t res; if (v == Py_True) return 1; if (v == Py_False) @@ -1432,7 +1432,8 @@ PyObject_IsTrue(PyObject *v) res = (*v->ob_type->tp_as_sequence->sq_length)(v); else return 1; - return (res > 0) ? 1 : res; + /* if it is negative, it should be either -1 or -2 */ + return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); } /* equivalent of 'not v' @@ -1556,7 +1557,7 @@ merge_class_dict(PyObject* dict, PyObject* aclass) PyErr_Clear(); else { /* We have no guarantee that bases is a real tuple */ - int i, n; + Py_ssize_t i, n; n = PySequence_Size(bases); /* This better be right */ if (n < 0) PyErr_Clear(); @@ -1949,7 +1950,7 @@ PyTypeObject *_Py_cobject_hack = &PyCObject_Type; /* Hack to force loading of abstract.o */ -int (*_Py_abstract_hack)(PyObject *) = PyObject_Size; +Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; /* Python's malloc wrappers (see pymem.h) */ @@ -1992,7 +1993,7 @@ Py_ReprEnter(PyObject *obj) { PyObject *dict; PyObject *list; - int i; + Py_ssize_t i; dict = PyThreadState_GetDict(); if (dict == NULL) @@ -2020,7 +2021,7 @@ Py_ReprLeave(PyObject *obj) { PyObject *dict; PyObject *list; - int i; + Py_ssize_t i; dict = PyThreadState_GetDict(); if (dict == NULL) diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index 266b4c6..090abda 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -541,8 +541,8 @@ error: /* This is only useful when running memory debuggers such as * Purify or Valgrind. Uncomment to use. * -#define Py_USING_MEMORY_DEBUGGER */ +#define Py_USING_MEMORY_DEBUGGER #ifdef Py_USING_MEMORY_DEBUGGER @@ -816,7 +816,7 @@ PyObject_Realloc(void *p, size_t nbytes) { void *bp; poolp pool; - uint size; + size_t size; if (p == NULL) return PyObject_Malloc(nbytes); @@ -1005,6 +1005,8 @@ _PyObject_DebugMalloc(size_t nbytes) bumpserialno(); total = nbytes + 16; +#if SIZEOF_SIZE_T < 8 + /* XXX do this check only on 32-bit machines */ if (total < nbytes || (total >> 31) > 1) { /* overflow, or we can't represent it in 4 bytes */ /* Obscure: can't do (total >> 32) != 0 instead, because @@ -1013,12 +1015,13 @@ _PyObject_DebugMalloc(size_t nbytes) size_t is an unsigned type. */ return NULL; } +#endif p = (uchar *)PyObject_Malloc(total); if (p == NULL) return NULL; - write4(p, nbytes); + write4(p, (ulong)nbytes); p[4] = p[5] = p[6] = p[7] = FORBIDDENBYTE; if (nbytes > 0) @@ -1081,7 +1084,7 @@ _PyObject_DebugRealloc(void *p, size_t nbytes) if (q == NULL) return NULL; - write4(q, nbytes); + write4(q, (ulong)nbytes); assert(q[4] == FORBIDDENBYTE && q[5] == FORBIDDENBYTE && q[6] == FORBIDDENBYTE && diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index cfd9100..a9c0b55 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -91,27 +91,27 @@ generates the numbers in the range on demand. For looping, this is \n\ slightly faster than range() and more memory efficient."); static PyObject * -range_item(rangeobject *r, int i) +range_item(rangeobject *r, Py_ssize_t i) { if (i < 0 || i >= r->len) { PyErr_SetString(PyExc_IndexError, "xrange object index out of range"); return NULL; } - return PyInt_FromLong(r->start + (i % r->len) * r->step); + return PyInt_FromSsize_t(r->start + (i % r->len) * r->step); } -static int +static Py_ssize_t range_length(rangeobject *r) { -#if LONG_MAX != INT_MAX +#if LONG_MAX != INT_MAX /* XXX ssize_t_max */ if (r->len > INT_MAX) { PyErr_SetString(PyExc_ValueError, "xrange object size cannot be reported"); return -1; } #endif - return (int)(r->len); + return (Py_ssize_t)(r->len); } static PyObject * @@ -137,10 +137,10 @@ range_repr(rangeobject *r) } static PySequenceMethods range_as_sequence = { - (inquiry)range_length, /* sq_length */ + (lenfunc)range_length, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ - (intargfunc)range_item, /* sq_item */ + (ssizeargfunc)range_item, /* sq_item */ 0, /* sq_slice */ }; diff --git a/Objects/setobject.c b/Objects/setobject.c index baa2d01..0041a10 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -51,8 +51,8 @@ NULL if the rich comparison returns an error. static setentry * set_lookkey(PySetObject *so, PyObject *key, register long hash) { - register int i; - register unsigned int perturb; + register Py_ssize_t i; + register size_t perturb; register setentry *freeslot; register unsigned int mask = so->mask; setentry *table = so->table; @@ -129,8 +129,8 @@ set_lookkey(PySetObject *so, PyObject *key, register long hash) static setentry * set_lookkey_string(PySetObject *so, PyObject *key, register long hash) { - register int i; - register unsigned int perturb; + register Py_ssize_t i; + register size_t perturb; register setentry *freeslot; register unsigned int mask = so->mask; setentry *table = so->table; @@ -468,9 +468,10 @@ set_clear_internal(PySetObject *so) * mutates the table. */ static int -set_next(PySetObject *so, int *pos_ptr, setentry **entry_ptr) +set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr) { - register int i, mask; + Py_ssize_t i; + int mask; register setentry *table; assert (PyAnySet_Check(so)); @@ -517,7 +518,7 @@ static int set_tp_print(PySetObject *so, FILE *fp, int flags) { setentry *entry; - int pos=0; + Py_ssize_t pos=0; char *emit = ""; /* No separator emitted on first pass */ char *separator = ", "; @@ -551,7 +552,7 @@ set_repr(PySetObject *so) return result; } -static int +static Py_ssize_t set_len(PyObject *so) { return ((PySetObject *)so)->used; @@ -673,7 +674,7 @@ PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element."); static int set_traverse(PySetObject *so, visitproc visit, void *arg) { - int pos = 0; + Py_ssize_t pos = 0; setentry *entry; while (set_next(so, &pos, &entry)) @@ -687,7 +688,7 @@ frozenset_hash(PyObject *self) PySetObject *so = (PySetObject *)self; long h, hash = 1927868237L; setentry *entry; - int pos = 0; + Py_ssize_t pos = 0; if (so->hash != -1) return so->hash; @@ -752,7 +753,7 @@ setiter_dealloc(setiterobject *si) static PyObject * setiter_len(setiterobject *si) { - int len = 0; + long len = 0; if (si->si_set != NULL && si->si_used == si->si_set->used) len = si->len; return PyInt_FromLong(len); @@ -847,7 +848,7 @@ set_update_internal(PySetObject *so, PyObject *other) if (PyDict_Check(other)) { PyObject *value; - int pos = 0; + Py_ssize_t pos = 0; while (PyDict_Next(other, &pos, &key, &value)) { if (set_add_key(so, key) == -1) return -1; @@ -1121,7 +1122,7 @@ set_intersection(PySetObject *so, PyObject *other) return NULL; if (PyAnySet_Check(other)) { - int pos = 0; + Py_ssize_t pos = 0; setentry *entry; if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) { @@ -1222,7 +1223,7 @@ set_difference_update_internal(PySetObject *so, PyObject *other) if (PyAnySet_Check(other)) { setentry *entry; - int pos = 0; + Py_ssize_t pos = 0; while (set_next((PySetObject *)other, &pos, &entry)) set_discard_entry(so, entry); @@ -1266,7 +1267,7 @@ set_difference(PySetObject *so, PyObject *other) { PyObject *result; setentry *entry; - int pos = 0; + Py_ssize_t pos = 0; if (!PyAnySet_Check(other) && !PyDict_Check(other)) { result = set_copy(so); @@ -1340,7 +1341,7 @@ set_symmetric_difference_update(PySetObject *so, PyObject *other) { PySetObject *otherset; PyObject *key; - int pos = 0; + Py_ssize_t pos = 0; setentry *entry; if ((PyObject *)so == other) @@ -1442,7 +1443,7 @@ static PyObject * set_issubset(PySetObject *so, PyObject *other) { setentry *entry; - int pos = 0; + Py_ssize_t pos = 0; if (!PyAnySet_Check(other)) { PyObject *tmp, *result; @@ -1687,7 +1688,7 @@ set_init(PySetObject *self, PyObject *args, PyObject *kwds) } static PySequenceMethods set_as_sequence = { - (inquiry)set_len, /* sq_length */ + (lenfunc)set_len, /* sq_length */ 0, /* sq_concat */ 0, /* sq_repeat */ 0, /* sq_item */ diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index f5ed898..3b37dbb 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -80,9 +80,10 @@ PySlice_New(PyObject *start, PyObject *stop, PyObject *step) } int -PySlice_GetIndices(PySliceObject *r, int length, - int *start, int *stop, int *step) +PySlice_GetIndices(PySliceObject *r, Py_ssize_t length, + Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) { + /* XXX support long ints */ if (r->step == Py_None) { *step = 1; } else { @@ -110,12 +111,12 @@ PySlice_GetIndices(PySliceObject *r, int length, } int -PySlice_GetIndicesEx(PySliceObject *r, int length, - int *start, int *stop, int *step, int *slicelength) +PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length, + Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength) { /* this is harder to get right than you might think */ - int defstart, defstop; + Py_ssize_t defstart, defstop; if (r->step == Py_None) { *step = 1; @@ -230,7 +231,7 @@ static PyMemberDef slice_members[] = { static PyObject* slice_indices(PySliceObject* self, PyObject* len) { - int ilen, start, stop, step, slicelength; + Py_ssize_t ilen, start, stop, step, slicelength; ilen = PyInt_AsLong(len); diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 7c3ab09..a8e1cb6 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -49,7 +49,7 @@ static PyObject *interned; parameter (for PyString_FromString()). */ PyObject * -PyString_FromStringAndSize(const char *str, int size) +PyString_FromStringAndSize(const char *str, Py_ssize_t size) { register PyStringObject *op; assert(size >= 0); @@ -154,7 +154,7 @@ PyObject * PyString_FromFormatV(const char *format, va_list vargs) { va_list count; - int n = 0; + Py_ssize_t n = 0; const char* f; char *s; PyObject* string; @@ -235,7 +235,8 @@ PyString_FromFormatV(const char *format, va_list vargs) for (f = format; *f; f++) { if (*f == '%') { const char* p = f++; - int i, longflag = 0; + Py_ssize_t i; + int longflag = 0; /* parse the width.precision part (we're only interested in the precision value, if any) */ n = 0; @@ -330,7 +331,7 @@ PyString_FromFormat(const char *format, ...) PyObject *PyString_Decode(const char *s, - int size, + Py_ssize_t size, const char *encoding, const char *errors) { @@ -410,7 +411,7 @@ PyObject *PyString_AsDecodedString(PyObject *str, } PyObject *PyString_Encode(const char *s, - int size, + Py_ssize_t size, const char *encoding, const char *errors) { @@ -519,16 +520,16 @@ string_dealloc(PyObject *op) specified encoding. */ PyObject *PyString_DecodeEscape(const char *s, - int len, + Py_ssize_t len, const char *errors, - int unicode, + Py_ssize_t unicode, const char *recode_encoding) { int c; char *p, *buf; const char *end; PyObject *v; - int newlen = recode_encoding ? 4*len:len; + Py_ssize_t newlen = recode_encoding ? 4*len:len; v = PyString_FromStringAndSize((char *)NULL, newlen); if (v == NULL) return NULL; @@ -542,7 +543,7 @@ PyObject *PyString_DecodeEscape(const char *s, PyObject *u, *w; char *r; const char* t; - int rn; + Py_ssize_t rn; t = s; /* Decode non-ASCII bytes as UTF-8. */ while (t < end && (*t & 0x80)) t++; @@ -658,18 +659,18 @@ PyObject *PyString_DecodeEscape(const char *s, } } if (p-buf < newlen) - _PyString_Resize(&v, (int)(p - buf)); + _PyString_Resize(&v, p - buf); return v; failed: Py_DECREF(v); return NULL; } -static int +static Py_ssize_t string_getsize(register PyObject *op) { char *s; - int len; + Py_ssize_t len; if (PyString_AsStringAndSize(op, &s, &len)) return -1; return len; @@ -679,13 +680,13 @@ static /*const*/ char * string_getbuffer(register PyObject *op) { char *s; - int len; + Py_ssize_t len; if (PyString_AsStringAndSize(op, &s, &len)) return NULL; return s; } -int +Py_ssize_t PyString_Size(register PyObject *op) { if (!PyString_Check(op)) @@ -704,7 +705,7 @@ PyString_AsString(register PyObject *op) int PyString_AsStringAndSize(register PyObject *obj, register char **s, - register int *len) + register Py_ssize_t *len) { if (s == NULL) { PyErr_BadInternalCall(); @@ -731,7 +732,7 @@ PyString_AsStringAndSize(register PyObject *obj, *s = PyString_AS_STRING(obj); if (len != NULL) *len = PyString_GET_SIZE(obj); - else if ((int)strlen(*s) != PyString_GET_SIZE(obj)) { + else if (strlen(*s) != PyString_GET_SIZE(obj)) { PyErr_SetString(PyExc_TypeError, "expected string without null bytes"); return -1; @@ -744,7 +745,7 @@ PyString_AsStringAndSize(register PyObject *obj, static int string_print(PyStringObject *op, FILE *fp, int flags) { - int i; + Py_ssize_t i; char c; int quote; @@ -809,7 +810,7 @@ PyString_Repr(PyObject *obj, int smartquotes) return NULL; } else { - register int i; + register Py_ssize_t i; register char c; register char *p; int quote; @@ -876,7 +877,7 @@ string_str(PyObject *s) } } -static int +static Py_ssize_t string_length(PyStringObject *a) { return a->ob_size; @@ -885,7 +886,7 @@ string_length(PyStringObject *a) static PyObject * string_concat(register PyStringObject *a, register PyObject *bb) { - register unsigned int size; + register size_t size; register PyStringObject *op; if (!PyString_Check(bb)) { #ifdef Py_USING_UNICODE @@ -909,6 +910,7 @@ string_concat(register PyStringObject *a, register PyObject *bb) return (PyObject *)a; } size = a->ob_size + b->ob_size; + /* XXX check overflow */ /* Inline PyObject_NewVar */ op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); if (op == NULL) @@ -916,19 +918,19 @@ string_concat(register PyStringObject *a, register PyObject *bb) PyObject_INIT_VAR(op, &PyString_Type, size); op->ob_shash = -1; op->ob_sstate = SSTATE_NOT_INTERNED; - memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); - memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size); + memcpy(op->ob_sval, a->ob_sval, a->ob_size); + memcpy(op->ob_sval + a->ob_size, b->ob_sval, b->ob_size); op->ob_sval[size] = '\0'; return (PyObject *) op; #undef b } static PyObject * -string_repeat(register PyStringObject *a, register int n) +string_repeat(register PyStringObject *a, register Py_ssize_t n) { - register int i; - register int j; - register int size; + register Py_ssize_t i; + register Py_ssize_t j; + register Py_ssize_t size; register PyStringObject *op; size_t nbytes; if (n < 0) @@ -966,8 +968,8 @@ string_repeat(register PyStringObject *a, register int n) } i = 0; if (i < size) { - memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); - i = (int) a->ob_size; + memcpy(op->ob_sval, a->ob_sval, a->ob_size); + i = a->ob_size; } while (i < size) { j = (i <= size-i) ? i : size-i; @@ -980,7 +982,8 @@ string_repeat(register PyStringObject *a, register int n) /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ static PyObject * -string_slice(register PyStringObject *a, register int i, register int j) +string_slice(register PyStringObject *a, register Py_ssize_t i, + register Py_ssize_t j) /* j -- may be negative! */ { if (i < 0) @@ -996,7 +999,7 @@ string_slice(register PyStringObject *a, register int i, register int j) } if (j < i) j = i; - return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i)); + return PyString_FromStringAndSize(a->ob_sval + i, j-i); } static int @@ -1005,7 +1008,7 @@ string_contains(PyObject *a, PyObject *el) char *s = PyString_AS_STRING(a); const char *sub = PyString_AS_STRING(el); char *last; - int len_sub = PyString_GET_SIZE(el); + Py_ssize_t len_sub = PyString_GET_SIZE(el); int shortsub; char firstchar, lastchar; @@ -1047,7 +1050,7 @@ string_contains(PyObject *a, PyObject *el) } static PyObject * -string_item(PyStringObject *a, register int i) +string_item(PyStringObject *a, register Py_ssize_t i) { PyObject *v; char *pchar; @@ -1072,8 +1075,8 @@ static PyObject* string_richcompare(PyStringObject *a, PyStringObject *b, int op) { int c; - int len_a, len_b; - int min_len; + Py_ssize_t len_a, len_b; + Py_ssize_t min_len; PyObject *result; /* Make sure both arguments are strings. */ @@ -1145,7 +1148,7 @@ _PyString_Eq(PyObject *o1, PyObject *o2) static long string_hash(PyStringObject *a) { - register int len; + register Py_ssize_t len; register unsigned char *p; register long x; @@ -1166,14 +1169,8 @@ string_hash(PyStringObject *a) static PyObject* string_subscript(PyStringObject* self, PyObject* item) { - if (PyInt_Check(item)) { - long i = PyInt_AS_LONG(item); - if (i < 0) - i += PyString_GET_SIZE(self); - return string_item(self,i); - } - else if (PyLong_Check(item)) { - long i = PyLong_AsLong(item); + if (PyInt_Check(item) || PyLong_Check(item)) { + Py_ssize_t i = PyInt_AsSsize_t(item); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) @@ -1181,7 +1178,7 @@ string_subscript(PyStringObject* self, PyObject* item) return string_item(self,i); } else if (PySlice_Check(item)) { - int start, stop, step, slicelength, cur, i; + Py_ssize_t start, stop, step, slicelength, cur, i; char* source_buf; char* result_buf; PyObject* result; @@ -1219,8 +1216,8 @@ string_subscript(PyStringObject* self, PyObject* item) } } -static int -string_buffer_getreadbuf(PyStringObject *self, int index, const void **ptr) +static Py_ssize_t +string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr) { if ( index != 0 ) { PyErr_SetString(PyExc_SystemError, @@ -1231,24 +1228,24 @@ string_buffer_getreadbuf(PyStringObject *self, int index, const void **ptr) return self->ob_size; } -static int -string_buffer_getwritebuf(PyStringObject *self, int index, const void **ptr) +static Py_ssize_t +string_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr) { PyErr_SetString(PyExc_TypeError, "Cannot use string as modifiable buffer"); return -1; } -static int -string_buffer_getsegcount(PyStringObject *self, int *lenp) +static Py_ssize_t +string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp) { if ( lenp ) *lenp = self->ob_size; return 1; } -static int -string_buffer_getcharbuf(PyStringObject *self, int index, const char **ptr) +static Py_ssize_t +string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr) { if ( index != 0 ) { PyErr_SetString(PyExc_SystemError, @@ -1260,27 +1257,27 @@ string_buffer_getcharbuf(PyStringObject *self, int index, const char **ptr) } static PySequenceMethods string_as_sequence = { - (inquiry)string_length, /*sq_length*/ + (lenfunc)string_length, /*sq_length*/ (binaryfunc)string_concat, /*sq_concat*/ - (intargfunc)string_repeat, /*sq_repeat*/ - (intargfunc)string_item, /*sq_item*/ - (intintargfunc)string_slice, /*sq_slice*/ + (ssizeargfunc)string_repeat, /*sq_repeat*/ + (ssizeargfunc)string_item, /*sq_item*/ + (ssizessizeargfunc)string_slice, /*sq_slice*/ 0, /*sq_ass_item*/ 0, /*sq_ass_slice*/ (objobjproc)string_contains /*sq_contains*/ }; static PyMappingMethods string_as_mapping = { - (inquiry)string_length, + (lenfunc)string_length, (binaryfunc)string_subscript, 0, }; static PyBufferProcs string_as_buffer = { - (getreadbufferproc)string_buffer_getreadbuf, - (getwritebufferproc)string_buffer_getwritebuf, - (getsegcountproc)string_buffer_getsegcount, - (getcharbufferproc)string_buffer_getcharbuf, + (readbufferproc)string_buffer_getreadbuf, + (writebufferproc)string_buffer_getwritebuf, + (segcountproc)string_buffer_getsegcount, + (charbufferproc)string_buffer_getcharbuf, }; @@ -1319,9 +1316,9 @@ static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; Py_DECREF(str); static PyObject * -split_whitespace(const char *s, int len, int maxsplit) +split_whitespace(const char *s, Py_ssize_t len, int maxsplit) { - int i, j; + Py_ssize_t i, j; PyObject *str; PyObject *list = PyList_New(0); @@ -1353,9 +1350,9 @@ split_whitespace(const char *s, int len, int maxsplit) } static PyObject * -split_char(const char *s, int len, char ch, int maxcount) +split_char(const char *s, Py_ssize_t len, char ch, int maxcount) { - register int i, j; + register Py_ssize_t i, j; PyObject *str; PyObject *list = PyList_New(0); @@ -1392,7 +1389,8 @@ whitespace string is a separator."); static PyObject * string_split(PyStringObject *self, PyObject *args) { - int len = PyString_GET_SIZE(self), n, i, j, err; + Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; + int err; int maxsplit = -1; const char *s = PyString_AS_STRING(self), *sub; PyObject *list, *item, *subobj = Py_None; @@ -1430,7 +1428,7 @@ string_split(PyStringObject *self, PyObject *args) if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) { if (maxsplit-- <= 0) break; - item = PyString_FromStringAndSize(s+j, (int)(i-j)); + item = PyString_FromStringAndSize(s+j, i-j); if (item == NULL) goto fail; err = PyList_Append(list, item); @@ -1442,7 +1440,7 @@ string_split(PyStringObject *self, PyObject *args) else i++; } - item = PyString_FromStringAndSize(s+j, (int)(len-j)); + item = PyString_FromStringAndSize(s+j, len-j); if (item == NULL) goto fail; err = PyList_Append(list, item); @@ -1458,9 +1456,9 @@ string_split(PyStringObject *self, PyObject *args) } static PyObject * -rsplit_whitespace(const char *s, int len, int maxsplit) +rsplit_whitespace(const char *s, Py_ssize_t len, int maxsplit) { - int i, j; + Py_ssize_t i, j; PyObject *str; PyObject *list = PyList_New(0); @@ -1492,9 +1490,9 @@ rsplit_whitespace(const char *s, int len, int maxsplit) } static PyObject * -rsplit_char(const char *s, int len, char ch, int maxcount) +rsplit_char(const char *s, Py_ssize_t len, char ch, int maxcount) { - register int i, j; + register Py_ssize_t i, j; PyObject *str; PyObject *list = PyList_New(0); @@ -1532,7 +1530,8 @@ is a separator."); static PyObject * string_rsplit(PyStringObject *self, PyObject *args) { - int len = PyString_GET_SIZE(self), n, i, j, err; + Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; + int err; int maxsplit = -1; const char *s = PyString_AS_STRING(self), *sub; PyObject *list, *item, *subobj = Py_None; @@ -1571,7 +1570,7 @@ string_rsplit(PyStringObject *self, PyObject *args) if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) { if (maxsplit-- <= 0) break; - item = PyString_FromStringAndSize(s+i+n, (int)(j-i-n)); + item = PyString_FromStringAndSize(s+i+n, j-i-n); if (item == NULL) goto fail; err = PyList_Insert(list, 0, item); @@ -1610,12 +1609,12 @@ static PyObject * string_join(PyStringObject *self, PyObject *orig) { char *sep = PyString_AS_STRING(self); - const int seplen = PyString_GET_SIZE(self); + const Py_ssize_t seplen = PyString_GET_SIZE(self); PyObject *res = NULL; char *p; - int seqlen = 0; + Py_ssize_t seqlen = 0; size_t sz = 0; - int i; + Py_ssize_t i; PyObject *seq, *item; seq = PySequence_Fast(orig, ""); @@ -1663,7 +1662,7 @@ string_join(PyStringObject *self, PyObject *orig) PyErr_Format(PyExc_TypeError, "sequence item %i: expected string," " %.80s found", - i, item->ob_type->tp_name); + /*XXX*/(int)i, item->ob_type->tp_name); Py_DECREF(seq); return NULL; } @@ -1679,7 +1678,7 @@ string_join(PyStringObject *self, PyObject *orig) } /* Allocate result space. */ - res = PyString_FromStringAndSize((char*)NULL, (int)sz); + res = PyString_FromStringAndSize((char*)NULL, sz); if (res == NULL) { Py_DECREF(seq); return NULL; @@ -1712,7 +1711,7 @@ _PyString_Join(PyObject *sep, PyObject *x) } static void -string_adjust_indices(int *start, int *end, int len) +string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len) { if (*end > len) *end = len; @@ -1726,14 +1725,15 @@ string_adjust_indices(int *start, int *end, int len) *start = 0; } -static long +static Py_ssize_t string_find_internal(PyStringObject *self, PyObject *args, int dir) { const char *s = PyString_AS_STRING(self), *sub; - int len = PyString_GET_SIZE(self); - int n, i = 0, last = INT_MAX; + Py_ssize_t len = PyString_GET_SIZE(self); + Py_ssize_t n, i = 0, last = INT_MAX; PyObject *subobj; + /* XXX ssize_t i */ if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) return -2; @@ -1759,13 +1759,13 @@ string_find_internal(PyStringObject *self, PyObject *args, int dir) return (long)i; } else { - int j; + Py_ssize_t j; if (n == 0 && i <= last) - return (long)last; + return last; for (j = last-n; j >= i; --j) if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0) - return (long)j; + return j; } return -1; @@ -1784,10 +1784,10 @@ Return -1 on failure."); static PyObject * string_find(PyStringObject *self, PyObject *args) { - long result = string_find_internal(self, args, +1); + Py_ssize_t result = string_find_internal(self, args, +1); if (result == -2) return NULL; - return PyInt_FromLong(result); + return PyInt_FromSsize_t(result); } @@ -1799,7 +1799,7 @@ Like S.find() but raise ValueError when the substring is not found."); static PyObject * string_index(PyStringObject *self, PyObject *args) { - long result = string_find_internal(self, args, +1); + Py_ssize_t result = string_find_internal(self, args, +1); if (result == -2) return NULL; if (result == -1) { @@ -1807,7 +1807,7 @@ string_index(PyStringObject *self, PyObject *args) "substring not found"); return NULL; } - return PyInt_FromLong(result); + return PyInt_FromSsize_t(result); } @@ -1823,10 +1823,10 @@ Return -1 on failure."); static PyObject * string_rfind(PyStringObject *self, PyObject *args) { - long result = string_find_internal(self, args, -1); + Py_ssize_t result = string_find_internal(self, args, -1); if (result == -2) return NULL; - return PyInt_FromLong(result); + return PyInt_FromSsize_t(result); } @@ -1838,7 +1838,7 @@ Like S.rfind() but raise ValueError when the substring is not found."); static PyObject * string_rindex(PyStringObject *self, PyObject *args) { - long result = string_find_internal(self, args, -1); + Py_ssize_t result = string_find_internal(self, args, -1); if (result == -2) return NULL; if (result == -1) { @@ -1846,7 +1846,7 @@ string_rindex(PyStringObject *self, PyObject *args) "substring not found"); return NULL; } - return PyInt_FromLong(result); + return PyInt_FromSsize_t(result); } @@ -1854,10 +1854,10 @@ static PyObject * do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) { char *s = PyString_AS_STRING(self); - int len = PyString_GET_SIZE(self); + Py_ssize_t len = PyString_GET_SIZE(self); char *sep = PyString_AS_STRING(sepobj); - int seplen = PyString_GET_SIZE(sepobj); - int i, j; + Py_ssize_t seplen = PyString_GET_SIZE(sepobj); + Py_ssize_t i, j; i = 0; if (striptype != RIGHTSTRIP) { @@ -1887,7 +1887,7 @@ static PyObject * do_strip(PyStringObject *self, int striptype) { char *s = PyString_AS_STRING(self); - int len = PyString_GET_SIZE(self), i, j; + Py_ssize_t len = PyString_GET_SIZE(self), i, j; i = 0; if (striptype != RIGHTSTRIP) { @@ -2014,7 +2014,7 @@ static PyObject * string_lower(PyStringObject *self) { char *s = PyString_AS_STRING(self), *s_new; - int i, n = PyString_GET_SIZE(self); + Py_ssize_t i, n = PyString_GET_SIZE(self); PyObject *new; new = PyString_FromStringAndSize(NULL, n); @@ -2042,7 +2042,7 @@ static PyObject * string_upper(PyStringObject *self) { char *s = PyString_AS_STRING(self), *s_new; - int i, n = PyString_GET_SIZE(self); + Py_ssize_t i, n = PyString_GET_SIZE(self); PyObject *new; new = PyString_FromStringAndSize(NULL, n); @@ -2071,7 +2071,7 @@ static PyObject* string_title(PyStringObject *self) { char *s = PyString_AS_STRING(self), *s_new; - int i, n = PyString_GET_SIZE(self); + Py_ssize_t i, n = PyString_GET_SIZE(self); int previous_is_cased = 0; PyObject *new; @@ -2106,7 +2106,7 @@ static PyObject * string_capitalize(PyStringObject *self) { char *s = PyString_AS_STRING(self), *s_new; - int i, n = PyString_GET_SIZE(self); + Py_ssize_t i, n = PyString_GET_SIZE(self); PyObject *new; new = PyString_FromStringAndSize(NULL, n); @@ -2144,9 +2144,9 @@ static PyObject * string_count(PyStringObject *self, PyObject *args) { const char *s = PyString_AS_STRING(self), *sub, *t; - int len = PyString_GET_SIZE(self), n; - int i = 0, last = INT_MAX; - int m, r; + Py_ssize_t len = PyString_GET_SIZE(self), n; + Py_ssize_t i = 0, last = INT_MAX; + Py_ssize_t m, r; PyObject *subobj; if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj, @@ -2159,7 +2159,7 @@ string_count(PyStringObject *self, PyObject *args) } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(subobj)) { - int count; + Py_ssize_t count; count = PyUnicode_Count((PyObject *)self, subobj, i, last); if (count == -1) return NULL; @@ -2174,7 +2174,7 @@ string_count(PyStringObject *self, PyObject *args) m = last + 1 - n; if (n == 0) - return PyInt_FromLong((long) (m-i)); + return PyInt_FromSsize_t(m-i); r = 0; while (i < m) { @@ -2191,7 +2191,7 @@ string_count(PyStringObject *self, PyObject *args) break; i = t - s; } - return PyInt_FromLong((long) r); + return PyInt_FromSsize_t(r); } PyDoc_STRVAR(swapcase__doc__, @@ -2204,7 +2204,7 @@ static PyObject * string_swapcase(PyStringObject *self) { char *s = PyString_AS_STRING(self), *s_new; - int i, n = PyString_GET_SIZE(self); + Py_ssize_t i, n = PyString_GET_SIZE(self); PyObject *new; new = PyString_FromStringAndSize(NULL, n); @@ -2240,10 +2240,10 @@ string_translate(PyStringObject *self, PyObject *args) { register char *input, *output; register const char *table; - register int i, c, changed = 0; + register Py_ssize_t i, c, changed = 0; PyObject *input_obj = (PyObject*)self; const 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]; PyObject *tableobj, *delobj = NULL; @@ -2357,10 +2357,10 @@ string_translate(PyStringObject *self, PyObject *args) found, or -1 if not found. If len of PAT is greater than length of MEM, the function returns -1. */ -static int -mymemfind(const char *mem, int len, const char *pat, int pat_len) +static Py_ssize_t +mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len) { - register int ii; + register Py_ssize_t ii; /* pattern can not occur in the last pat_len-1 chars */ len -= pat_len; @@ -2380,11 +2380,11 @@ mymemfind(const char *mem, int len, const char *pat, int pat_len) meaning mem=1111 and pat==11 returns 2. mem=11111 and pat==11 also return 2. */ -static int -mymemcnt(const char *mem, int len, const char *pat, int pat_len) +static Py_ssize_t +mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len) { - register int offset = 0; - int nfound = 0; + register Py_ssize_t offset = 0; + Py_ssize_t nfound = 0; while (len >= 0) { offset = mymemfind(mem, len, pat, pat_len); @@ -2417,15 +2417,15 @@ mymemcnt(const char *mem, int len, const char *pat, int pat_len) NULL if an error occurred. */ static char * -mymemreplace(const char *str, int len, /* input string */ - const char *pat, int pat_len, /* pattern string to find */ - const char *sub, int sub_len, /* substitution string */ - int count, /* number of replacements */ - int *out_len) +mymemreplace(const char *str, Py_ssize_t len, /* input string */ + const char *pat, Py_ssize_t pat_len, /* pattern string to find */ + const char *sub, Py_ssize_t sub_len, /* substitution string */ + Py_ssize_t count, /* number of replacements */ + Py_ssize_t *out_len) { char *out_s; char *new_s; - int nfound, offset, new_len; + Py_ssize_t nfound, offset, new_len; if (len == 0 || (pat_len == 0 && sub_len == 0) || pat_len > len) goto return_same; @@ -2508,8 +2508,8 @@ string_replace(PyStringObject *self, PyObject *args) { const char *str = PyString_AS_STRING(self), *sub, *repl; char *new_s; - const int len = PyString_GET_SIZE(self); - int sub_len, repl_len, out_len; + const Py_ssize_t len = PyString_GET_SIZE(self); + Py_ssize_t sub_len, repl_len, out_len; int count = -1; PyObject *new; PyObject *subobj, *replobj; @@ -2578,11 +2578,11 @@ static PyObject * string_startswith(PyStringObject *self, PyObject *args) { const char* str = PyString_AS_STRING(self); - int len = PyString_GET_SIZE(self); + Py_ssize_t len = PyString_GET_SIZE(self); const char* prefix; - int plen; - int start = 0; - int end = INT_MAX; + Py_ssize_t plen; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; PyObject *subobj; if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, @@ -2594,7 +2594,7 @@ string_startswith(PyStringObject *self, PyObject *args) } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(subobj)) { - int rc; + Py_ssize_t rc; rc = PyUnicode_Tailmatch((PyObject *)self, subobj, start, end, -1); if (rc == -1) @@ -2629,11 +2629,11 @@ static PyObject * string_endswith(PyStringObject *self, PyObject *args) { const char* str = PyString_AS_STRING(self); - int len = PyString_GET_SIZE(self); + Py_ssize_t len = PyString_GET_SIZE(self); const char* suffix; - int slen; - int start = 0; - int end = INT_MAX; + Py_ssize_t slen; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; PyObject *subobj; if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, @@ -2645,7 +2645,7 @@ string_endswith(PyStringObject *self, PyObject *args) } #ifdef Py_USING_UNICODE else if (PyUnicode_Check(subobj)) { - int rc; + Py_ssize_t rc; rc = PyUnicode_Tailmatch((PyObject *)self, subobj, start, end, +1); if (rc == -1) @@ -2756,7 +2756,7 @@ string_expandtabs(PyStringObject *self, PyObject *args) { const char *e, *p; char *q; - int i, j; + Py_ssize_t i, j; PyObject *u; int tabsize = 8; @@ -2807,7 +2807,7 @@ string_expandtabs(PyStringObject *self, PyObject *args) } static PyObject * -pad(PyStringObject *self, int left, int right, char fill) +pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill) { PyObject *u; @@ -2894,11 +2894,11 @@ PyDoc_STRVAR(center__doc__, static PyObject * string_center(PyStringObject *self, PyObject *args) { - int marg, left; - int width; + Py_ssize_t marg, left; + long width; char fillchar = ' '; - if (!PyArg_ParseTuple(args, "i|c:center", &width, &fillchar)) + if (!PyArg_ParseTuple(args, "l|c:center", &width, &fillchar)) return NULL; if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { @@ -2921,12 +2921,12 @@ PyDoc_STRVAR(zfill__doc__, static PyObject * string_zfill(PyStringObject *self, PyObject *args) { - int fill; + long fill; PyObject *s; char *p; int width; - if (!PyArg_ParseTuple(args, "i:zfill", &width)) + if (!PyArg_ParseTuple(args, "l:zfill", &width)) return NULL; if (PyString_GET_SIZE(self) >= width) { @@ -3209,9 +3209,9 @@ is given and true."); static PyObject* string_splitlines(PyStringObject *self, PyObject *args) { - register int i; - register int j; - int len; + register Py_ssize_t i; + register Py_ssize_t j; + Py_ssize_t len; int keepends = 0; PyObject *list; PyObject *str; @@ -3228,7 +3228,7 @@ string_splitlines(PyStringObject *self, PyObject *args) goto onError; for (i = j = 0; i < len; ) { - int eol; + Py_ssize_t eol; /* Find a line and append it */ while (i < len && data[i] != '\n' && data[i] != '\r') @@ -3340,7 +3340,7 @@ static PyObject * str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *tmp, *pnew; - int n; + Py_ssize_t n; assert(PyType_IsSubtype(type, &PyString_Type)); tmp = string_new(&PyString_Type, args, kwds); @@ -3521,7 +3521,7 @@ PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) */ int -_PyString_Resize(PyObject **pv, int newsize) +_PyString_Resize(PyObject **pv, Py_ssize_t newsize) { register PyObject *v; register PyStringObject *sv; @@ -3625,7 +3625,7 @@ formatfloat(char *buf, size_t buflen, int flags, (flags&F_ALT) ? "#" : "", prec, type); PyOS_ascii_formatd(buf, buflen, fmt, x); - return strlen(buf); + return (int)strlen(buf); } /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and @@ -3655,7 +3655,7 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type, { PyObject *result = NULL; char *buf; - int i; + Py_ssize_t i; int sign; /* 1 if '-', else 0 */ int len; /* number of characters */ int numdigits; /* len == numnondigits + numdigits */ @@ -3832,7 +3832,7 @@ formatint(char *buf, size_t buflen, int flags, PyOS_snprintf(buf, buflen, fmt, -x); else PyOS_snprintf(buf, buflen, fmt, x); - return strlen(buf); + return (int)strlen(buf); } static int @@ -3865,7 +3865,8 @@ PyObject * PyString_Format(PyObject *format, PyObject *args) { char *fmt, *res; - int fmtcnt, rescnt, reslen, arglen, argidx; + int arglen, argidx; + Py_ssize_t reslen, rescnt, fmtcnt; int args_owned = 0; PyObject *result, *orig_args; #ifdef Py_USING_UNICODE @@ -3911,7 +3912,7 @@ PyString_Format(PyObject *format, PyObject *args) else { /* Got a format specifier */ int flags = 0; - int width = -1; + Py_ssize_t width = -1; int prec = -1; int c = '\0'; int fill; @@ -3930,7 +3931,7 @@ PyString_Format(PyObject *format, PyObject *args) fmt++; if (*fmt == '(') { char *keystart; - int keylen; + Py_ssize_t keylen; PyObject *key; int pcount = 1; @@ -4393,7 +4394,7 @@ void _Py_ReleaseInternedStrings(void) { PyObject *keys; PyStringObject *s; - int i, n; + Py_ssize_t i, n; if (interned == NULL || !PyDict_Check(interned)) return; diff --git a/Objects/structseq.c b/Objects/structseq.c index ac3cf03..b035ca2 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -40,7 +40,7 @@ PyStructSequence_New(PyTypeObject *type) static void structseq_dealloc(PyStructSequence *obj) { - int i, size; + Py_ssize_t i, size; size = REAL_SIZE(obj); for (i = 0; i < size; ++i) { @@ -49,14 +49,14 @@ structseq_dealloc(PyStructSequence *obj) PyObject_Del(obj); } -static int +static Py_ssize_t structseq_length(PyStructSequence *obj) { return VISIBLE_SIZE(obj); } static PyObject* -structseq_item(PyStructSequence *obj, int i) +structseq_item(PyStructSequence *obj, Py_ssize_t i) { if (i < 0 || i >= VISIBLE_SIZE(obj)) { PyErr_SetString(PyExc_IndexError, "tuple index out of range"); @@ -67,10 +67,10 @@ structseq_item(PyStructSequence *obj, int i) } static PyObject* -structseq_slice(PyStructSequence *obj, int low, int high) +structseq_slice(PyStructSequence *obj, Py_ssize_t low, Py_ssize_t high) { PyTupleObject *np; - int i; + Py_ssize_t i; if (low < 0) low = 0; @@ -96,7 +96,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *dict = NULL; PyObject *ob; PyStructSequence *res = NULL; - int len, min_len, max_len, i, n_unnamed_fields; + Py_ssize_t len, min_len, max_len, i, n_unnamed_fields; static const char *kwlist[] = {"sequence", "dict", 0}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:structseq", @@ -125,7 +125,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (min_len != max_len) { if (len < min_len) { PyErr_Format(PyExc_TypeError, - "%.500s() takes an at least %d-sequence (%d-sequence given)", + "%.500s() takes an at least %ld-sequence (%ld-sequence given)", type->tp_name, min_len, len); Py_DECREF(arg); return NULL; @@ -133,7 +133,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (len > max_len) { PyErr_Format(PyExc_TypeError, - "%.500s() takes an at most %d-sequence (%d-sequence given)", + "%.500s() takes an at most %ld-sequence (%ld-sequence given)", type->tp_name, max_len, len); Py_DECREF(arg); return NULL; @@ -142,7 +142,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds) else { if (len != min_len) { PyErr_Format(PyExc_TypeError, - "%.500s() takes a %d-sequence (%d-sequence given)", + "%.500s() takes a %ld-sequence (%ld-sequence given)", type->tp_name, min_len, len); Py_DECREF(arg); return NULL; @@ -200,7 +200,7 @@ structseq_concat(PyStructSequence *obj, PyObject *b) } static PyObject * -structseq_repeat(PyStructSequence *obj, int n) +structseq_repeat(PyStructSequence *obj, Py_ssize_t n) { PyObject *tup, *result; tup = make_tuple(obj); @@ -284,11 +284,11 @@ structseq_reduce(PyStructSequence* self) } static PySequenceMethods structseq_as_sequence = { - (inquiry)structseq_length, + (lenfunc)structseq_length, (binaryfunc)structseq_concat, /* sq_concat */ - (intargfunc)structseq_repeat, /* sq_repeat */ - (intargfunc)structseq_item, /* sq_item */ - (intintargfunc)structseq_slice, /* sq_slice */ + (ssizeargfunc)structseq_repeat, /* sq_repeat */ + (ssizeargfunc)structseq_item, /* sq_item */ + (ssizessizeargfunc)structseq_slice, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ (objobjproc)structseq_contains, /* sq_contains */ diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 8ea29f0..2de2899 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -24,10 +24,10 @@ int tuple_zero_allocs; #endif PyObject * -PyTuple_New(register int size) +PyTuple_New(register Py_ssize_t size) { register PyTupleObject *op; - int i; + Py_ssize_t i; if (size < 0) { PyErr_BadInternalCall(); return NULL; @@ -57,7 +57,7 @@ PyTuple_New(register int size) else #endif { - int nbytes = size * sizeof(PyObject *); + Py_ssize_t nbytes = size * sizeof(PyObject *); /* Check for overflow */ if (nbytes / sizeof(PyObject *) != (size_t)size || (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *)) @@ -82,7 +82,7 @@ PyTuple_New(register int size) return (PyObject *) op; } -int +Py_ssize_t PyTuple_Size(register PyObject *op) { if (!PyTuple_Check(op)) { @@ -94,7 +94,7 @@ PyTuple_Size(register PyObject *op) } PyObject * -PyTuple_GetItem(register PyObject *op, register int i) +PyTuple_GetItem(register PyObject *op, register Py_ssize_t i) { if (!PyTuple_Check(op)) { PyErr_BadInternalCall(); @@ -108,7 +108,7 @@ PyTuple_GetItem(register PyObject *op, register int i) } int -PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem) +PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem) { register PyObject *olditem; register PyObject **p; @@ -131,9 +131,9 @@ PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem) } PyObject * -PyTuple_Pack(int n, ...) +PyTuple_Pack(Py_ssize_t n, ...) { - int i; + Py_ssize_t i; PyObject *o; PyObject *result; PyObject **items; @@ -159,8 +159,8 @@ PyTuple_Pack(int n, ...) static void tupledealloc(register PyTupleObject *op) { - register int i; - register int len = op->ob_size; + register Py_ssize_t i; + register Py_ssize_t len = op->ob_size; PyObject_GC_UnTrack(op); Py_TRASHCAN_SAFE_BEGIN(op) if (len > 0) { @@ -187,7 +187,7 @@ done: static int tupleprint(PyTupleObject *op, FILE *fp, int flags) { - int i; + Py_ssize_t i; fprintf(fp, "("); for (i = 0; i < op->ob_size; i++) { if (i > 0) @@ -204,7 +204,7 @@ tupleprint(PyTupleObject *op, FILE *fp, int flags) static PyObject * tuplerepr(PyTupleObject *v) { - int i, n; + Py_ssize_t i, n; PyObject *s, *temp; PyObject *pieces, *result = NULL; @@ -268,7 +268,7 @@ static long tuplehash(PyTupleObject *v) { register long x, y; - register int len = v->ob_size; + register Py_ssize_t len = v->ob_size; register PyObject **p; long mult = 1000003L; x = 0x345678L; @@ -286,7 +286,7 @@ tuplehash(PyTupleObject *v) return x; } -static int +static Py_ssize_t tuplelength(PyTupleObject *a) { return a->ob_size; @@ -295,7 +295,8 @@ tuplelength(PyTupleObject *a) static int tuplecontains(PyTupleObject *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, PyTuple_GET_ITEM(a, i), @@ -304,7 +305,7 @@ tuplecontains(PyTupleObject *a, PyObject *el) } static PyObject * -tupleitem(register PyTupleObject *a, register int i) +tupleitem(register PyTupleObject *a, register Py_ssize_t i) { if (i < 0 || i >= a->ob_size) { PyErr_SetString(PyExc_IndexError, "tuple index out of range"); @@ -315,12 +316,13 @@ tupleitem(register PyTupleObject *a, register int i) } static PyObject * -tupleslice(register PyTupleObject *a, register int ilow, register int ihigh) +tupleslice(register PyTupleObject *a, register Py_ssize_t ilow, + register Py_ssize_t ihigh) { register PyTupleObject *np; PyObject **src, **dest; - register int i; - int len; + register Py_ssize_t i; + Py_ssize_t len; if (ilow < 0) ilow = 0; if (ihigh > a->ob_size) @@ -346,7 +348,7 @@ tupleslice(register PyTupleObject *a, register int ilow, register int ihigh) } PyObject * -PyTuple_GetSlice(PyObject *op, int i, int j) +PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j) { if (op == NULL || !PyTuple_Check(op)) { PyErr_BadInternalCall(); @@ -358,8 +360,8 @@ PyTuple_GetSlice(PyObject *op, int i, int j) static PyObject * tupleconcat(register PyTupleObject *a, register PyObject *bb) { - register int size; - register int i; + register Py_ssize_t size; + register Py_ssize_t i; PyObject **src, **dest; PyTupleObject *np; if (!PyTuple_Check(bb)) { @@ -395,10 +397,10 @@ tupleconcat(register PyTupleObject *a, register PyObject *bb) } static PyObject * -tuplerepeat(PyTupleObject *a, int n) +tuplerepeat(PyTupleObject *a, Py_ssize_t n) { - int i, j; - int size; + Py_ssize_t i, j; + Py_ssize_t size; PyTupleObject *np; PyObject **p, **items; if (n < 0) @@ -434,13 +436,13 @@ tuplerepeat(PyTupleObject *a, int n) static int tupletraverse(PyTupleObject *o, visitproc visit, void *arg) { - int i, err; + Py_ssize_t i; PyObject *x; for (i = o->ob_size; --i >= 0; ) { x = o->ob_item[i]; if (x != NULL) { - err = visit(x, arg); + int err = visit(x, arg); if (err) return err; } @@ -452,8 +454,8 @@ static PyObject * tuplerichcompare(PyObject *v, PyObject *w, int op) { PyTupleObject *vt, *wt; - int i; - int vlen, wlen; + Py_ssize_t i; + Py_ssize_t vlen, wlen; if (!PyTuple_Check(v) || !PyTuple_Check(w)) { Py_INCREF(Py_NotImplemented); @@ -545,7 +547,7 @@ static PyObject * tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *tmp, *new, *item; - int i, n; + Py_ssize_t i, n; assert(PyType_IsSubtype(type, &PyTuple_Type)); tmp = tuple_new(&PyTuple_Type, args, kwds); @@ -571,11 +573,11 @@ PyDoc_STRVAR(tuple_doc, "If the argument is a tuple, the return value is the same object."); static PySequenceMethods tuple_as_sequence = { - (inquiry)tuplelength, /* sq_length */ + (lenfunc)tuplelength, /* sq_length */ (binaryfunc)tupleconcat, /* sq_concat */ - (intargfunc)tuplerepeat, /* sq_repeat */ - (intargfunc)tupleitem, /* sq_item */ - (intintargfunc)tupleslice, /* sq_slice */ + (ssizeargfunc)tuplerepeat, /* sq_repeat */ + (ssizeargfunc)tupleitem, /* sq_item */ + (ssizessizeargfunc)tupleslice, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ (objobjproc)tuplecontains, /* sq_contains */ @@ -584,14 +586,8 @@ static PySequenceMethods tuple_as_sequence = { static PyObject* tuplesubscript(PyTupleObject* self, PyObject* item) { - if (PyInt_Check(item)) { - long i = PyInt_AS_LONG(item); - if (i < 0) - i += PyTuple_GET_SIZE(self); - return tupleitem(self, i); - } - else if (PyLong_Check(item)) { - long i = PyLong_AsLong(item); + if (PyInt_Check(item) || PyLong_Check(item)) { + Py_ssize_t i = PyInt_AsSsize_t(item); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) @@ -599,7 +595,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item) return tupleitem(self, i); } else if (PySlice_Check(item)) { - int start, stop, step, slicelength, cur, i; + Py_ssize_t start, stop, step, slicelength, cur, i; PyObject* result; PyObject* it; PyObject **src, **dest; @@ -648,7 +644,7 @@ static PyMethodDef tuple_methods[] = { }; static PyMappingMethods tuple_as_mapping = { - (inquiry)tuplelength, + (lenfunc)tuplelength, (binaryfunc)tuplesubscript, 0 }; @@ -707,12 +703,12 @@ PyTypeObject PyTuple_Type = { known to some other part of the code. */ int -_PyTuple_Resize(PyObject **pv, int newsize) +_PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) { register PyTupleObject *v; register PyTupleObject *sv; - int i; - int oldsize; + Py_ssize_t i; + Py_ssize_t oldsize; v = (PyTupleObject *) *pv; if (v == NULL || v->ob_type != &PyTuple_Type || @@ -854,7 +850,7 @@ tupleiter_next(tupleiterobject *it) static PyObject * tupleiter_len(tupleiterobject *it) { - int len = 0; + long len = 0; if (it->it_seq) len = PyTuple_GET_SIZE(it->it_seq) - it->it_index; return PyInt_FromLong(len); diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 03f1adb..f52a87f 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -145,7 +145,7 @@ mro_subclasses(PyTypeObject *type, PyObject* temp) { PyTypeObject *subclass; PyObject *ref, *subclasses, *old_mro; - int i, n; + Py_ssize_t i, n; subclasses = type->tp_subclasses; if (subclasses == NULL) @@ -184,7 +184,8 @@ mro_subclasses(PyTypeObject *type, PyObject* temp) static int type_set_bases(PyTypeObject *type, PyObject *value, void *context) { - int i, r = 0; + Py_ssize_t i; + int r = 0; PyObject *ob, *temp; PyTypeObject *new_base, *old_base; PyObject *old_bases, *old_mro; @@ -443,7 +444,7 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) } PyObject * -PyType_GenericAlloc(PyTypeObject *type, int nitems) +PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) { PyObject *obj; const size_t size = _PyObject_VAR_SIZE(type, nitems+1); @@ -483,7 +484,7 @@ PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) static int traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg) { - int i, n; + Py_ssize_t i, n; PyMemberDef *mp; n = type->ob_size; @@ -548,7 +549,7 @@ subtype_traverse(PyObject *self, visitproc visit, void *arg) static void clear_slots(PyTypeObject *type, PyObject *self) { - int i, n; + Py_ssize_t i, n; PyMemberDef *mp; n = type->ob_size; @@ -825,7 +826,7 @@ PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) if (mro != NULL) { /* Deal with multiple inheritance without recursion by walking the MRO tuple */ - int i, n; + Py_ssize_t i, n; assert(PyTuple_Check(mro)); n = PyTuple_GET_SIZE(mro); for (i = 0; i < n; i++) { @@ -970,7 +971,7 @@ static int fill_classic_mro(PyObject *mro, PyObject *cls) { PyObject *bases, *base; - int i, n; + Py_ssize_t i, n; assert(PyList_Check(mro)); assert(PyClass_Check(cls)); @@ -1037,7 +1038,7 @@ classic_mro(PyObject *cls) static int tail_contains(PyObject *list, int whence, PyObject *o) { - int j, size; + Py_ssize_t j, size; size = PyList_GET_SIZE(list); for (j = whence+1; j < size; j++) { @@ -1068,7 +1069,7 @@ class_name(PyObject *cls) static int check_duplicates(PyObject *list) { - int i, j, n; + Py_ssize_t i, j, n; /* Let's use a quadratic time algorithm, assuming that the bases lists is short. */ @@ -1101,7 +1102,7 @@ check_duplicates(PyObject *list) static void set_mro_error(PyObject *to_merge, int *remain) { - int i, n, off, to_merge_size; + Py_ssize_t i, n, off, to_merge_size; char buf[1000]; PyObject *k, *v; PyObject *set = PyDict_New(); @@ -1136,9 +1137,9 @@ consistent method resolution\norder (MRO) for bases"); static int pmerge(PyObject *acc, PyObject* to_merge) { - int i, j, to_merge_size; + Py_ssize_t i, j, to_merge_size, empty_cnt; int *remain; - int ok, empty_cnt; + int ok; to_merge_size = PyList_GET_SIZE(to_merge); @@ -1206,7 +1207,8 @@ pmerge(PyObject *acc, PyObject* to_merge) { static PyObject * mro_implementation(PyTypeObject *type) { - int i, n, ok; + Py_ssize_t i, n; + int ok; PyObject *bases, *result; PyObject *to_merge, *bases_aslist; @@ -1309,7 +1311,7 @@ mro_internal(PyTypeObject *type) if (tuple == NULL) return -1; if (checkit) { - int i, len; + Py_ssize_t i, len; PyObject *cls; PyTypeObject *solid; @@ -1350,7 +1352,7 @@ mro_internal(PyTypeObject *type) static PyTypeObject * best_base(PyObject *bases) { - int i, n; + Py_ssize_t i, n; PyTypeObject *base, *winner, *candidate, *base_i; PyObject *base_proto; @@ -1532,7 +1534,7 @@ static int valid_identifier(PyObject *s) { unsigned char *p; - int i, n; + Py_ssize_t i, n; if (!PyString_Check(s)) { PyErr_SetString(PyExc_TypeError, @@ -1564,7 +1566,7 @@ _unicode_to_string(PyObject *slots, int nslots) PyObject *tmp = slots; PyObject *o, *o1; int i; - intintargfunc copy = slots->ob_type->tp_as_sequence->sq_slice; + ssizessizeargfunc copy = slots->ob_type->tp_as_sequence->sq_slice; for (i = 0; i < nslots; i++) { if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) { if (tmp == slots) { @@ -1596,7 +1598,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) PyTypeObject *type, *base, *tmptype, *winner; PyHeapTypeObject *et; PyMemberDef *mp; - int i, nbases, nslots, slotoffset, add_dict, add_weak; + Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; int j, may_add_dict, may_add_weak; assert(args != NULL && PyTuple_Check(args)); @@ -1604,8 +1606,8 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) /* Special case: type(x) should return x->ob_type */ { - const int nargs = PyTuple_GET_SIZE(args); - const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); + const Py_ssize_t nargs = PyTuple_GET_SIZE(args); + const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { PyObject *x = PyTuple_GET_ITEM(args, 0); @@ -1999,7 +2001,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) PyObject * _PyType_Lookup(PyTypeObject *type, PyObject *name) { - int i, n; + Py_ssize_t i, n; PyObject *mro, *res, *base, *dict; /* Look in tp_dict of types in MRO */ @@ -2154,7 +2156,7 @@ static PyObject * type_subclasses(PyTypeObject *type, PyObject *args_ignored) { PyObject *list, *raw, *ref; - int i, n; + Py_ssize_t i, n; list = PyList_New(0); if (list == NULL) @@ -2587,7 +2589,7 @@ reduce_2(PyObject *obj) PyObject *getstate = NULL, *state = NULL, *names = NULL; PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL; PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL; - int i, n; + Py_ssize_t i, n; cls = PyObject_GetAttrString(obj, "__class__"); if (cls == NULL) @@ -3155,7 +3157,7 @@ PyType_Ready(PyTypeObject *type) { PyObject *dict, *bases; PyTypeObject *base; - int i, n; + Py_ssize_t i, n; if (type->tp_flags & Py_TPFLAGS_READY) { assert(type->tp_dict != NULL); @@ -3340,7 +3342,7 @@ add_subclass(PyTypeObject *base, PyTypeObject *type) static void remove_subclass(PyTypeObject *base, PyTypeObject *type) { - int i; + Py_ssize_t i; PyObject *list, *ref; list = base->tp_subclasses; @@ -3370,9 +3372,10 @@ check_num_args(PyObject *ob, int n) } if (n == PyTuple_GET_SIZE(ob)) return 1; + /* XXX %zd? */ PyErr_Format( PyExc_TypeError, - "expected %d arguments, got %d", n, PyTuple_GET_SIZE(ob)); + "expected %d arguments, got %d", n, (int)PyTuple_GET_SIZE(ob)); return 0; } @@ -3385,10 +3388,10 @@ check_num_args(PyObject *ob, int n) entries, one regular and one with reversed arguments. */ static PyObject * -wrap_inquiry(PyObject *self, PyObject *args, void *wrapped) +wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) { - inquiry func = (inquiry)wrapped; - int res; + lenfunc func = (lenfunc)wrapped; + Py_ssize_t res; if (!check_num_args(args, 0)) return NULL; @@ -3525,28 +3528,28 @@ wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) } static PyObject * -wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped) +wrap_ssizeargfunc(PyObject *self, PyObject *args, void *wrapped) { - intargfunc func = (intargfunc)wrapped; - int i; + ssizeargfunc func = (ssizeargfunc)wrapped; + Py_ssize_t i; - if (!PyArg_ParseTuple(args, "i", &i)) + if (!PyArg_ParseTuple(args, "n", &i)) return NULL; return (*func)(self, i); } -static int +static Py_ssize_t getindex(PyObject *self, PyObject *arg) { - int i; + Py_ssize_t i; - i = PyInt_AsLong(arg); + i = PyInt_AsSsize_t(arg); if (i == -1 && PyErr_Occurred()) return -1; if (i < 0) { PySequenceMethods *sq = self->ob_type->tp_as_sequence; if (sq && sq->sq_length) { - int n = (*sq->sq_length)(self); + Py_ssize_t n = (*sq->sq_length)(self); if (n < 0) return -1; i += n; @@ -3558,9 +3561,9 @@ getindex(PyObject *self, PyObject *arg) static PyObject * wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) { - intargfunc func = (intargfunc)wrapped; + ssizeargfunc func = (ssizeargfunc)wrapped; PyObject *arg; - int i; + Py_ssize_t i; if (PyTuple_GET_SIZE(args) == 1) { arg = PyTuple_GET_ITEM(args, 0); @@ -3575,12 +3578,12 @@ wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) } static PyObject * -wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped) +wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped) { - intintargfunc func = (intintargfunc)wrapped; - int i, j; + ssizessizeargfunc func = (ssizessizeargfunc)wrapped; + Py_ssize_t i, j; - if (!PyArg_ParseTuple(args, "ii", &i, &j)) + if (!PyArg_ParseTuple(args, "nn", &i, &j)) return NULL; return (*func)(self, i, j); } @@ -3588,8 +3591,9 @@ wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped) static PyObject * wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) { - intobjargproc func = (intobjargproc)wrapped; - int i, res; + ssizeobjargproc func = (ssizeobjargproc)wrapped; + Py_ssize_t i; + int res; PyObject *arg, *value; if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value)) @@ -3607,8 +3611,9 @@ wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) static PyObject * wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) { - intobjargproc func = (intobjargproc)wrapped; - int i, res; + ssizeobjargproc func = (ssizeobjargproc)wrapped; + Py_ssize_t i; + int res; PyObject *arg; if (!check_num_args(args, 1)) @@ -3625,13 +3630,14 @@ wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) } static PyObject * -wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped) +wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped) { - intintobjargproc func = (intintobjargproc)wrapped; - int i, j, res; + ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; + Py_ssize_t i, j; + int res; PyObject *value; - if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value)) + if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value)) return NULL; res = (*func)(self, i, j, value); if (res == -1 && PyErr_Occurred()) @@ -3643,10 +3649,11 @@ wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped) static PyObject * wrap_delslice(PyObject *self, PyObject *args, void *wrapped) { - intintobjargproc func = (intintobjargproc)wrapped; - int i, j, res; + ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped; + Py_ssize_t i, j; + int res; - if (!PyArg_ParseTuple(args, "ii", &i, &j)) + if (!PyArg_ParseTuple(args, "nn", &i, &j)) return NULL; res = (*func)(self, i, j, NULL); if (res == -1 && PyErr_Occurred()) @@ -4099,23 +4106,23 @@ FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ "(" ARGCODES ")", arg1, arg2); \ } -static int +static Py_ssize_t slot_sq_length(PyObject *self) { static PyObject *len_str; PyObject *res = call_method(self, "__len__", &len_str, "()"); - long temp; - int len; + Py_ssize_t temp; + Py_ssize_t len; if (res == NULL) return -1; - temp = PyInt_AsLong(res); + temp = PyInt_AsSsize_t(res); len = (int)temp; Py_DECREF(res); if (len == -1 && PyErr_Occurred()) return -1; -#if SIZEOF_INT < SIZEOF_LONG - /* Overflow check -- range of PyInt is more than C int */ +#if SIZEOF_SIZE_T < SIZEOF_LONG + /* Overflow check -- range of PyInt is more than C ssize_t */ if (len != temp) { PyErr_SetString(PyExc_OverflowError, "__len__() should return 0 <= outcome < 2**31"); @@ -4133,7 +4140,7 @@ slot_sq_length(PyObject *self) /* Super-optimized version of slot_sq_item. Other slots could do the same... */ static PyObject * -slot_sq_item(PyObject *self, int i) +slot_sq_item(PyObject *self, Py_ssize_t i) { static PyObject *getitem_str; PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; @@ -4175,10 +4182,10 @@ slot_sq_item(PyObject *self, int i) return NULL; } -SLOT2(slot_sq_slice, "__getslice__", int, int, "ii") +SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn") static int -slot_sq_ass_item(PyObject *self, int index, PyObject *value) +slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) { PyObject *res; static PyObject *delitem_str, *setitem_str; @@ -4196,7 +4203,7 @@ slot_sq_ass_item(PyObject *self, int index, PyObject *value) } static int -slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value) +slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value) { PyObject *res; static PyObject *delslice_str, *setslice_str; @@ -4428,7 +4435,7 @@ half_compare(PyObject *self, PyObject *other) { PyObject *func, *args, *res; static PyObject *cmp_str; - int c; + Py_ssize_t c; func = lookup_method(self, "__cmp__", &cmp_str); if (func == NULL) { @@ -4806,7 +4813,7 @@ slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static PyObject *new_str; PyObject *func; PyObject *newargs, *x; - int i, n; + Py_ssize_t i, n; if (new_str == NULL) { new_str = PyString_InternFromString("__new__"); @@ -4953,7 +4960,7 @@ typedef struct wrapperbase slotdef; "x." NAME "(y) <==> " DOC) static slotdef slotdefs[] = { - SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry, + SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, "x.__len__() <==> len(x)"), /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. The logic in abstract.c always falls back to nb_add/nb_multiply in @@ -4962,13 +4969,13 @@ static slotdef slotdefs[] = { test_descr.notimplemented() */ SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, "x.__add__(y) <==> x+y"), - SQSLOT("__mul__", sq_repeat, NULL, wrap_intargfunc, + SQSLOT("__mul__", sq_repeat, NULL, wrap_ssizeargfunc, "x.__mul__(n) <==> x*n"), - SQSLOT("__rmul__", sq_repeat, NULL, wrap_intargfunc, + SQSLOT("__rmul__", sq_repeat, NULL, wrap_ssizeargfunc, "x.__rmul__(n) <==> n*x"), SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, "x.__getitem__(y) <==> x[y]"), - SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc, + SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc, "x.__getslice__(i, j) <==> x[i:j]\n\ \n\ Use of negative indices is not supported."), @@ -4977,7 +4984,7 @@ static slotdef slotdefs[] = { SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, "x.__delitem__(y) <==> del x[y]"), SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, - wrap_intintobjargproc, + wrap_ssizessizeobjargproc, "x.__setslice__(i, j, y) <==> x[i:j]=y\n\ \n\ Use of negative indices is not supported."), @@ -4990,9 +4997,9 @@ static slotdef slotdefs[] = { SQSLOT("__iadd__", sq_inplace_concat, NULL, wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), SQSLOT("__imul__", sq_inplace_repeat, NULL, - wrap_intargfunc, "x.__imul__(y) <==> x*=y"), + wrap_ssizeargfunc, "x.__imul__(y) <==> x*=y"), - MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry, + MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, "x.__len__() <==> len(x)"), MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, wrap_binaryfunc, @@ -5152,9 +5159,10 @@ static slotdef slotdefs[] = { proper indirection pointer (as_buffer, etc.); it returns NULL if the indirection pointer is NULL. */ static void ** -slotptr(PyTypeObject *type, int offset) +slotptr(PyTypeObject *type, int ioffset) { char *ptr; + long offset = ioffset; /* Note: this depends on the order of the members of PyHeapTypeObject! */ assert(offset >= 0); @@ -5320,7 +5328,9 @@ slotdef_cmp(const void *aa, const void *bb) if (c != 0) return c; else - return a - b; + /* Cannot use a-b, as this gives off_t, + which may lose precision when converted to int. */ + return (a > b) ? 1 : (a < b) ? -1 : 0; } /* Initialize the slotdefs table by adding interned string objects for the @@ -5417,7 +5427,7 @@ recurse_down_subclasses(PyTypeObject *type, PyObject *name, { PyTypeObject *subclass; PyObject *ref, *subclasses, *dict; - int i, n; + Py_ssize_t i, n; subclasses = type->tp_subclasses; if (subclasses == NULL) @@ -5570,7 +5580,7 @@ super_getattro(PyObject *self, PyObject *name) PyObject *mro, *res, *tmp, *dict; PyTypeObject *starttype; descrgetfunc f; - int i, n; + Py_ssize_t i, n; starttype = su->obj_type; mro = starttype->tp_mro; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 037c45c..43ef061 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -119,7 +119,7 @@ PyUnicode_GetMax(void) static int unicode_resize(register PyUnicodeObject *unicode, - int length) + Py_ssize_t length) { void *oldstr; @@ -154,7 +154,8 @@ int unicode_resize(register PyUnicodeObject *unicode, return -1; } unicode->str[length] = 0; - unicode->length = length; + assert(length < INT_MAX); + unicode->length = (int)length; reset: /* Reset the object caches */ @@ -176,7 +177,7 @@ int unicode_resize(register PyUnicodeObject *unicode, */ static -PyUnicodeObject *_PyUnicode_New(int length) +PyUnicodeObject *_PyUnicode_New(Py_ssize_t length) { register PyUnicodeObject *unicode; @@ -225,7 +226,8 @@ PyUnicodeObject *_PyUnicode_New(int length) */ unicode->str[0] = 0; unicode->str[length] = 0; - unicode->length = length; + assert(length<INT_MAX); + unicode->length = (int)length; unicode->hash = -1; unicode->defenc = NULL; return unicode; @@ -263,7 +265,7 @@ void unicode_dealloc(register PyUnicodeObject *unicode) } } -int PyUnicode_Resize(PyObject **unicode, int length) +int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length) { register PyUnicodeObject *v; @@ -303,7 +305,7 @@ int PyUnicode_Resize(PyObject **unicode, int length) PyUnicode_Resize(((PyObject **)(unicodevar)), length) PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, - int size) + Py_ssize_t size) { PyUnicodeObject *unicode; @@ -347,7 +349,7 @@ PyObject *PyUnicode_FromUnicode(const Py_UNICODE *u, #ifdef HAVE_WCHAR_H PyObject *PyUnicode_FromWideChar(register const wchar_t *w, - int size) + Py_ssize_t size) { PyUnicodeObject *unicode; @@ -376,9 +378,9 @@ PyObject *PyUnicode_FromWideChar(register const wchar_t *w, return (PyObject *)unicode; } -int PyUnicode_AsWideChar(PyUnicodeObject *unicode, - register wchar_t *w, - int size) +Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, + wchar_t *w, + Py_ssize_t size) { if (unicode == NULL) { PyErr_BadInternalCall(); @@ -455,7 +457,7 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, const char *errors) { const char *s = NULL; - int len; + Py_ssize_t len; PyObject *v; if (obj == NULL) { @@ -520,7 +522,7 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj, } PyObject *PyUnicode_Decode(const char *s, - int size, + Py_ssize_t size, const char *encoding, const char *errors) { @@ -588,7 +590,7 @@ PyObject *PyUnicode_AsDecodedObject(PyObject *unicode, } PyObject *PyUnicode_Encode(const Py_UNICODE *s, - int size, + Py_ssize_t size, const char *encoding, const char *errors) { @@ -696,7 +698,7 @@ Py_UNICODE *PyUnicode_AsUnicode(PyObject *unicode) return NULL; } -int PyUnicode_GetSize(PyObject *unicode) +Py_ssize_t PyUnicode_GetSize(PyObject *unicode) { if (!PyUnicode_Check(unicode)) { PyErr_BadArgument(); @@ -742,18 +744,18 @@ int PyUnicode_SetDefaultEncoding(const char *encoding) static int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler, const char *encoding, const char *reason, - const char *input, int insize, int *startinpos, int *endinpos, PyObject **exceptionObject, const char **inptr, - PyObject **output, int *outpos, Py_UNICODE **outptr) + const char *input, Py_ssize_t insize, Py_ssize_t *startinpos, Py_ssize_t *endinpos, PyObject **exceptionObject, const char **inptr, + PyObject **output, Py_ssize_t *outpos, Py_UNICODE **outptr) { - static char *argparse = "O!i;decoding error handler must return (unicode, int) tuple"; + static char *argparse = "O!n;decoding error handler must return (unicode, int) tuple"; PyObject *restuple = NULL; PyObject *repunicode = NULL; - int outsize = PyUnicode_GET_SIZE(*output); - int requiredsize; - int newpos; + Py_ssize_t outsize = PyUnicode_GET_SIZE(*output); + Py_ssize_t requiredsize; + Py_ssize_t newpos; Py_UNICODE *repptr; - int repsize; + Py_ssize_t repsize; int res = -1; if (*errorHandler == NULL) { @@ -789,7 +791,8 @@ int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler if (newpos<0) newpos = insize+newpos; if (newpos<0 || newpos>insize) { - PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", newpos); + /* XXX %zd? */ + PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", (int)newpos); goto onError; } @@ -887,13 +890,13 @@ char utf7_special[128] = { } PyObject *PyUnicode_DecodeUTF7(const char *s, - int size, + Py_ssize_t size, const char *errors) { const char *starts = s; - int startinpos; - int endinpos; - int outpos; + Py_ssize_t startinpos; + Py_ssize_t endinpos; + Py_ssize_t outpos; const char *e; PyUnicodeObject *unicode; Py_UNICODE *p; @@ -1023,16 +1026,16 @@ onError: PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s, - int size, + Py_ssize_t size, int encodeSetO, int encodeWhiteSpace, const char *errors) { PyObject *v; /* It might be possible to tighten this worst case */ - unsigned int cbAllocated = 5 * size; + Py_ssize_t cbAllocated = 5 * size; int inShift = 0; - int i = 0; + Py_ssize_t i = 0; unsigned int bitsleft = 0; unsigned long charsleft = 0; char * out; @@ -1147,22 +1150,22 @@ char utf8_code_length[256] = { }; PyObject *PyUnicode_DecodeUTF8(const char *s, - int size, + Py_ssize_t size, const char *errors) { return PyUnicode_DecodeUTF8Stateful(s, size, errors, NULL); } PyObject *PyUnicode_DecodeUTF8Stateful(const char *s, - int size, + Py_ssize_t size, const char *errors, - int *consumed) + Py_ssize_t *consumed) { const char *starts = s; int n; - int startinpos; - int endinpos; - int outpos; + Py_ssize_t startinpos; + Py_ssize_t endinpos; + Py_ssize_t outpos; const char *e; PyUnicodeObject *unicode; Py_UNICODE *p; @@ -1347,15 +1350,15 @@ onError: */ PyObject * PyUnicode_EncodeUTF8(const Py_UNICODE *s, - int size, + Py_ssize_t size, const char *errors) { #define MAX_SHORT_UNICHARS 300 /* largest size we'll do on the stack */ - int i; /* index into s of next input byte */ + Py_ssize_t i; /* index into s of next input byte */ PyObject *v; /* result string object */ char *p; /* next free byte in output buffer */ - int nallocated; /* number of result bytes allocated */ + Py_ssize_t nallocated; /* number of result bytes allocated */ int nneeded; /* number of result bytes needed */ char stackbuf[MAX_SHORT_UNICHARS * 4]; @@ -1455,7 +1458,7 @@ PyObject *PyUnicode_AsUTF8String(PyObject *unicode) PyObject * PyUnicode_DecodeUTF16(const char *s, - int size, + Py_ssize_t size, const char *errors, int *byteorder) { @@ -1464,15 +1467,15 @@ PyUnicode_DecodeUTF16(const char *s, PyObject * PyUnicode_DecodeUTF16Stateful(const char *s, - int size, + Py_ssize_t size, const char *errors, int *byteorder, - int *consumed) + Py_ssize_t *consumed) { const char *starts = s; - int startinpos; - int endinpos; - int outpos; + Py_ssize_t startinpos; + Py_ssize_t endinpos; + Py_ssize_t outpos; PyUnicodeObject *unicode; Py_UNICODE *p; const unsigned char *q, *e; @@ -1630,7 +1633,7 @@ onError: PyObject * PyUnicode_EncodeUTF16(const Py_UNICODE *s, - int size, + Py_ssize_t size, const char *errors, int byteorder) { @@ -1716,13 +1719,13 @@ PyObject *PyUnicode_AsUTF16String(PyObject *unicode) static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL; PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, - int size, + Py_ssize_t size, const char *errors) { const char *starts = s; - int startinpos; - int endinpos; - int outpos; + Py_ssize_t startinpos; + Py_ssize_t endinpos; + Py_ssize_t outpos; int i; PyUnicodeObject *v; Py_UNICODE *p; @@ -1896,7 +1899,7 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, /* found a name. look it up in the unicode database */ message = "unknown Unicode character name"; s++; - if (ucnhash_CAPI->getcode(start, s-start-1, &chr)) + if (ucnhash_CAPI->getcode(start, (int)(s-start-1), &chr)) goto store; } } @@ -1962,12 +1965,12 @@ onError: */ static const Py_UNICODE *findchar(const Py_UNICODE *s, - int size, + Py_ssize_t size, Py_UNICODE ch); static PyObject *unicodeescape_string(const Py_UNICODE *s, - int size, + Py_ssize_t size, int quotes) { PyObject *repr; @@ -2093,7 +2096,7 @@ PyObject *unicodeescape_string(const Py_UNICODE *s, } PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, - int size) + Py_ssize_t size) { return unicodeescape_string(s, size, 0); } @@ -2111,13 +2114,13 @@ PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode) /* --- Raw Unicode Escape Codec ------------------------------------------- */ PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, - int size, + Py_ssize_t size, const char *errors) { const char *starts = s; - int startinpos; - int endinpos; - int outpos; + Py_ssize_t startinpos; + Py_ssize_t endinpos; + Py_ssize_t outpos; PyUnicodeObject *v; Py_UNICODE *p; const char *end; @@ -2216,7 +2219,7 @@ PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s, } PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, - int size) + Py_ssize_t size) { PyObject *repr; char *p; @@ -2284,13 +2287,13 @@ PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) /* --- Unicode Internal Codec ------------------------------------------- */ PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, - int size, + Py_ssize_t size, const char *errors) { const char *starts = s; - int startinpos; - int endinpos; - int outpos; + Py_ssize_t startinpos; + Py_ssize_t endinpos; + Py_ssize_t outpos; PyUnicodeObject *v; Py_UNICODE *p; const char *end; @@ -2361,7 +2364,7 @@ PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s, /* --- Latin-1 Codec ------------------------------------------------------ */ PyObject *PyUnicode_DecodeLatin1(const char *s, - int size, + Py_ssize_t size, const char *errors) { PyUnicodeObject *v; @@ -2391,8 +2394,8 @@ PyObject *PyUnicode_DecodeLatin1(const char *s, /* create or adjust a UnicodeEncodeError */ static void make_encode_exception(PyObject **exceptionObject, const char *encoding, - const Py_UNICODE *unicode, int size, - int startpos, int endpos, + const Py_UNICODE *unicode, Py_ssize_t size, + Py_ssize_t startpos, Py_ssize_t endpos, const char *reason) { if (*exceptionObject == NULL) { @@ -2416,8 +2419,8 @@ static void make_encode_exception(PyObject **exceptionObject, /* raises a UnicodeEncodeError */ static void raise_encode_exception(PyObject **exceptionObject, const char *encoding, - const Py_UNICODE *unicode, int size, - int startpos, int endpos, + const Py_UNICODE *unicode, Py_ssize_t size, + Py_ssize_t startpos, Py_ssize_t endpos, const char *reason) { make_encode_exception(exceptionObject, @@ -2433,11 +2436,11 @@ static void raise_encode_exception(PyObject **exceptionObject, static PyObject *unicode_encode_call_errorhandler(const char *errors, PyObject **errorHandler, const char *encoding, const char *reason, - const Py_UNICODE *unicode, int size, PyObject **exceptionObject, - int startpos, int endpos, - int *newpos) + const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, + Py_ssize_t startpos, Py_ssize_t endpos, + Py_ssize_t *newpos) { - static char *argparse = "O!i;encoding error handler must return (unicode, int) tuple"; + static char *argparse = "O!n;encoding error handler must return (unicode, int) tuple"; PyObject *restuple; PyObject *resunicode; @@ -2470,7 +2473,8 @@ static PyObject *unicode_encode_call_errorhandler(const char *errors, if (*newpos<0) *newpos = size+*newpos; if (*newpos<0 || *newpos>size) { - PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); + /* XXX %zd? */ + PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", (int)*newpos); Py_DECREF(restuple); return NULL; } @@ -2480,7 +2484,7 @@ static PyObject *unicode_encode_call_errorhandler(const char *errors, } static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, - int size, + Py_ssize_t size, const char *errors, int limit) { @@ -2494,8 +2498,8 @@ static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, /* pointer into the output */ char *str; /* current output position */ - int respos = 0; - int ressize; + Py_ssize_t respos = 0; + Py_ssize_t ressize; char *encoding = (limit == 256) ? "latin-1" : "ascii"; char *reason = (limit == 256) ? "ordinal not in range(256)" : "ordinal not in range(128)"; PyObject *errorHandler = NULL; @@ -2524,12 +2528,12 @@ static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, ++p; } else { - int unicodepos = p-startp; - int requiredsize; + Py_ssize_t unicodepos = p-startp; + Py_ssize_t requiredsize; PyObject *repunicode; - int repsize; - int newpos; - int respos; + Py_ssize_t repsize; + Py_ssize_t newpos; + Py_ssize_t respos; Py_UNICODE *uni2; /* startpos for collecting unencodable chars */ const Py_UNICODE *collstart = p; @@ -2655,7 +2659,7 @@ static PyObject *unicode_encode_ucs1(const Py_UNICODE *p, } PyObject *PyUnicode_EncodeLatin1(const Py_UNICODE *p, - int size, + Py_ssize_t size, const char *errors) { return unicode_encode_ucs1(p, size, errors, 256); @@ -2675,15 +2679,15 @@ PyObject *PyUnicode_AsLatin1String(PyObject *unicode) /* --- 7-bit ASCII Codec -------------------------------------------------- */ PyObject *PyUnicode_DecodeASCII(const char *s, - int size, + Py_ssize_t size, const char *errors) { const char *starts = s; PyUnicodeObject *v; Py_UNICODE *p; - int startinpos; - int endinpos; - int outpos; + Py_ssize_t startinpos; + Py_ssize_t endinpos; + Py_ssize_t outpos; const char *e; PyObject *errorHandler = NULL; PyObject *exc = NULL; @@ -2734,7 +2738,7 @@ PyObject *PyUnicode_DecodeASCII(const char *s, } PyObject *PyUnicode_EncodeASCII(const Py_UNICODE *p, - int size, + Py_ssize_t size, const char *errors) { return unicode_encode_ucs1(p, size, errors, 128); @@ -2756,14 +2760,16 @@ PyObject *PyUnicode_AsASCIIString(PyObject *unicode) /* --- MBCS codecs for Windows -------------------------------------------- */ PyObject *PyUnicode_DecodeMBCS(const char *s, - int size, + Py_ssize_t size, const char *errors) { PyUnicodeObject *v; Py_UNICODE *p; + DWORD usize; /* First get the size of the result */ - DWORD usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0); + assert(size < INT_MAX); + usize = MultiByteToWideChar(CP_ACP, 0, s, (int)size, NULL, 0); if (size > 0 && usize==0) return PyErr_SetFromWindowsErrWithFilename(0, NULL); @@ -2773,7 +2779,7 @@ PyObject *PyUnicode_DecodeMBCS(const char *s, if (usize == 0) return (PyObject *)v; p = PyUnicode_AS_UNICODE(v); - if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) { + if (0 == MultiByteToWideChar(CP_ACP, 0, s, (int)size, p, usize)) { Py_DECREF(v); return PyErr_SetFromWindowsErrWithFilename(0, NULL); } @@ -2782,7 +2788,7 @@ PyObject *PyUnicode_DecodeMBCS(const char *s, } PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, - int size, + Py_ssize_t size, const char *errors) { PyObject *repr; @@ -2794,7 +2800,8 @@ PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, return PyString_FromString(""); /* First get the size of the result */ - mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL); + assert(size<INT_MAX); + mbcssize = WideCharToMultiByte(CP_ACP, 0, p, (int)size, NULL, 0, NULL, NULL); if (mbcssize==0) return PyErr_SetFromWindowsErrWithFilename(0, NULL); @@ -2806,7 +2813,8 @@ PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p, /* Do the conversion */ s = PyString_AS_STRING(repr); - if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) { + assert(size < INT_MAX); + if (0 == WideCharToMultiByte(CP_ACP, 0, p, (int)size, s, mbcssize, NULL, NULL)) { Py_DECREF(repr); return PyErr_SetFromWindowsErrWithFilename(0, NULL); } @@ -2829,22 +2837,22 @@ PyObject *PyUnicode_AsMBCSString(PyObject *unicode) /* --- Character Mapping Codec -------------------------------------------- */ PyObject *PyUnicode_DecodeCharmap(const char *s, - int size, + Py_ssize_t size, PyObject *mapping, const char *errors) { const char *starts = s; - int startinpos; - int endinpos; - int outpos; + Py_ssize_t startinpos; + Py_ssize_t endinpos; + Py_ssize_t outpos; const char *e; PyUnicodeObject *v; Py_UNICODE *p; - int extrachars = 0; + Py_ssize_t extrachars = 0; PyObject *errorHandler = NULL; PyObject *exc = NULL; Py_UNICODE *mapstring = NULL; - int maplen = 0; + Py_ssize_t maplen = 0; /* Default to Latin-1 */ if (mapping == NULL) @@ -2934,7 +2942,7 @@ PyObject *PyUnicode_DecodeCharmap(const char *s, continue; } else if (PyUnicode_Check(x)) { - int targetsize = PyUnicode_GET_SIZE(x); + Py_ssize_t targetsize = PyUnicode_GET_SIZE(x); if (targetsize == 1) /* 1-1 mapping */ @@ -2944,8 +2952,8 @@ PyObject *PyUnicode_DecodeCharmap(const char *s, /* 1-n mapping */ if (targetsize > extrachars) { /* resize first */ - int oldpos = (int)(p - PyUnicode_AS_UNICODE(v)); - int needed = (targetsize - extrachars) + \ + Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v); + Py_ssize_t needed = (targetsize - extrachars) + \ (targetsize << 2); extrachars += needed; if (_PyUnicode_Resize(&v, @@ -3041,7 +3049,7 @@ static PyObject *charmapencode_lookup(Py_UNICODE c, PyObject *mapping) reallocation error occurred. The caller must decref the result */ static PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, - PyObject **outobj, int *outpos) + PyObject **outobj, Py_ssize_t *outpos) { PyObject *rep = charmapencode_lookup(c, mapping); @@ -3051,9 +3059,9 @@ PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, return rep; else { char *outstart = PyString_AS_STRING(*outobj); - int outsize = PyString_GET_SIZE(*outobj); + Py_ssize_t outsize = PyString_GET_SIZE(*outobj); if (PyInt_Check(rep)) { - int requiredsize = *outpos+1; + Py_ssize_t requiredsize = *outpos+1; if (outsize<requiredsize) { /* exponentially overallocate to minimize reallocations */ if (requiredsize < 2*outsize) @@ -3068,8 +3076,8 @@ PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, } else { const char *repchars = PyString_AS_STRING(rep); - int repsize = PyString_GET_SIZE(rep); - int requiredsize = *outpos+repsize; + Py_ssize_t repsize = PyString_GET_SIZE(rep); + Py_ssize_t requiredsize = *outpos+repsize; if (outsize<requiredsize) { /* exponentially overallocate to minimize reallocations */ if (requiredsize < 2*outsize) @@ -3091,19 +3099,19 @@ PyObject *charmapencode_output(Py_UNICODE c, PyObject *mapping, Return 0 on success, -1 on error */ static int charmap_encoding_error( - const Py_UNICODE *p, int size, int *inpos, PyObject *mapping, + const Py_UNICODE *p, Py_ssize_t size, Py_ssize_t *inpos, PyObject *mapping, PyObject **exceptionObject, int *known_errorHandler, PyObject **errorHandler, const char *errors, - PyObject **res, int *respos) + PyObject **res, Py_ssize_t *respos) { PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ - int repsize; - int newpos; + Py_ssize_t repsize; + Py_ssize_t newpos; Py_UNICODE *uni2; /* startpos for collecting unencodable chars */ - int collstartpos = *inpos; - int collendpos = *inpos+1; - int collpos; + Py_ssize_t collstartpos = *inpos; + Py_ssize_t collendpos = *inpos+1; + Py_ssize_t collpos; char *encoding = "charmap"; char *reason = "character maps to <undefined>"; @@ -3204,16 +3212,16 @@ int charmap_encoding_error( } PyObject *PyUnicode_EncodeCharmap(const Py_UNICODE *p, - int size, + Py_ssize_t size, PyObject *mapping, const char *errors) { /* output object */ PyObject *res = NULL; /* current input position */ - int inpos = 0; + Py_ssize_t inpos = 0; /* current output position */ - int respos = 0; + Py_ssize_t respos = 0; PyObject *errorHandler = NULL; PyObject *exc = NULL; /* the following variable is used for caching string comparisons @@ -3284,8 +3292,8 @@ PyObject *PyUnicode_AsCharmapString(PyObject *unicode, /* create or adjust a UnicodeTranslateError */ static void make_translate_exception(PyObject **exceptionObject, - const Py_UNICODE *unicode, int size, - int startpos, int endpos, + const Py_UNICODE *unicode, Py_ssize_t size, + Py_ssize_t startpos, Py_ssize_t endpos, const char *reason) { if (*exceptionObject == NULL) { @@ -3308,8 +3316,8 @@ static void make_translate_exception(PyObject **exceptionObject, /* raises a UnicodeTranslateError */ static void raise_translate_exception(PyObject **exceptionObject, - const Py_UNICODE *unicode, int size, - int startpos, int endpos, + const Py_UNICODE *unicode, Py_ssize_t size, + Py_ssize_t startpos, Py_ssize_t endpos, const char *reason) { make_translate_exception(exceptionObject, @@ -3325,12 +3333,13 @@ static void raise_translate_exception(PyObject **exceptionObject, static PyObject *unicode_translate_call_errorhandler(const char *errors, PyObject **errorHandler, const char *reason, - const Py_UNICODE *unicode, int size, PyObject **exceptionObject, - int startpos, int endpos, - int *newpos) + const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject, + Py_ssize_t startpos, Py_ssize_t endpos, + Py_ssize_t *newpos) { static char *argparse = "O!i;translating error handler must return (unicode, int) tuple"; + int i_newpos; PyObject *restuple; PyObject *resunicode; @@ -3355,14 +3364,17 @@ static PyObject *unicode_translate_call_errorhandler(const char *errors, return NULL; } if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, - &resunicode, newpos)) { + &resunicode, &i_newpos)) { Py_DECREF(restuple); return NULL; } - if (*newpos<0) - *newpos = size+*newpos; + if (i_newpos<0) + *newpos = size+i_newpos; + else + *newpos = i_newpos; if (*newpos<0 || *newpos>size) { - PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); + /* XXX %zd? */ + PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", (int)*newpos); Py_DECREF(restuple); return NULL; } @@ -3426,12 +3438,12 @@ if not reallocate and adjust various state variables. Return 0 on success, -1 on error */ static int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, - int requiredsize) + Py_ssize_t requiredsize) { - int oldsize = PyUnicode_GET_SIZE(*outobj); + Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj); if (requiredsize > oldsize) { /* remember old output position */ - int outpos = *outp-PyUnicode_AS_UNICODE(*outobj); + Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj); /* exponentially overallocate to minimize reallocations */ if (requiredsize < 2 * oldsize) requiredsize = 2 * oldsize; @@ -3449,7 +3461,7 @@ int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, Return 0 on success, -1 on error. */ static int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp, - int insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, + Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp, PyObject **res) { if (charmaptranslate_lookup(*curinp, mapping, res)) @@ -3465,14 +3477,14 @@ int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp *(*outp)++ = (Py_UNICODE)PyInt_AS_LONG(*res); } else if (PyUnicode_Check(*res)) { - int repsize = PyUnicode_GET_SIZE(*res); + Py_ssize_t repsize = PyUnicode_GET_SIZE(*res); if (repsize==1) { /* no overflow check, because we know that the space is enough */ *(*outp)++ = *PyUnicode_AS_UNICODE(*res); } else if (repsize!=0) { /* more than one character */ - int requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + + Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) + (insize - (curinp-startinp)) + repsize - 1; if (charmaptranslate_makespace(outobj, outp, requiredsize)) @@ -3487,7 +3499,7 @@ int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp } PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, - int size, + Py_ssize_t size, PyObject *mapping, const char *errors) { @@ -3499,7 +3511,7 @@ PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, /* pointer into the output */ Py_UNICODE *str; /* current output position */ - int respos = 0; + Py_ssize_t respos = 0; char *reason = "character maps to <undefined>"; PyObject *errorHandler = NULL; PyObject *exc = NULL; @@ -3534,8 +3546,8 @@ PyObject *PyUnicode_TranslateCharmap(const Py_UNICODE *p, ++p; else { /* untranslatable character */ PyObject *repunicode = NULL; /* initialize to prevent gcc warning */ - int repsize; - int newpos; + Py_ssize_t repsize; + Py_ssize_t newpos; Py_UNICODE *uni2; /* startpos for collecting untranslatable chars */ const Py_UNICODE *collstart = p; @@ -3652,7 +3664,7 @@ PyObject *PyUnicode_Translate(PyObject *str, /* --- Decimal Encoder ---------------------------------------------------- */ int PyUnicode_EncodeDecimal(Py_UNICODE *s, - int length, + Py_ssize_t length, char *output, const char *errors) { @@ -3676,8 +3688,8 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s, register Py_UNICODE ch = *p; int decimal; PyObject *repunicode; - int repsize; - int newpos; + Py_ssize_t repsize; + Py_ssize_t newpos; Py_UNICODE *uni2; Py_UNICODE *collstart; Py_UNICODE *collend; @@ -3783,10 +3795,10 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s, /* --- Helpers ------------------------------------------------------------ */ static -int count(PyUnicodeObject *self, - int start, - int end, - PyUnicodeObject *substring) +Py_ssize_t count(PyUnicodeObject *self, + Py_ssize_t start, + Py_ssize_t end, + PyUnicodeObject *substring) { int count = 0; @@ -3816,12 +3828,12 @@ int count(PyUnicodeObject *self, return count; } -int PyUnicode_Count(PyObject *str, +Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, - int start, - int end) + Py_ssize_t start, + Py_ssize_t end) { - int result; + Py_ssize_t result; str = PyUnicode_FromObject(str); if (str == NULL) @@ -3842,10 +3854,10 @@ int PyUnicode_Count(PyObject *str, } static -int findstring(PyUnicodeObject *self, +Py_ssize_t findstring(PyUnicodeObject *self, PyUnicodeObject *substring, - int start, - int end, + Py_ssize_t start, + Py_ssize_t end, int direction) { if (start < 0) @@ -3878,13 +3890,13 @@ int findstring(PyUnicodeObject *self, return -1; } -int PyUnicode_Find(PyObject *str, +Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, - int start, - int end, + Py_ssize_t start, + Py_ssize_t end, int direction) { - int result; + Py_ssize_t result; str = PyUnicode_FromObject(str); if (str == NULL) @@ -3906,8 +3918,8 @@ int PyUnicode_Find(PyObject *str, static int tailmatch(PyUnicodeObject *self, PyUnicodeObject *substring, - int start, - int end, + Py_ssize_t start, + Py_ssize_t end, int direction) { if (start < 0) @@ -3940,13 +3952,13 @@ int tailmatch(PyUnicodeObject *self, return 0; } -int PyUnicode_Tailmatch(PyObject *str, +Py_ssize_t PyUnicode_Tailmatch(PyObject *str, PyObject *substr, - int start, - int end, + Py_ssize_t start, + Py_ssize_t end, int direction) { - int result; + Py_ssize_t result; str = PyUnicode_FromObject(str); if (str == NULL) @@ -3967,7 +3979,7 @@ int PyUnicode_Tailmatch(PyObject *str, static const Py_UNICODE *findchar(const Py_UNICODE *s, - int size, + Py_ssize_t size, Py_UNICODE ch) { /* like wcschr, but doesn't stop at NULL characters */ @@ -4011,7 +4023,7 @@ PyObject *fixup(PyUnicodeObject *self, static int fixupper(PyUnicodeObject *self) { - int len = self->length; + Py_ssize_t len = self->length; Py_UNICODE *s = self->str; int status = 0; @@ -4032,7 +4044,7 @@ int fixupper(PyUnicodeObject *self) static int fixlower(PyUnicodeObject *self) { - int len = self->length; + Py_ssize_t len = self->length; Py_UNICODE *s = self->str; int status = 0; @@ -4053,7 +4065,7 @@ int fixlower(PyUnicodeObject *self) static int fixswapcase(PyUnicodeObject *self) { - int len = self->length; + Py_ssize_t len = self->length; Py_UNICODE *s = self->str; int status = 0; @@ -4074,7 +4086,7 @@ int fixswapcase(PyUnicodeObject *self) static int fixcapitalize(PyUnicodeObject *self) { - int len = self->length; + Py_ssize_t len = self->length; Py_UNICODE *s = self->str; int status = 0; @@ -4145,7 +4157,7 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) size_t res_used; /* # used bytes */ Py_UNICODE *res_p; /* pointer to free byte in res's string area */ PyObject *fseq; /* PySequence_Fast(seq) */ - int seqlen; /* len(fseq) -- number of items in sequence */ + Py_ssize_t seqlen; /* len(fseq) -- number of items in sequence */ PyObject *item; int i; @@ -4285,8 +4297,8 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) static PyUnicodeObject *pad(PyUnicodeObject *self, - int left, - int right, + Py_ssize_t left, + Py_ssize_t right, Py_UNICODE fill) { PyUnicodeObject *u; @@ -4338,11 +4350,11 @@ PyUnicodeObject *pad(PyUnicodeObject *self, static PyObject *split_whitespace(PyUnicodeObject *self, PyObject *list, - int maxcount) + Py_ssize_t maxcount) { - register int i; - register int j; - int len = self->length; + register Py_ssize_t i; + register Py_ssize_t j; + Py_ssize_t len = self->length; PyObject *str; for (i = j = 0; i < len; ) { @@ -4374,9 +4386,9 @@ PyObject *split_whitespace(PyUnicodeObject *self, PyObject *PyUnicode_Splitlines(PyObject *string, int keepends) { - register int i; - register int j; - int len; + register Py_ssize_t i; + register Py_ssize_t j; + Py_ssize_t len; PyObject *list; PyObject *str; Py_UNICODE *data; @@ -4392,7 +4404,7 @@ PyObject *PyUnicode_Splitlines(PyObject *string, goto onError; for (i = j = 0; i < len; ) { - int eol; + Py_ssize_t eol; /* Find a line and append it */ while (i < len && !Py_UNICODE_ISLINEBREAK(data[i])) @@ -4429,11 +4441,11 @@ static PyObject *split_char(PyUnicodeObject *self, PyObject *list, Py_UNICODE ch, - int maxcount) + Py_ssize_t maxcount) { - register int i; - register int j; - int len = self->length; + register Py_ssize_t i; + register Py_ssize_t j; + Py_ssize_t len = self->length; PyObject *str; for (i = j = 0; i < len; ) { @@ -4459,12 +4471,12 @@ static PyObject *split_substring(PyUnicodeObject *self, PyObject *list, PyUnicodeObject *substring, - int maxcount) + Py_ssize_t maxcount) { - register int i; - register int j; - int len = self->length; - int sublen = substring->length; + register Py_ssize_t i; + register Py_ssize_t j; + Py_ssize_t len = self->length; + Py_ssize_t sublen = substring->length; PyObject *str; for (i = j = 0; i <= len - sublen; ) { @@ -4489,11 +4501,11 @@ PyObject *split_substring(PyUnicodeObject *self, static PyObject *rsplit_whitespace(PyUnicodeObject *self, PyObject *list, - int maxcount) + Py_ssize_t maxcount) { - register int i; - register int j; - int len = self->length; + register Py_ssize_t i; + register Py_ssize_t j; + Py_ssize_t len = self->length; PyObject *str; for (i = j = len - 1; i >= 0; ) { @@ -4526,11 +4538,11 @@ static PyObject *rsplit_char(PyUnicodeObject *self, PyObject *list, Py_UNICODE ch, - int maxcount) + Py_ssize_t maxcount) { - register int i; - register int j; - int len = self->length; + register Py_ssize_t i; + register Py_ssize_t j; + Py_ssize_t len = self->length; PyObject *str; for (i = j = len - 1; i >= 0; ) { @@ -4556,12 +4568,12 @@ static PyObject *rsplit_substring(PyUnicodeObject *self, PyObject *list, PyUnicodeObject *substring, - int maxcount) + Py_ssize_t maxcount) { - register int i; - register int j; - int len = self->length; - int sublen = substring->length; + register Py_ssize_t i; + register Py_ssize_t j; + Py_ssize_t len = self->length; + Py_ssize_t sublen = substring->length; PyObject *str; for (i = len - sublen, j = len; i >= 0; ) { @@ -4590,7 +4602,7 @@ PyObject *rsplit_substring(PyUnicodeObject *self, static PyObject *split(PyUnicodeObject *self, PyUnicodeObject *substring, - int maxcount) + Py_ssize_t maxcount) { PyObject *list; @@ -4619,7 +4631,7 @@ PyObject *split(PyUnicodeObject *self, static PyObject *rsplit(PyUnicodeObject *self, PyUnicodeObject *substring, - int maxcount) + Py_ssize_t maxcount) { PyObject *list; @@ -4649,7 +4661,7 @@ static PyObject *replace(PyUnicodeObject *self, PyUnicodeObject *str1, PyUnicodeObject *str2, - int maxcount) + Py_ssize_t maxcount) { PyUnicodeObject *u; @@ -4686,7 +4698,7 @@ PyObject *replace(PyUnicodeObject *self, } } else { - int n, i; + Py_ssize_t n, i; Py_UNICODE *p; /* replace strings */ @@ -4778,7 +4790,7 @@ unicode_capwords(PyUnicodeObject *self) { PyObject *list; PyObject *item; - int i; + Py_ssize_t i; /* Split into words */ list = split(self, NULL, -1); @@ -4840,8 +4852,8 @@ done using the specified fill character (default is a space)"); static PyObject * unicode_center(PyUnicodeObject *self, PyObject *args) { - int marg, left; - int width; + Py_ssize_t marg, left; + Py_ssize_t width; Py_UNICODE fillchar = ' '; if (!PyArg_ParseTuple(args, "i|O&:center", &width, convert_uc, &fillchar)) @@ -4879,7 +4891,7 @@ static short utf16Fixup[32] = static int unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) { - int len1, len2; + Py_ssize_t len1, len2; Py_UNICODE *s1 = str1->str; Py_UNICODE *s2 = str2->str; @@ -4913,7 +4925,7 @@ unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) static int unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2) { - register int len1, len2; + register Py_ssize_t len1, len2; Py_UNICODE *s1 = str1->str; Py_UNICODE *s2 = str2->str; @@ -4975,7 +4987,8 @@ int PyUnicode_Contains(PyObject *container, PyObject *element) { PyUnicodeObject *u = NULL, *v = NULL; - int result, size; + int result; + Py_ssize_t size; register const Py_UNICODE *lhs, *end, *rhs; /* Coerce the two arguments */ @@ -5076,8 +5089,8 @@ static PyObject * unicode_count(PyUnicodeObject *self, PyObject *args) { PyUnicodeObject *substring; - int start = 0; - int end = INT_MAX; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; PyObject *result; if (!PyArg_ParseTuple(args, "O|O&O&:count", &substring, @@ -5190,7 +5203,7 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args) Py_UNICODE *e; Py_UNICODE *p; Py_UNICODE *q; - int i, j; + Py_ssize_t i, j; PyUnicodeObject *u; int tabsize = 8; @@ -5253,8 +5266,8 @@ static PyObject * unicode_find(PyUnicodeObject *self, PyObject *args) { PyUnicodeObject *substring; - int start = 0; - int end = INT_MAX; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; PyObject *result; if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring, @@ -5265,14 +5278,14 @@ unicode_find(PyUnicodeObject *self, PyObject *args) if (substring == NULL) return NULL; - result = PyInt_FromLong(findstring(self, substring, start, end, 1)); + result = PyInt_FromSsize_t(findstring(self, substring, start, end, 1)); Py_DECREF(substring); return result; } static PyObject * -unicode_getitem(PyUnicodeObject *self, int index) +unicode_getitem(PyUnicodeObject *self, Py_ssize_t index) { if (index < 0 || index >= self->length) { PyErr_SetString(PyExc_IndexError, "string index out of range"); @@ -5291,7 +5304,7 @@ unicode_hash(PyUnicodeObject *self) strings and Unicode objects behave in the same way as dictionary keys. */ - register int len; + register Py_ssize_t len; register Py_UNICODE *p; register long x; @@ -5317,10 +5330,10 @@ Like S.find() but raise ValueError when the substring is not found."); static PyObject * unicode_index(PyUnicodeObject *self, PyObject *args) { - int result; + Py_ssize_t result; PyUnicodeObject *substring; - int start = 0; - int end = INT_MAX; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring, _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) @@ -5338,7 +5351,7 @@ unicode_index(PyUnicodeObject *self, PyObject *args) PyErr_SetString(PyExc_ValueError, "substring not found"); return NULL; } - return PyInt_FromLong(result); + return PyInt_FromSsize_t(result); } PyDoc_STRVAR(islower__doc__, @@ -5643,7 +5656,7 @@ unicode_join(PyObject *self, PyObject *data) return PyUnicode_Join(self, data); } -static int +static Py_ssize_t unicode_length(PyUnicodeObject *self) { return self->length; @@ -5707,10 +5720,10 @@ PyObject * _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj) { Py_UNICODE *s = PyUnicode_AS_UNICODE(self); - int len = PyUnicode_GET_SIZE(self); + Py_ssize_t len = PyUnicode_GET_SIZE(self); Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj); - int seplen = PyUnicode_GET_SIZE(sepobj); - int i, j; + Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj); + Py_ssize_t i, j; i = 0; if (striptype != RIGHTSTRIP) { @@ -5740,7 +5753,7 @@ static PyObject * do_strip(PyUnicodeObject *self, int striptype) { Py_UNICODE *s = PyUnicode_AS_UNICODE(self); - int len = PyUnicode_GET_SIZE(self), i, j; + Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j; i = 0; if (striptype != RIGHTSTRIP) { @@ -5851,11 +5864,11 @@ unicode_rstrip(PyUnicodeObject *self, PyObject *args) static PyObject* -unicode_repeat(PyUnicodeObject *str, int len) +unicode_repeat(PyUnicodeObject *str, Py_ssize_t len) { PyUnicodeObject *u; Py_UNICODE *p; - int nchars; + Py_ssize_t nchars; size_t nbytes; if (len < 0) @@ -5899,7 +5912,7 @@ unicode_repeat(PyUnicodeObject *str, int len) PyObject *PyUnicode_Replace(PyObject *obj, PyObject *subobj, PyObject *replobj, - int maxcount) + Py_ssize_t maxcount) { PyObject *self; PyObject *str1; @@ -5942,10 +5955,10 @@ unicode_replace(PyUnicodeObject *self, PyObject *args) { PyUnicodeObject *str1; PyUnicodeObject *str2; - int maxcount = -1; + Py_ssize_t maxcount = -1; PyObject *result; - if (!PyArg_ParseTuple(args, "OO|i:replace", &str1, &str2, &maxcount)) + if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount)) return NULL; str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1); if (str1 == NULL) @@ -5984,8 +5997,8 @@ static PyObject * unicode_rfind(PyUnicodeObject *self, PyObject *args) { PyUnicodeObject *substring; - int start = 0; - int end = INT_MAX; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; PyObject *result; if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring, @@ -5996,7 +6009,7 @@ unicode_rfind(PyUnicodeObject *self, PyObject *args) if (substring == NULL) return NULL; - result = PyInt_FromLong(findstring(self, substring, start, end, -1)); + result = PyInt_FromSsize_t(findstring(self, substring, start, end, -1)); Py_DECREF(substring); return result; @@ -6010,10 +6023,10 @@ Like S.rfind() but raise ValueError when the substring is not found."); static PyObject * unicode_rindex(PyUnicodeObject *self, PyObject *args) { - int result; + Py_ssize_t result; PyUnicodeObject *substring; - int start = 0; - int end = INT_MAX; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring, _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) @@ -6030,7 +6043,7 @@ unicode_rindex(PyUnicodeObject *self, PyObject *args) PyErr_SetString(PyExc_ValueError, "substring not found"); return NULL; } - return PyInt_FromLong(result); + return PyInt_FromSsize_t(result); } PyDoc_STRVAR(rjust__doc__, @@ -6057,7 +6070,7 @@ unicode_rjust(PyUnicodeObject *self, PyObject *args) } static PyObject* -unicode_slice(PyUnicodeObject *self, int start, int end) +unicode_slice(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t end) { /* standard clamping */ if (start < 0) @@ -6080,7 +6093,7 @@ unicode_slice(PyUnicodeObject *self, int start, int end) PyObject *PyUnicode_Split(PyObject *s, PyObject *sep, - int maxsplit) + Py_ssize_t maxsplit) { PyObject *result; @@ -6114,9 +6127,9 @@ static PyObject* unicode_split(PyUnicodeObject *self, PyObject *args) { PyObject *substring = Py_None; - int maxcount = -1; + Py_ssize_t maxcount = -1; - if (!PyArg_ParseTuple(args, "|Oi:split", &substring, &maxcount)) + if (!PyArg_ParseTuple(args, "|On:split", &substring, &maxcount)) return NULL; if (substring == Py_None) @@ -6129,7 +6142,7 @@ unicode_split(PyUnicodeObject *self, PyObject *args) PyObject *PyUnicode_RSplit(PyObject *s, PyObject *sep, - int maxsplit) + Py_ssize_t maxsplit) { PyObject *result; @@ -6164,9 +6177,9 @@ static PyObject* unicode_rsplit(PyUnicodeObject *self, PyObject *args) { PyObject *substring = Py_None; - int maxcount = -1; + Py_ssize_t maxcount = -1; - if (!PyArg_ParseTuple(args, "|Oi:rsplit", &substring, &maxcount)) + if (!PyArg_ParseTuple(args, "|On:rsplit", &substring, &maxcount)) return NULL; if (substring == Py_None) @@ -6251,11 +6264,11 @@ of the specified width. The string x is never truncated."); static PyObject * unicode_zfill(PyUnicodeObject *self, PyObject *args) { - int fill; + Py_ssize_t fill; PyUnicodeObject *u; - int width; - if (!PyArg_ParseTuple(args, "i:zfill", &width)) + Py_ssize_t width; + if (!PyArg_ParseTuple(args, "n:zfill", &width)) return NULL; if (self->length >= width) { @@ -6306,8 +6319,8 @@ unicode_startswith(PyUnicodeObject *self, PyObject *args) { PyUnicodeObject *substring; - int start = 0; - int end = INT_MAX; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; PyObject *result; if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &substring, @@ -6337,8 +6350,8 @@ unicode_endswith(PyUnicodeObject *self, PyObject *args) { PyUnicodeObject *substring; - int start = 0; - int end = INT_MAX; + Py_ssize_t start = 0; + Py_ssize_t end = INT_MAX; PyObject *result; if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &substring, @@ -6439,11 +6452,11 @@ static PyNumberMethods unicode_as_number = { }; static PySequenceMethods unicode_as_sequence = { - (inquiry) unicode_length, /* sq_length */ + (lenfunc) unicode_length, /* sq_length */ (binaryfunc) PyUnicode_Concat, /* sq_concat */ - (intargfunc) unicode_repeat, /* sq_repeat */ - (intargfunc) unicode_getitem, /* sq_item */ - (intintargfunc) unicode_slice, /* sq_slice */ + (ssizeargfunc) unicode_repeat, /* sq_repeat */ + (ssizeargfunc) unicode_getitem, /* sq_item */ + (ssizessizeargfunc) unicode_slice, /* sq_slice */ 0, /* sq_ass_item */ 0, /* sq_ass_slice */ (objobjproc)PyUnicode_Contains, /*sq_contains*/ @@ -6452,20 +6465,15 @@ static PySequenceMethods unicode_as_sequence = { static PyObject* unicode_subscript(PyUnicodeObject* self, PyObject* item) { - if (PyInt_Check(item)) { - long i = PyInt_AS_LONG(item); - if (i < 0) - i += PyUnicode_GET_SIZE(self); - return unicode_getitem(self, i); - } else if (PyLong_Check(item)) { - long i = PyLong_AsLong(item); + if (PyInt_Check(item) || PyLong_Check(item)) { + Py_ssize_t i = PyInt_AsSsize_t(item); if (i == -1 && PyErr_Occurred()) return NULL; if (i < 0) i += PyUnicode_GET_SIZE(self); return unicode_getitem(self, i); } else if (PySlice_Check(item)) { - int start, stop, step, slicelength, cur, i; + Py_ssize_t start, stop, step, slicelength, cur, i; Py_UNICODE* source_buf; Py_UNICODE* result_buf; PyObject* result; @@ -6499,14 +6507,14 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item) } static PyMappingMethods unicode_as_mapping = { - (inquiry)unicode_length, /* mp_length */ + (lenfunc)unicode_length, /* mp_length */ (binaryfunc)unicode_subscript, /* mp_subscript */ (objobjargproc)0, /* mp_ass_subscript */ }; -static int +static Py_ssize_t unicode_buffer_getreadbuf(PyUnicodeObject *self, - int index, + Py_ssize_t index, const void **ptr) { if (index != 0) { @@ -6518,8 +6526,8 @@ unicode_buffer_getreadbuf(PyUnicodeObject *self, return PyUnicode_GET_DATA_SIZE(self); } -static int -unicode_buffer_getwritebuf(PyUnicodeObject *self, int index, +static Py_ssize_t +unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index, const void **ptr) { PyErr_SetString(PyExc_TypeError, @@ -6529,7 +6537,7 @@ unicode_buffer_getwritebuf(PyUnicodeObject *self, int index, static int unicode_buffer_getsegcount(PyUnicodeObject *self, - int *lenp) + Py_ssize_t *lenp) { if (lenp) *lenp = PyUnicode_GET_DATA_SIZE(self); @@ -6538,7 +6546,7 @@ unicode_buffer_getsegcount(PyUnicodeObject *self, static int unicode_buffer_getcharbuf(PyUnicodeObject *self, - int index, + Py_ssize_t index, const void **ptr) { PyObject *str; @@ -6558,9 +6566,9 @@ unicode_buffer_getcharbuf(PyUnicodeObject *self, /* Helpers for PyUnicode_Format() */ static PyObject * -getnextarg(PyObject *args, int arglen, int *p_argidx) +getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) { - int argidx = *p_argidx; + Py_ssize_t argidx = *p_argidx; if (argidx < arglen) { (*p_argidx)++; if (arglen < 0) @@ -6579,11 +6587,11 @@ getnextarg(PyObject *args, int arglen, int *p_argidx) #define F_ALT (1<<3) #define F_ZERO (1<<4) -static int +static Py_ssize_t strtounicode(Py_UNICODE *buffer, const char *charbuffer) { - register long i; - long len = strlen(charbuffer); + register Py_ssize_t i; + Py_ssize_t len = strlen(charbuffer); for (i = len - 1; i >= 0; i--) buffer[i] = (Py_UNICODE) charbuffer[i]; @@ -6594,14 +6602,16 @@ static int doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x) { PyOS_ascii_formatd((char *)buffer, len, format, x); - return strtounicode(buffer, (char *)buffer); + return Py_SAFE_DOWNCAST(strtounicode(buffer, (char *)buffer), + Py_ssize_t, int); } static int longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x) { PyOS_snprintf((char *)buffer, len, format, x); - return strtounicode(buffer, (char *)buffer); + return Py_SAFE_DOWNCAST(strtounicode(buffer, (char *)buffer), + Py_ssize_t, int); } /* XXX To save some code duplication, formatfloat/long/int could have been @@ -6620,6 +6630,7 @@ formatfloat(Py_UNICODE *buf, worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/ char fmt[20]; double x; + Py_ssize_t result; x = PyFloat_AsDouble(v); if (x == -1.0 && PyErr_Occurred()) @@ -6691,6 +6702,7 @@ formatint(Py_UNICODE *buf, char fmt[64]; /* plenty big enough! */ char *sign; long x; + Py_ssize_t result; x = PyInt_AsLong(v); if (x == -1 && PyErr_Occurred()) @@ -6814,7 +6826,7 @@ PyObject *PyUnicode_Format(PyObject *format, PyObject *args) { Py_UNICODE *fmt, *res; - int fmtcnt, rescnt, reslen, arglen, argidx; + Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx; int args_owned = 0; PyUnicodeObject *result = NULL; PyObject *dict = NULL; @@ -6863,7 +6875,7 @@ PyObject *PyUnicode_Format(PyObject *format, else { /* Got a format specifier */ int flags = 0; - int width = -1; + Py_ssize_t width = -1; int prec = -1; Py_UNICODE c = '\0'; Py_UNICODE fill; @@ -6871,13 +6883,13 @@ PyObject *PyUnicode_Format(PyObject *format, PyObject *temp = NULL; Py_UNICODE *pbuf; Py_UNICODE sign; - int len; + Py_ssize_t len; Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ fmt++; if (*fmt == '(') { Py_UNICODE *keystart; - int keylen; + Py_ssize_t keylen; PyObject *key; int pcount = 1; @@ -7235,10 +7247,10 @@ PyObject *PyUnicode_Format(PyObject *format, } static PyBufferProcs unicode_as_buffer = { - (getreadbufferproc) unicode_buffer_getreadbuf, - (getwritebufferproc) unicode_buffer_getwritebuf, - (getsegcountproc) unicode_buffer_getsegcount, - (getcharbufferproc) unicode_buffer_getcharbuf, + (readbufferproc) unicode_buffer_getreadbuf, + (writebufferproc) unicode_buffer_getwritebuf, + (segcountproc) unicode_buffer_getsegcount, + (charbufferproc) unicode_buffer_getcharbuf, }; static PyObject * @@ -7269,7 +7281,7 @@ static PyObject * unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyUnicodeObject *tmp, *pnew; - int n; + Py_ssize_t n; assert(PyType_IsSubtype(type, &PyUnicode_Type)); tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds); diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index fd99a63..39c5db2 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -520,7 +520,7 @@ proxy_dealloc(PyWeakReference *self) /* sequence slots */ static PyObject * -proxy_slice(PyWeakReference *proxy, int i, int j) +proxy_slice(PyWeakReference *proxy, Py_ssize_t i, Py_ssize_t j) { if (!proxy_checkref(proxy)) return NULL; @@ -528,7 +528,7 @@ proxy_slice(PyWeakReference *proxy, int i, int j) } static int -proxy_ass_slice(PyWeakReference *proxy, int i, int j, PyObject *value) +proxy_ass_slice(PyWeakReference *proxy, Py_ssize_t i, Py_ssize_t j, PyObject *value) { if (!proxy_checkref(proxy)) return -1; @@ -546,7 +546,7 @@ proxy_contains(PyWeakReference *proxy, PyObject *value) /* mapping slots */ -static int +static Py_ssize_t proxy_length(PyWeakReference *proxy) { if (!proxy_checkref(proxy)) @@ -625,18 +625,18 @@ static PyNumberMethods proxy_as_number = { }; static PySequenceMethods proxy_as_sequence = { - (inquiry)proxy_length, /*sq_length*/ + (lenfunc)proxy_length, /*sq_length*/ 0, /*sq_concat*/ 0, /*sq_repeat*/ 0, /*sq_item*/ - (intintargfunc)proxy_slice, /*sq_slice*/ + (ssizessizeargfunc)proxy_slice, /*sq_slice*/ 0, /*sq_ass_item*/ - (intintobjargproc)proxy_ass_slice, /*sq_ass_slice*/ + (ssizessizeobjargproc)proxy_ass_slice, /*sq_ass_slice*/ (objobjproc)proxy_contains, /* sq_contains */ }; static PyMappingMethods proxy_as_mapping = { - (inquiry)proxy_length, /*mp_length*/ + (lenfunc)proxy_length, /*mp_length*/ (binaryfunc)proxy_getitem, /*mp_subscript*/ (objobjargproc)proxy_setitem, /*mp_ass_subscript*/ }; @@ -886,7 +886,7 @@ PyObject_ClearWeakRefs(PyObject *object) } if (*list != NULL) { PyWeakReference *current = *list; - int count = _PyWeakref_GetWeakrefCount(current); + Py_ssize_t count = _PyWeakref_GetWeakrefCount(current); int restore_error = PyErr_Occurred() ? 1 : 0; PyObject *err_type, *err_value, *err_tb; @@ -904,7 +904,7 @@ PyObject_ClearWeakRefs(PyObject *object) } else { PyObject *tuple = PyTuple_New(count * 2); - int i = 0; + Py_ssize_t i = 0; for (i = 0; i < count; ++i) { PyWeakReference *next = current->wr_next; |