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
Boxiang Sun
cython
Commits
285ca063
Commit
285ca063
authored
Jun 20, 2018
by
scoder
Committed by
GitHub
Jun 20, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2376 from gabrieldemarmiesse/test_string_2
Adding tests for "Unicode and passing strings" part 2
parents
36239b06
94354722
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
8 deletions
+11
-8
docs/examples/tutorial/string/try_finally.pyx
docs/examples/tutorial/string/try_finally.pyx
+9
-0
docs/src/tutorial/strings.rst
docs/src/tutorial/strings.rst
+2
-8
No files found.
docs/examples/tutorial/string/try_finally.pyx
0 → 100644
View file @
285ca063
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
)
docs/src/tutorial/strings.rst
View file @
285ca063
...
...
@@ -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::
...
...
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