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
94354722
Commit
94354722
authored
Jun 18, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved an example from string.rst to the examples directory.
parent
5c04c1a8
Changes
2
Hide 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 @
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
)
docs/src/tutorial/strings.rst
View file @
94354722
...
...
@@ -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