Commit 285ca063 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2376 from gabrieldemarmiesse/test_string_2

Adding tests for "Unicode and passing strings" part 2
parents 36239b06 94354722
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.
Note that the creation of the Python bytes string can fail with an
exception, e.g. due to insufficient memory. If you need to
: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
cdef bytes py_string
cdef char* c_string = c_call_creating_a_new_c_string()
try:
py_string = c_string
finally:
free(c_string)
.. literalinclude:: ../../examples/tutorial/string/try_finally.pyx
To convert the byte string back into a C :c:type:`char*`, use the
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