From: Stefan Behnel Date: Fri, 18 Jan 2013 13:06:26 +0000 (+0100) Subject: update 'const' section in string handling tutorial to reflect the new 'const' languag... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7adee4f62f14e738dbce4011cc96d7ed6bb86167;p=platform%2Fupstream%2Fpython-cython.git 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 --- diff --git a/docs/src/tutorial/strings.rst b/docs/src/tutorial/strings.rst index 8068702..25c5346 100644 --- a/docs/src/tutorial/strings.rst +++ b/docs/src/tutorial/strings.rst @@ -125,9 +125,18 @@ not modify a string they return, for example: int process_string(const char* s); const unsigned char* look_up_cached_string(const unsigned char* key); -Cython does not currently have support for the ``const`` modifier in -the language, but it allows users to make the necessary declarations -at a textual level. +Since version 0.18, Cython has support for the ``const`` modifier in +the language, so you can declare the above functions straight away as +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`` 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:: However, in most other situations, such as for return values and variables that use specifically typedef-ed API types, it does matter -and the C compiler will emit a warning if used incorrectly. To help -with this, you can use the type definitions in the ``libc.string`` -module, e.g.:: +and the C compiler will emit at least a warning if used incorrectly. +To help with this, you can use the type definitions in the +``libc.string`` module, e.g.:: from libc.string cimport const_char, const_uchar @@ -151,9 +160,11 @@ module, e.g.:: const_uchar* look_up_cached_string(const_uchar* key) Note: even if the API only uses ``const`` for function arguments, -it is still preferable to properly declare them using the -:c:type:`const_char` types in order to simplify adaptations, e.g. -if Cython ever gains language support for ``const``. +it is still preferable to properly declare them using these +provided :c:type:`const_char` types in order to simplify adaptations. +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 ----------------------