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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
21771276
Commit
21771276
authored
Jun 19, 2018
by
gabrieldemarmiesse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved stuff from string.rst to the examples directory.
parent
5c04c1a8
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
22 additions
and
20 deletions
+22
-20
docs/examples/tutorial/string/const.pyx
docs/examples/tutorial/string/const.pyx
+4
-0
docs/examples/tutorial/string/const_char.pyx
docs/examples/tutorial/string/const_char.pyx
+6
-0
docs/examples/tutorial/string/const_left_out.pyx
docs/examples/tutorial/string/const_left_out.pyx
+2
-0
docs/examples/tutorial/string/someheader.h
docs/examples/tutorial/string/someheader.h
+3
-0
docs/src/tutorial/strings.rst
docs/src/tutorial/strings.rst
+7
-20
No files found.
docs/examples/tutorial/string/const.pyx
0 → 100644
View file @
21771276
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
)
docs/examples/tutorial/string/const_char.pyx
0 → 100644
View file @
21771276
from
libc.string
cimport
const_char
,
const_uchar
cdef
extern
from
"someheader.h"
:
ctypedef
const_char
specialChar
int
process_string
(
const_char
*
s
)
const_uchar
*
look_up_cached_string
(
const_uchar
*
key
)
docs/examples/tutorial/string/const_left_out.pyx
0 → 100644
View file @
21771276
cdef
extern
from
"someheader.h"
:
int
process_string
(
char
*
s
)
# note: looses API information!
docs/examples/tutorial/string/someheader.h
0 → 100644
View file @
21771276
typedef
const
char
specialChar
;
int
process_string
(
const
char
*
s
);
const
unsigned
char
*
look_up_cached_string
(
const
unsigned
char
*
key
);
docs/src/tutorial/strings.rst
View file @
21771276
...
...
@@ -300,20 +300,13 @@ Many C libraries use the ``const`` modifier in their API to declare
that they will not modify a string, or to require that users must
not modify a string they return, for example:
.. code-block:: c
typedef const char specialChar;
int process_string(const char* s);
const unsigned char* look_up_cached_string(const unsigned char* key);
.. literalinclude:: ../../examples/tutorial/string/someheader.h
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:
:
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)
.. literalinclude:: ../../examples/tutorial/string/const.pyx
Previous versions required users to make the necessary declarations
at a textual level. If you need to support older Cython versions,
...
...
@@ -322,23 +315,17 @@ 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
declaration (e.g. in a .pxd file). The C compiler will still do
the right thing, even if you declare this to Cython:
:
the right thing, even if you declare this to Cython:
cdef extern from "someheader.h":
int process_string(char* s) # note: looses API information!
.. literalinclude:: ../../examples/tutorial/string/const_left_out.pyx
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 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
``libc.string`` module, e.g.:
cdef extern from "someheader.h":
ctypedef const_char specialChar
int process_string(const_char* s)
const_uchar* look_up_cached_string(const_uchar* key)
.. literalinclude:: ../../examples/tutorial/string/const_char.pyx
Note: even if the API only uses ``const`` for function arguments,
it is still preferable to properly declare them using these
...
...
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