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
347d55de
Commit
347d55de
authored
Jun 21, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved examples from the string.rst to the examples directory.
parent
04780910
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
30 deletions
+33
-30
docs/examples/tutorial/string/auto_conversion_1.pyx
docs/examples/tutorial/string/auto_conversion_1.pyx
+9
-0
docs/examples/tutorial/string/auto_conversion_2.pyx
docs/examples/tutorial/string/auto_conversion_2.pyx
+12
-0
docs/examples/tutorial/string/auto_conversion_3.pyx
docs/examples/tutorial/string/auto_conversion_3.pyx
+6
-0
docs/src/tutorial/strings.rst
docs/src/tutorial/strings.rst
+6
-30
No files found.
docs/examples/tutorial/string/auto_conversion_1.pyx
0 → 100644
View file @
347d55de
# cython: c_string_type=unicode, c_string_encoding=utf8
cdef
char
*
c_string
=
'abcdefg'
# implicit decoding:
cdef
object
py_unicode_object
=
c_string
# explicit conversion to Python bytes:
py_bytes_object
=
<
bytes
>
c_string
docs/examples/tutorial/string/auto_conversion_2.pyx
0 → 100644
View file @
347d55de
# cython: c_string_type=str, c_string_encoding=ascii
cdef
char
*
c_string
=
'abcdefg'
# implicit decoding in Py3, bytes conversion in Py2:
cdef
object
py_str_object
=
c_string
# explicit conversion to Python bytes:
py_bytes_object
=
<
bytes
>
c_string
# explicit conversion to Python unicode:
py_bytes_object
=
<
unicode
>
c_string
docs/examples/tutorial/string/auto_conversion_3.pyx
0 → 100644
View file @
347d55de
# cython: c_string_type=unicode, c_string_encoding=ascii
def
func
():
ustring
=
u'abc'
cdef
char
*
s
=
ustring
return
s
[
0
]
# returns u'a'
docs/src/tutorial/strings.rst
View file @
347d55de
...
...
@@ -419,36 +419,17 @@ text, automatic encoding and decoding from and to Python unicode
objects can reduce the code overhead a little. In this case, you
can set the ``c_string_type`` directive in your module to :obj:`unicode`
and the ``c_string_encoding`` to the encoding that your C code uses,
for example:
:
for example:
# cython: c_string_type=unicode, c_string_encoding=utf8
cdef char* c_string = 'abcdefg'
# implicit decoding:
cdef object py_unicode_object = c_string
# explicit conversion to Python bytes:
py_bytes_object = <bytes>c_string
.. literalinclude:: ../../examples/tutorial/string/auto_conversion_1.pyx
The second use case is when all C strings that are being processed
only contain ASCII encodable characters (e.g. numbers) and you want
your code to use the native legacy string type in Python 2 for them,
instead of always using Unicode. In this case, you can set the
string type to :obj:`str`::
# cython: c_string_type=str, c_string_encoding=ascii
string type to :obj:`str`:
cdef char* c_string = 'abcdefg'
# implicit decoding in Py3, bytes conversion in Py2:
cdef object py_str_object = c_string
# explicit conversion to Python bytes:
py_bytes_object = <bytes>c_string
# explicit conversion to Python unicode:
py_bytes_object = <unicode>c_string
.. literalinclude:: ../../examples/tutorial/string/auto_conversion_2.pyx
The other direction, i.e. automatic encoding to C strings, is only
supported for ASCII and the "default encoding", which is usually UTF-8
...
...
@@ -459,14 +440,9 @@ way to limit the lifetime of the encoded string in any sensible way,
thus rendering any attempt to extract a C string pointer from it a
dangerous endeavour. The following safely converts a Unicode string to
ASCII (change ``c_string_encoding`` to ``default`` to use the default
encoding instead)::
# cython: c_string_type=unicode, c_string_encoding=ascii
encoding instead):
def func():
ustring = u'abc'
cdef char* s = ustring
return s[0] # returns u'a'
.. literalinclude:: ../../examples/tutorial/string/auto_conversion_3.pyx
(This example uses a function context in order to safely control the
lifetime of the Unicode string. Global Python variables can be
...
...
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