Commit cc511a3d authored by Yury V. Zaytsev's avatar Yury V. Zaytsev

Update code samples to use newer Cython / Python syntax

parent 6d343f58
...@@ -2,11 +2,13 @@ cimport cqueue ...@@ -2,11 +2,13 @@ cimport cqueue
cimport python_exc cimport python_exc
cdef class Queue: cdef class Queue:
cdef cqueue.Queue* _c_queue cdef cqueue.Queue* _c_queue
def __cinit__(self): def __cinit__(self):
self._c_queue = cqueue.queue_new() self._c_queue = cqueue.queue_new()
if self._c_queue is NULL: if self._c_queue is NULL:
python_exc.PyErr_NoMemory() raise MemoryError()
def __dealloc__(self): def __dealloc__(self):
if self._c_queue is not NULL: if self._c_queue is not NULL:
...@@ -14,14 +16,14 @@ cdef class Queue: ...@@ -14,14 +16,14 @@ cdef class Queue:
cpdef int append(self, int value) except -1: cpdef int append(self, int value) except -1:
if not cqueue.queue_push_tail(self._c_queue, <void*>value): if not cqueue.queue_push_tail(self._c_queue, <void*>value):
python_exc.PyErr_NoMemory() raise MemoryError()
return 0 return 0
cdef int extend(self, int* values, Py_ssize_t count) except -1: cdef int extend(self, int* values, Py_ssize_t count) except -1:
cdef Py_ssize_t i cdef Py_ssize_t i
for i in xrange(count): for i in range(count):
if not cqueue.queue_push_tail(self._c_queue, <void*>values[i]): if not cqueue.queue_push_tail(self._c_queue, <void*>values[i]):
python_exc.PyErr_NoMemory() raise MemoryError()
return 0 return 0
cpdef int peek(self) except? 0: cpdef int peek(self) except? 0:
...@@ -42,7 +44,7 @@ cdef class Queue: ...@@ -42,7 +44,7 @@ cdef class Queue:
raise IndexError("Queue is empty") raise IndexError("Queue is empty")
return value return value
def __nonzero__(self): def __bool__(self):
return not cqueue.queue_is_empty(self._c_queue) return not cqueue.queue_is_empty(self._c_queue)
DEF repeat_count=10000 DEF repeat_count=10000
...@@ -50,9 +52,9 @@ DEF repeat_count=10000 ...@@ -50,9 +52,9 @@ DEF repeat_count=10000
def test_cy(): def test_cy():
cdef int i cdef int i
cdef Queue q = Queue() cdef Queue q = Queue()
for i in xrange(repeat_count): for i in range(repeat_count):
q.append(i) q.append(i)
for i in xrange(repeat_count): for i in range(repeat_count):
q.peek() q.peek()
while q: while q:
q.pop() q.pop()
...@@ -60,9 +62,9 @@ def test_cy(): ...@@ -60,9 +62,9 @@ def test_cy():
def test_py(): def test_py():
cdef int i cdef int i
q = Queue() q = Queue()
for i in xrange(repeat_count): for i in range(repeat_count):
q.append(i) q.append(i)
for i in xrange(repeat_count): for i in range(repeat_count):
q.peek() q.peek()
while q: while q:
q.pop() q.pop()
...@@ -72,9 +74,9 @@ from collections import deque ...@@ -72,9 +74,9 @@ from collections import deque
def test_deque(): def test_deque():
cdef int i cdef int i
q = deque() q = deque()
for i in xrange(repeat_count): for i in range(repeat_count):
q.appendleft(i) q.appendleft(i)
for i in xrange(repeat_count): for i in range(repeat_count):
q[-1] q[-1]
while q: while q:
q.pop() q.pop()
......
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