Commit 829d7bbe authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2077 from alexhenrie/master

Rewrite documentation paragraph on automatic string encoding for clarity
parents 0961b9a7 cf4bf9b5
...@@ -528,15 +528,15 @@ string type to :obj:`str`:: ...@@ -528,15 +528,15 @@ string type to :obj:`str`::
py_bytes_object = <unicode>c_string py_bytes_object = <unicode>c_string
The other direction, i.e. automatic encoding to C strings, is only The other direction, i.e. automatic encoding to C strings, is only
supported for the ASCII codec (and the "default encoding", which is supported for ASCII and the "default encoding", which is usually UTF-8
runtime specific and may or may not be ASCII). This is because in Python 3 and usually ASCII in Python 2. CPython handles the memory
CPython handles the memory management in this case by keeping an management in this case by keeping an encoded copy of the string alive
encoded copy of the string alive together with the original unicode together with the original unicode string. Otherwise, there would be no
string. Otherwise, there would be no way to limit the lifetime of way to limit the lifetime of the encoded string in any sensible way,
the encoded string in any sensible way, thus rendering any attempt to thus rendering any attempt to extract a C string pointer from it a
extract a C string pointer from it a dangerous endeavour. As long dangerous endeavour. The following safely converts a Unicode string to
as you stick to the ASCII encoding for the ``c_string_encoding`` ASCII (change ``c_string_encoding`` to ``default`` to use the default
directive, though, the following will work:: encoding instead)::
# cython: c_string_type=unicode, c_string_encoding=ascii # cython: c_string_type=unicode, c_string_encoding=ascii
......
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