Commit 7adee4f6 authored by Stefan Behnel's avatar Stefan Behnel

update 'const' section in string handling tutorial to reflect the new 'const' language support

--HG--
extra : transplant_source : U%15%0B%8E%81%02%F2kE%AA%07u%EF%82%3D14%F1C%86
parent 2cec986e
...@@ -125,9 +125,18 @@ not modify a string they return, for example: ...@@ -125,9 +125,18 @@ not modify a string they return, for example:
int process_string(const char* s); int process_string(const char* s);
const unsigned char* look_up_cached_string(const unsigned char* key); const unsigned char* look_up_cached_string(const unsigned char* key);
Cython does not currently have support for the ``const`` modifier in Since version 0.18, Cython has support for the ``const`` modifier in
the language, but it allows users to make the necessary declarations the language, so you can declare the above functions straight away as
at a textual level. follows::
cdef extern from "someheader.h":
ctypedef const char specialChar
int process_string(const char* s)
const unsigned char* look_up_cached_string(const unsigned char* key)
Previous versions required users to make the necessary declarations
at a textual level. If you need to support older Cython versions,
you can use the following approach.
In general, for arguments of external C functions, the ``const`` In general, for arguments of external C functions, the ``const``
modifier does not matter and can be left out in the Cython modifier does not matter and can be left out in the Cython
...@@ -139,9 +148,9 @@ the right thing, even if you declare this to Cython:: ...@@ -139,9 +148,9 @@ the right thing, even if you declare this to Cython::
However, in most other situations, such as for return values and However, in most other situations, such as for return values and
variables that use specifically typedef-ed API types, it does matter variables that use specifically typedef-ed API types, it does matter
and the C compiler will emit a warning if used incorrectly. To help and the C compiler will emit at least a warning if used incorrectly.
with this, you can use the type definitions in the ``libc.string`` To help with this, you can use the type definitions in the
module, e.g.:: ``libc.string`` module, e.g.::
from libc.string cimport const_char, const_uchar from libc.string cimport const_char, const_uchar
...@@ -151,9 +160,11 @@ module, e.g.:: ...@@ -151,9 +160,11 @@ module, e.g.::
const_uchar* look_up_cached_string(const_uchar* key) const_uchar* look_up_cached_string(const_uchar* key)
Note: even if the API only uses ``const`` for function arguments, Note: even if the API only uses ``const`` for function arguments,
it is still preferable to properly declare them using the it is still preferable to properly declare them using these
:c:type:`const_char` types in order to simplify adaptations, e.g. provided :c:type:`const_char` types in order to simplify adaptations.
if Cython ever gains language support for ``const``. In Cython 0.18, these standard declarations have been changed to
use the correct ``const`` modifier, so your code will automatically
benefit from the new ``const`` support if it uses them.
Decoding bytes to text Decoding bytes to text
---------------------- ----------------------
......
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