From 4cb8eb90e69a098c64d76e57f684bebd6f6f09c6 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Mon, 5 May 2014 20:56:58 +0200 Subject: [PATCH] replace "int" usage by safer "Py_ssize_t" types to avoid unnecessary range limitations --- cytoolz/itertoolz.pxd | 22 +++++++++++----------- cytoolz/itertoolz.pyx | 16 +++++++--------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/cytoolz/itertoolz.pxd b/cytoolz/itertoolz.pxd index ee0279d..910b7ea 100644 --- a/cytoolz/itertoolz.pxd +++ b/cytoolz/itertoolz.pxd @@ -30,8 +30,8 @@ cdef class interleave: cdef list iters cdef list newiters cdef tuple pass_exceptions - cdef int i - cdef int n + cdef Py_ssize_t i + cdef Py_ssize_t n cdef class _unique_key: @@ -54,13 +54,13 @@ cpdef object isiterable(object x) cpdef object isdistinct(object seq) -cpdef object take(int n, object seq) +cpdef object take(Py_ssize_t n, object seq) -cpdef object drop(int n, object seq) +cpdef object drop(Py_ssize_t n, object seq) -cpdef object take_nth(int n, object seq) +cpdef object take_nth(Py_ssize_t n, object seq) cpdef object first(object seq) @@ -69,7 +69,7 @@ cpdef object first(object seq) cpdef object second(object seq) -cpdef object nth(int n, object seq) +cpdef object nth(Py_ssize_t n, object seq) cpdef object last(object seq) @@ -109,14 +109,14 @@ cdef class iterate: cdef class sliding_window: cdef object iterseq cdef tuple prev - cdef int n + cdef Py_ssize_t n -cpdef object partition(int n, object seq, object pad=*) +cpdef object partition(Py_ssize_t n, object seq, object pad=*) cdef class partition_all: - cdef int n + cdef Py_ssize_t n cdef object iterseq @@ -137,14 +137,14 @@ cdef class _pluck_index_default: cdef class _pluck_list: cdef list ind cdef object iterseqs - cdef int n + cdef Py_ssize_t n cdef class _pluck_list_default: cdef list ind cdef object iterseqs cdef object default - cdef int n + cdef Py_ssize_t n cpdef object pluck(object ind, object seqs, object default=*) diff --git a/cytoolz/itertoolz.pyx b/cytoolz/itertoolz.pyx index dea509f..8b2293c 100644 --- a/cytoolz/itertoolz.pyx +++ b/cytoolz/itertoolz.pyx @@ -439,7 +439,7 @@ cpdef object isdistinct(object seq): return len(seq) == len(set(seq)) -cpdef object take(int n, object seq): +cpdef object take(Py_ssize_t n, object seq): """ The first n elements of a sequence @@ -449,7 +449,7 @@ cpdef object take(int n, object seq): return islice(seq, n) -cpdef object drop(int n, object seq): +cpdef object drop(Py_ssize_t n, object seq): """ The sequence following the first n elements @@ -458,20 +458,18 @@ cpdef object drop(int n, object seq): """ if n < 0: raise ValueError('n argument for drop() must be non-negative') - cdef int i + cdef Py_ssize_t i cdef object iter_seq - i = 0 iter_seq = iter(seq) try: - while i < n: - i += 1 + for i in range(n): next(iter_seq) except StopIteration: pass return iter_seq -cpdef object take_nth(int n, object seq): +cpdef object take_nth(Py_ssize_t n, object seq): """ Every nth item in seq @@ -503,7 +501,7 @@ cpdef object second(object seq): return next(seq) -cpdef object nth(int n, object seq): +cpdef object nth(Py_ssize_t n, object seq): """ The nth element in a sequence @@ -858,7 +856,7 @@ cdef class sliding_window: no_pad = '__no__pad__' -cpdef object partition(int n, object seq, object pad=no_pad): +cpdef object partition(Py_ssize_t n, object seq, object pad=no_pad): """ Partition sequence into tuples of length n