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
9cbd59ed
Commit
9cbd59ed
authored
Jun 16, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extended the examples of string.rst and put them in the examples directory for testing.
parent
3d291a58
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
15 deletions
+61
-15
docs/examples/tutorial/string/assignment.pyx
docs/examples/tutorial/string/assignment.pyx
+12
-0
docs/examples/tutorial/string/c_func.pxd
docs/examples/tutorial/string/c_func.pxd
+2
-0
docs/examples/tutorial/string/c_func.pyx
docs/examples/tutorial/string/c_func.pyx
+18
-0
docs/examples/tutorial/string/slicing_c_string.pyx
docs/examples/tutorial/string/slicing_c_string.pyx
+16
-0
docs/src/tutorial/strings.rst
docs/src/tutorial/strings.rst
+13
-15
No files found.
docs/examples/tutorial/string/assignment.pyx
0 → 100644
View file @
9cbd59ed
from
libc.stdlib
cimport
free
from
c_func
cimport
c_call_returning_a_c_string
def
main
():
cdef
char
*
c_string
=
c_call_returning_a_c_string
()
cdef
bytes
py_string
=
c_string
# A type cast to `object` or `bytes` will do the same thing:
py_string
=
<
bytes
>
c_string
free
(
c_string
)
docs/examples/tutorial/string/c_func.pxd
0 → 100644
View file @
9cbd59ed
cdef
char
*
c_call_returning_a_c_string
()
cdef
void
get_a_c_string
(
char
**
c_string
,
Py_ssize_t
*
length
)
docs/examples/tutorial/string/c_func.pyx
0 → 100644
View file @
9cbd59ed
from
libc.stdlib
cimport
malloc
from
libc.string
cimport
strcpy
,
strlen
cdef
char
*
hello_world
=
'hello world'
cdef
Py_ssize_t
n
=
strlen
(
hello_world
)
cdef
char
*
c_call_returning_a_c_string
():
cdef
char
*
c_string
=
<
char
*>
malloc
((
n
+
1
)
*
sizeof
(
char
))
strcpy
(
c_string
,
hello_world
)
return
c_string
cdef
void
get_a_c_string
(
char
**
c_string_ptr
,
Py_ssize_t
*
length
):
c_string_ptr
[
0
]
=
<
char
*>
malloc
((
n
+
1
)
*
sizeof
(
char
))
strcpy
(
c_string_ptr
[
0
],
hello_world
)
length
[
0
]
=
n
docs/examples/tutorial/string/slicing_c_string.pyx
0 → 100644
View file @
9cbd59ed
from
libc.stdlib
cimport
free
from
c_func
cimport
get_a_c_string
def
main
():
cdef
char
*
c_string
=
NULL
cdef
Py_ssize_t
length
=
0
# get pointer and length from a C function
get_a_c_string
(
&
c_string
,
&
length
)
py_bytes_string
=
c_string
[:
length
]
free
(
c_string
)
print
(
py_bytes_string
)
# py_bytes_string is still available
docs/src/tutorial/strings.rst
View file @
9cbd59ed
...
...
@@ -107,17 +107,21 @@ within a well defined context.
Passing byte strings
--------------------
we have a dummy C functions declared in
a file called :file:`c_func.pyx` that we are going to reuse throughout this tutorial:
.. literalinclude:: ../../examples/tutorial/string/c_func.pyx
We make a corresponding :file:`c_func.pxd` to be able to cimport those functions:
.. literalinclude:: ../../examples/tutorial/string/c_func.pxd
It is very easy to pass byte strings between C code and Python.
When receiving a byte string from a C library, you can let Cython
convert it into a Python byte string by simply assigning it to a
Python variable::
cdef char* c_string = c_call_returning_a_c_string()
cdef bytes py_string = c_string
A type cast to :obj:`object` or :obj:`bytes` will do the same thing::
Python variable:
py_string = <bytes> c_string
.. literalinclude:: ../../examples/tutorial/string/assignment.pyx
This creates a Python byte string object that holds a copy of the
original C string. It can be safely passed around in Python code, and
...
...
@@ -133,15 +137,9 @@ C string first to find out the length by counting the bytes up to the
terminating null byte. In many cases, the user code will know the
length already, e.g. because a C function returned it. In this case,
it is much more efficient to tell Cython the exact number of bytes by
slicing the C string::
cdef char* c_string = NULL
cdef Py_ssize_t length = 0
# get pointer and length from a C function
get_a_c_string(&c_string, &length)
slicing the C string. Here is an example:
py_bytes_string = c_string[:length]
.. literalinclude:: ../../examples/tutorial/string/slicing_c_string.pyx
Here, no additional byte counting is required and ``length`` bytes from
the ``c_string`` will be copied into the Python bytes object, including
...
...
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