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
Kirill Smelkov
cython
Commits
696a1959
Commit
696a1959
authored
Jul 14, 2021
by
Matus Valo
Committed by
GitHub
Jul 14, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
docs: Add Pure Python mode to "Calling C functions" tutorial (GH-4247)
parent
8552e3d3
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
79 additions
and
4 deletions
+79
-4
docs/examples/tutorial/external/atoi.py
docs/examples/tutorial/external/atoi.py
+6
-0
docs/examples/tutorial/external/atoi.pyx
docs/examples/tutorial/external/atoi.pyx
+1
-0
docs/examples/tutorial/external/keyword_args_call.py
docs/examples/tutorial/external/keyword_args_call.py
+8
-0
docs/examples/tutorial/external/keyword_args_call.pyx
docs/examples/tutorial/external/keyword_args_call.pyx
+1
-0
docs/examples/tutorial/external/libc_sin.py
docs/examples/tutorial/external/libc_sin.py
+5
-0
docs/examples/tutorial/external/libc_sin.pyx
docs/examples/tutorial/external/libc_sin.pyx
+1
-0
docs/examples/tutorial/external/py_version_hex.py
docs/examples/tutorial/external/py_version_hex.py
+4
-0
docs/examples/tutorial/external/strstr.pxd
docs/examples/tutorial/external/strstr.pxd
+2
-0
docs/src/tutorial/external.rst
docs/src/tutorial/external.rst
+51
-4
No files found.
docs/examples/tutorial/external/atoi.py
0 → 100644
View file @
696a1959
from
cython.cimports.libc.stdlib
import
atoi
@
cython
.
cfunc
def
parse_charptr_to_py_int
(
s
:
cython
.
p_char
):
assert
s
is
not
cython
.
NULL
,
"byte string value is NULL"
return
atoi
(
s
)
# note: atoi() has no error detection!
docs/examples/tutorial/external/atoi.pyx
View file @
696a1959
from
libc.stdlib
cimport
atoi
from
libc.stdlib
cimport
atoi
cdef
parse_charptr_to_py_int
(
char
*
s
):
cdef
parse_charptr_to_py_int
(
char
*
s
):
assert
s
is
not
NULL
,
"byte string value is NULL"
assert
s
is
not
NULL
,
"byte string value is NULL"
return
atoi
(
s
)
# note: atoi() has no error detection!
return
atoi
(
s
)
# note: atoi() has no error detection!
docs/examples/tutorial/external/keyword_args_call.py
0 → 100644
View file @
696a1959
import
cython
from
cython.cimports.strstr
import
strstr
def
main
():
data
:
p_char
=
"hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh"
pos
:
p_char
=
strstr
(
needle
=
'akd'
,
haystack
=
data
)
print
(
pos
is
not
cython
.
NULL
)
docs/examples/tutorial/external/keyword_args_call.pyx
View file @
696a1959
cdef
extern
from
"string.h"
:
cdef
extern
from
"string.h"
:
char
*
strstr
(
const
char
*
haystack
,
const
char
*
needle
)
char
*
strstr
(
const
char
*
haystack
,
const
char
*
needle
)
cdef
char
*
data
=
"hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh"
cdef
char
*
data
=
"hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh"
cdef
char
*
pos
=
strstr
(
needle
=
'akd'
,
haystack
=
data
)
cdef
char
*
pos
=
strstr
(
needle
=
'akd'
,
haystack
=
data
)
...
...
docs/examples/tutorial/external/libc_sin.py
0 → 100644
View file @
696a1959
from
cython.cimports.libc.math
import
sin
@
cython
.
cfunc
def
f
(
x
:
cython
.
double
)
->
cython
.
double
:
return
sin
(
x
*
x
)
docs/examples/tutorial/external/libc_sin.pyx
View file @
696a1959
from
libc.math
cimport
sin
from
libc.math
cimport
sin
cdef
double
f
(
double
x
):
cdef
double
f
(
double
x
):
return
sin
(
x
*
x
)
return
sin
(
x
*
x
)
docs/examples/tutorial/external/py_version_hex.py
0 → 100644
View file @
696a1959
from
cython.cimports.cpython.version
import
PY_VERSION_HEX
# Python version >= 3.2 final ?
print
(
PY_VERSION_HEX
>=
0x030200F0
)
docs/examples/tutorial/external/strstr.pxd
0 → 100644
View file @
696a1959
cdef
extern
from
"string.h"
:
char
*
strstr
(
const
char
*
haystack
,
const
char
*
needle
)
docs/src/tutorial/external.rst
View file @
696a1959
Calling C functions
Calling C functions
====================
====================
.. include::
../two-syntax-variants-used
This tutorial describes shortly what you need to know in order to call
This tutorial describes shortly what you need to know in order to call
C library functions from Cython code. For a longer and more
C library functions from Cython code. For a longer and more
comprehensive tutorial about using external C libraries, wrapping them
comprehensive tutorial about using external C libraries, wrapping them
...
@@ -15,7 +18,17 @@ For example, let's say you need a low-level way to parse a number from
...
@@ -15,7 +18,17 @@ For example, let's say you need a low-level way to parse a number from
a ``char*`` value. You could use the ``atoi()`` function, as defined
a ``char*`` value. You could use the ``atoi()`` function, as defined
by the ``stdlib.h`` header file. This can be done as follows:
by the ``stdlib.h`` header file. This can be done as follows:
.. literalinclude:: ../../examples/tutorial/external/atoi.pyx
.. tabs::
.. group-tab:: Pure Python
.. literalinclude:: ../../examples/tutorial/external/atoi.py
:caption: atoi.py
.. group-tab:: Cython
.. literalinclude:: ../../examples/tutorial/external/atoi.pyx
:caption: atoi.pyx
You can find a complete list of these standard cimport files in
You can find a complete list of these standard cimport files in
Cython's source package
Cython's source package
...
@@ -28,14 +41,33 @@ Cython also has a complete set of declarations for CPython's C-API.
...
@@ -28,14 +41,33 @@ Cython also has a complete set of declarations for CPython's C-API.
For example, to test at C compilation time which CPython version
For example, to test at C compilation time which CPython version
your code is being compiled with, you can do this:
your code is being compiled with, you can do this:
.. literalinclude:: ../../examples/tutorial/external/py_version_hex.pyx
.. tabs::
.. group-tab:: Pure Python
.. literalinclude:: ../../examples/tutorial/external/py_version_hex.py
:caption: py_version_hex.py
.. group-tab:: Cython
.. literalinclude:: ../../examples/tutorial/external/py_version_hex.pyx
:caption: py_version_hex.pyx
.. _libc.math:
.. _libc.math:
Cython also provides declarations for the C math library:
Cython also provides declarations for the C math library:
..
literalinclude:: ../../examples/tutorial/external/libc_sin.pyx
..
tabs::
.. group-tab:: Pure Python
.. literalinclude:: ../../examples/tutorial/external/libc_sin.py
:caption: libc_sin.py
.. group-tab:: Cython
.. literalinclude:: ../../examples/tutorial/external/libc_sin.pyx
:caption: libc_sin.pyx
Dynamic linking
Dynamic linking
---------------
---------------
...
@@ -83,6 +115,9 @@ This allows the C declaration to be reused in other Cython modules,
...
@@ -83,6 +115,9 @@ This allows the C declaration to be reused in other Cython modules,
while still providing an automatically generated Python wrapper in
while still providing an automatically generated Python wrapper in
this specific module.
this specific module.
.. note:: External declarations must be placed in a ``.pxd`` file in Pure
Python mode.
Naming parameters
Naming parameters
-----------------
-----------------
...
@@ -103,7 +138,19 @@ You can now make it clear which of the two arguments does what in
...
@@ -103,7 +138,19 @@ You can now make it clear which of the two arguments does what in
your call, thus avoiding any ambiguities and often making your code
your call, thus avoiding any ambiguities and often making your code
more readable:
more readable:
.. literalinclude:: ../../examples/tutorial/external/keyword_args_call.pyx
.. tabs::
.. group-tab:: Pure Python
.. literalinclude:: ../../examples/tutorial/external/keyword_args_call.py
:caption: keyword_args_call.py
.. literalinclude:: ../../examples/tutorial/external/strstr.pxd
:caption: strstr.pxd
.. group-tab:: Cython
.. literalinclude:: ../../examples/tutorial/external/keyword_args_call.pyx
:caption: keyword_args_call.pyx
Note that changing existing parameter names later is a backwards
Note that changing existing parameter names later is a backwards
incompatible API modification, just as for Python code. Thus, if
incompatible API modification, just as for Python code. Thus, if
...
...
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