Commit 94354722 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Moved an example from string.rst to the examples directory.

parent 5c04c1a8
from libc.stdlib cimport free
from c_func cimport c_call_returning_a_c_string
cdef bytes py_string
cdef char* c_string = c_call_returning_a_c_string()
try:
py_string = c_string
finally:
free(c_string)
...@@ -157,15 +157,9 @@ slice indices will lead to data corruption and crashes. ...@@ -157,15 +157,9 @@ slice indices will lead to data corruption and crashes.
Note that the creation of the Python bytes string can fail with an Note that the creation of the Python bytes string can fail with an
exception, e.g. due to insufficient memory. If you need to exception, e.g. due to insufficient memory. If you need to
:c:func:`free()` the string after the conversion, you should wrap :c:func:`free()` the string after the conversion, you should wrap
the assignment in a try-finally construct:: the assignment in a try-finally construct:
from libc.stdlib cimport free .. literalinclude:: ../../examples/tutorial/string/try_finally.pyx
cdef bytes py_string
cdef char* c_string = c_call_creating_a_new_c_string()
try:
py_string = c_string
finally:
free(c_string)
To convert the byte string back into a C :c:type:`char*`, use the To convert the byte string back into a C :c:type:`char*`, use the
opposite assignment:: opposite assignment::
......
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