Commit 084a25f5 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2394 from gabrieldemarmiesse/refactoring_clibraries_example

Refactoring of the extend function in "using C libraries" to make it simpler.
parents 7a48a98d 79f88002
...@@ -38,11 +38,9 @@ cdef class Queue: ...@@ -38,11 +38,9 @@ cdef class Queue:
self.append(value) self.append(value)
cdef extend_ints(self, int* values, size_t count): cdef extend_ints(self, int* values, size_t count):
cdef size_t i cdef int value
for i in range(count): for value in values[:count]: # Slicing pointer to limit the iteration boundaries.
if not cqueue.queue_push_tail( self.append(value)
self._c_queue, <void*> values[i]):
raise MemoryError()
cpdef int peek(self) except? -1: cpdef int peek(self) except? -1:
cdef int value = <Py_ssize_t> cqueue.queue_peek_head(self._c_queue) cdef int value = <Py_ssize_t> cqueue.queue_peek_head(self._c_queue)
......
...@@ -342,11 +342,9 @@ Adding an ``extend()`` method should now be straight forward:: ...@@ -342,11 +342,9 @@ Adding an ``extend()`` method should now be straight forward::
cdef extend(self, int* values, size_t count): cdef extend(self, int* values, size_t count):
"""Append all ints to the queue. """Append all ints to the queue.
""" """
cdef size_t i cdef int value
for i in range(count): for value in values[:count]: # Slicing pointer to limit the iteration boundaries.
if not cqueue.queue_push_tail( self.append(value)
self._c_queue, <void*>values[i]):
raise MemoryError()
This becomes handy when reading values from a C array, for example. This becomes handy when reading values from a C array, for example.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment