Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
8e3a8af1
Commit
8e3a8af1
authored
Jun 13, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added tests for some of the code snippets in "calling C libraries". Fixed a forgotten import.
parent
5fadf79e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
103 additions
and
93 deletions
+103
-93
docs/examples/tutorial/clibraries/c-algorithms/src/queue.h
docs/examples/tutorial/clibraries/c-algorithms/src/queue.h
+17
-0
docs/examples/tutorial/clibraries/cqueue.pxd
docs/examples/tutorial/clibraries/cqueue.pxd
+19
-0
docs/examples/tutorial/clibraries/queue.pyx
docs/examples/tutorial/clibraries/queue.pyx
+9
-0
docs/examples/tutorial/clibraries/queue2.pyx
docs/examples/tutorial/clibraries/queue2.pyx
+11
-0
docs/examples/tutorial/clibraries/test_queue.py
docs/examples/tutorial/clibraries/test_queue.py
+36
-0
docs/src/tutorial/clibraries.rst
docs/src/tutorial/clibraries.rst
+11
-93
No files found.
docs/examples/tutorial/clibraries/c-algorithms/src/queue.h
0 → 100644
View file @
8e3a8af1
/* queue.h */
typedef
struct
_Queue
Queue
;
typedef
void
*
QueueValue
;
Queue
*
queue_new
(
void
);
void
queue_free
(
Queue
*
queue
);
int
queue_push_head
(
Queue
*
queue
,
QueueValue
data
);
QueueValue
queue_pop_head
(
Queue
*
queue
);
QueueValue
queue_peek_head
(
Queue
*
queue
);
int
queue_push_tail
(
Queue
*
queue
,
QueueValue
data
);
QueueValue
queue_pop_tail
(
Queue
*
queue
);
QueueValue
queue_peek_tail
(
Queue
*
queue
);
int
queue_is_empty
(
Queue
*
queue
);
docs/examples/tutorial/clibraries/cqueue.pxd
0 → 100644
View file @
8e3a8af1
# cqueue.pxd
cdef
extern
from
"c-algorithms/src/queue.h"
:
ctypedef
struct
Queue
:
pass
ctypedef
void
*
QueueValue
Queue
*
queue_new
()
void
queue_free
(
Queue
*
queue
)
int
queue_push_head
(
Queue
*
queue
,
QueueValue
data
)
QueueValue
queue_pop_head
(
Queue
*
queue
)
QueueValue
queue_peek_head
(
Queue
*
queue
)
int
queue_push_tail
(
Queue
*
queue
,
QueueValue
data
)
QueueValue
queue_pop_tail
(
Queue
*
queue
)
QueueValue
queue_peek_tail
(
Queue
*
queue
)
bint
queue_is_empty
(
Queue
*
queue
)
docs/examples/tutorial/clibraries/queue.pyx
0 → 100644
View file @
8e3a8af1
# queue.pyx
cimport
cqueue
cdef
class
Queue
:
cdef
cqueue
.
Queue
*
_c_queue
def
__cinit__
(
self
):
self
.
_c_queue
=
cqueue
.
queue_new
()
docs/examples/tutorial/clibraries/queue2.pyx
0 → 100644
View file @
8e3a8af1
# queue.pyx
cimport
cqueue
cdef
class
Queue
:
cdef
cqueue
.
Queue
*
_c_queue
def
__cinit__
(
self
):
self
.
_c_queue
=
cqueue
.
queue_new
()
if
self
.
_c_queue
is
NULL
:
raise
MemoryError
()
docs/examples/tutorial/clibraries/test_queue.py
0 → 100644
View file @
8e3a8af1
from
__future__
import
print_function
import
time
import
queue
Q
=
queue
.
Queue
()
Q
.
append
(
10
)
Q
.
append
(
20
)
print
(
Q
.
peek
())
print
(
Q
.
pop
())
print
(
Q
.
pop
())
try
:
print
(
Q
.
pop
())
except
IndexError
as
e
:
print
(
"Error message:"
,
e
)
# Prints "Queue is empty"
i
=
10000
values
=
range
(
i
)
start_time
=
time
.
time
()
Q
.
extend
(
values
)
end_time
=
time
.
time
()
-
start_time
print
(
"Adding {} items took {:1.3f} msecs."
.
format
(
i
,
1000
*
end_time
))
for
i
in
range
(
41
):
Q
.
pop
()
Q
.
pop
()
print
(
"The answer is:"
)
print
(
Q
.
pop
())
docs/src/tutorial/clibraries.rst
View file @
8e3a8af1
...
...
@@ -33,48 +33,15 @@ Defining external declarations
You
can
download
CAlg
`
here
<
https
://
github
.
com
/
fragglet
/
c
-
algorithms
/
archive
/
master
.
zip
>`
_
.
The
C
API
of
the
queue
implementation
,
which
is
defined
in
the
header
file
``
c
-
algorithms
/
src
/
queue
.
h
``,
essentially
looks
like
this
:
:
file
``
c
-
algorithms
/
src
/
queue
.
h
``,
essentially
looks
like
this
:
/*
file
:
queue
.
h
*/
typedef
struct
_Queue
Queue
;
typedef
void
*
QueueValue
;
Queue
*
queue_new
(
void
);
void
queue_free
(
Queue
*
queue
);
int
queue_push_head
(
Queue
*
queue
,
QueueValue
data
);
QueueValue
queue_pop_head
(
Queue
*
queue
);
QueueValue
queue_peek_head
(
Queue
*
queue
);
int
queue_push_tail
(
Queue
*
queue
,
QueueValue
data
);
QueueValue
queue_pop_tail
(
Queue
*
queue
);
QueueValue
queue_peek_tail
(
Queue
*
queue
);
int
queue_is_empty
(
Queue
*
queue
);
..
literalinclude
::
../../
examples
/
tutorial
/
clibraries
/
c
-
algorithms
/
src
/
queue
.
h
:
language
:
C
To
get
started
,
the
first
step
is
to
redefine
the
C
API
in
a
``.
pxd
``
file
,
say
,
``
cqueue
.
pxd
``::
#
file
:
cqueue
.
pxd
cdef
extern
from
"c-algorithms/src/queue.h"
:
ctypedef
struct
Queue
:
pass
ctypedef
void
*
QueueValue
Queue
*
queue_new
()
void
queue_free
(
Queue
*
queue
)
int
queue_push_head
(
Queue
*
queue
,
QueueValue
data
)
QueueValue
queue_pop_head
(
Queue
*
queue
)
QueueValue
queue_peek_head
(
Queue
*
queue
)
int
queue_push_tail
(
Queue
*
queue
,
QueueValue
data
)
QueueValue
queue_pop_tail
(
Queue
*
queue
)
QueueValue
queue_peek_tail
(
Queue
*
queue
)
file
,
say
,
``
cqueue
.
pxd
``:
bint
queue_is_empty
(
Queue
*
queue
)
..
literalinclude
::
../../
examples
/
tutorial
/
clibraries
/
cqueue
.
pxd
Note
how
these
declarations
are
almost
identical
to
the
header
file
declarations
,
so
you
can
often
just
copy
them
over
.
However
,
you
do
...
...
@@ -144,16 +111,9 @@ class that should wrap the C queue. It will live in a file called
library, there must not be a ``.pyx`` file with the same name
that Cython associates with it.
Here is a first start for the Queue class:
:
Here is a first start for the Queue class:
# file: queue.pyx
cimport cqueue
cdef class Queue:
cdef cqueue.Queue* _c_queue
def __cinit__(self):
self._c_queue = cqueue.queue_new()
.. literalinclude:: ../../examples/tutorial/clibraries/queue.pyx
Note that it says ``__cinit__`` rather than ``__init__``. While
``__init__`` is available as well, it is not guaranteed to be run (for
...
...
@@ -190,16 +150,9 @@ that case, it will return ``NULL``, whereas it would normally return a
pointer
to
the
new
queue
.
The
Python
way
to
get
out
of
this
is
to
raise
a
``
MemoryError
``
[#]
_
.
We
can
thus
change
the
init
function
as
follows
::
cimport
cqueue
We
can
thus
change
the
init
function
as
follows
:
cdef
class
Queue
:
cdef
cqueue
.
Queue
*
_c_queue
def
__cinit__
(
self
):
self
.
_c_queue
=
cqueue
.
queue_new
()
if
self
.
_c_queue
is
NULL
:
raise
MemoryError
()
..
literalinclude
::
../../
examples
/
tutorial
/
clibraries
/
queue2
.
pyx
..
[#]
In
the
specific
case
of
a
``
MemoryError
``,
creating
a
new
exception
instance
in
order
to
raise
it
may
actually
fail
because
...
...
@@ -587,44 +540,9 @@ instead that accepts an arbitrary Python iterable::
Now we can test our Queue implementation using a python script,
for example here :file:`test_queue.py`.::
from __future__ import print_function
import queue
Q = queue.Queue()
Q.append(10)
Q.append(20)
print(Q.peek())
print(Q.pop())
print(Q.pop())
try:
print(Q.pop())
except IndexError as e:
print("Error message:", e) # Prints "Queue is empty"
i = 10000
values = range(i)
start_time = time.time()
Q.extend(values)
end_time = time.time() - start_time
print("Adding {} items took {:1.3f} msecs.".format(i, 1000 * end_time))
for i in range(41):
Q.pop()
Q.pop()
print("The answer is:")
print(Q.pop())
for example here :file:`test_queue.py`:
.. literalinclude:: ../../examples/tutorial/clibraries/test_queue.py
As a quick test with 10000 numbers on the author'
s
machine
indicates
,
using
this
Queue
from
Cython
code
with
C
``
int
``
values
is
about
five
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment