Commit 9cbd59ed authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Extended the examples of string.rst and put them in the examples directory for testing.

parent 3d291a58
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)
cdef char* c_call_returning_a_c_string()
cdef void get_a_c_string(char** c_string, Py_ssize_t *length)
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
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
...@@ -107,17 +107,21 @@ within a well defined context. ...@@ -107,17 +107,21 @@ within a well defined context.
Passing byte strings 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. 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 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 convert it into a Python byte string by simply assigning it to a
Python variable:: 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::
py_string = <bytes> c_string .. literalinclude:: ../../examples/tutorial/string/assignment.pyx
This creates a Python byte string object that holds a copy of the 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 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 ...@@ -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 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, 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 it is much more efficient to tell Cython the exact number of bytes by
slicing the C string:: slicing the C string. Here is an example:
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] .. literalinclude:: ../../examples/tutorial/string/slicing_c_string.pyx
Here, no additional byte counting is required and ``length`` bytes from Here, no additional byte counting is required and ``length`` bytes from
the ``c_string`` will be copied into the Python bytes object, including the ``c_string`` will be copied into the Python bytes object, including
......
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