Skip to content

Commit af19b18

Browse files
committed
Merge pull request #30 from scoder/less_explicit_capi
remove more unnecessary C-API usages
2 parents 4fbcbd9 + 4df68b1 commit af19b18

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

cytoolz/itertoolz.pyx

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#cython: embedsignature=True
2-
from cpython.dict cimport (PyDict_Contains, PyDict_GetItem, PyDict_New,
3-
PyDict_SetItem)
4-
from cpython.exc cimport PyErr_Clear, PyErr_ExceptionMatches, PyErr_GivenExceptionMatches, PyErr_Occurred
5-
from cpython.list cimport (PyList_Append, PyList_Check, PyList_GET_ITEM,
6-
PyList_GET_SIZE, PyList_New)
7-
from cpython.ref cimport PyObject, Py_DECREF, Py_INCREF
2+
from cpython.dict cimport PyDict_GetItem
3+
from cpython.exc cimport (PyErr_Clear, PyErr_ExceptionMatches,
4+
PyErr_GivenExceptionMatches, PyErr_Occurred)
5+
from cpython.list cimport (PyList_Append, PyList_GET_ITEM, PyList_GET_SIZE)
6+
from cpython.ref cimport PyObject, Py_DECREF, Py_INCREF, Py_XDECREF
87
from cpython.sequence cimport PySequence_Check
98
from cpython.set cimport PySet_Add, PySet_Contains
109
from cpython.tuple cimport PyTuple_GetSlice, PyTuple_New, PyTuple_SET_ITEM
@@ -120,7 +119,7 @@ cpdef dict groupby(object func, object seq):
120119
key = func(item)
121120
obj = PyDict_GetItem(d, key)
122121
if obj is NULL:
123-
PyDict_SetItem(d, key, [item])
122+
d[key] = [item]
124123
else:
125124
PyList_Append(<object>obj, item)
126125
return d
@@ -263,7 +262,7 @@ def merge_sorted(*seqs, **kwargs):
263262
>>> list(merge_sorted([2, 3], [1, 3], key=lambda x: x // 3))
264263
[2, 1, 3, 3]
265264
"""
266-
if PyDict_Contains(kwargs, 'key'):
265+
if 'key' in kwargs:
267266
return c_merge_sorted(seqs, kwargs['key'])
268267
return c_merge_sorted(seqs)
269268

@@ -284,9 +283,7 @@ cdef class interleave:
284283
Returns a lazy iterator
285284
"""
286285
def __cinit__(self, seqs, pass_exceptions=()):
287-
self.iters = []
288-
for seq in seqs:
289-
PyList_Append(self.iters, iter(seq))
286+
self.iters = [iter(seq) for seq in seqs]
290287
self.newiters = []
291288
self.pass_exceptions = tuple(pass_exceptions)
292289
self.i = 0
@@ -334,7 +331,7 @@ cdef class interleave:
334331

335332
PyList_Append(self.newiters, val)
336333
val = <object>obj
337-
Py_DECREF(val)
334+
Py_XDECREF(obj)
338335
return val
339336

340337

@@ -433,7 +430,7 @@ cpdef object isdistinct(object seq):
433430
for item in seq:
434431
if PySet_Contains(seen, item):
435432
return False
436-
PySet_Add(seen, item)
433+
seen.add(item)
437434
return True
438435
else:
439436
return len(seq) == len(set(seq))
@@ -586,7 +583,7 @@ cpdef object get(object ind, object seq, object default=no_default):
586583
cdef object val
587584
cdef tuple result
588585
cdef PyObject *obj
589-
if PyList_Check(ind):
586+
if isinstance(ind, list):
590587
i = PyList_GET_SIZE(ind)
591588
result = PyTuple_New(i)
592589
# List of indices, no default
@@ -692,23 +689,23 @@ cpdef dict frequencies(object seq):
692689
for item in seq:
693690
obj = PyDict_GetItem(d, item)
694691
if obj is NULL:
695-
PyDict_SetItem(d, item, 1)
692+
d[item] = 1
696693
else:
697694
val = <object>obj
698-
PyDict_SetItem(d, item, val + 1)
695+
d[item] = val + 1
699696
return d
700697

701698

702699
''' Alternative implementation of `frequencies`
703700
cpdef dict frequencies(object seq):
704701
cdef dict d = {}
705-
cdef int val
702+
cdef Py_ssize_t val
706703
for item in seq:
707-
if PyDict_Contains(d, item):
708-
val = PyObject_GetItem(d, item)
709-
PyDict_SetItem(d, item, val + 1)
704+
if item in d:
705+
val = d[item]
706+
d[item] = val + 1
710707
else:
711-
PyDict_SetItem(d, item, 1)
708+
d[item] = 1
712709
return d
713710
'''
714711

@@ -761,7 +758,7 @@ cpdef dict reduceby(object key, object binop, object seq, object init):
761758
else:
762759
val = <object>obj
763760
val = binop(val, item)
764-
PyDict_SetItem(d, k, val)
761+
d[k] = val
765762
return d
766763

767764

@@ -915,12 +912,11 @@ cdef class partition_all:
915912
PyTuple_SET_ITEM(result, i, item)
916913
i += 1
917914
if i == self.n:
918-
break
915+
return result
916+
# iterable exhausted before filling the tuple
919917
if i == 0:
920918
raise StopIteration
921-
if i != self.n:
922-
return PyTuple_GetSlice(result, 0, i)
923-
return result
919+
return PyTuple_GetSlice(result, 0, i)
924920

925921

926922
cpdef object count(object seq):
@@ -936,7 +932,6 @@ cpdef object count(object seq):
936932
"""
937933
if iter(seq) is not seq and hasattr(seq, '__len__'):
938934
return len(seq)
939-
cdef object _
940935
cdef Py_ssize_t i = 0
941936
for _ in seq:
942937
i += 1
@@ -1055,7 +1050,7 @@ cpdef object pluck(object ind, object seqs, object default=no_default):
10551050
get
10561051
map
10571052
"""
1058-
if PyList_Check(ind):
1053+
if isinstance(ind, list):
10591054
if default is not no_default:
10601055
return _pluck_list_default(ind, seqs, default)
10611056
if PyList_GET_SIZE(ind) < 10:

0 commit comments

Comments
 (0)