Commit b4fa0cd9 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2446 from gabrieldemarmiesse/test_wrapping_cplusplus_5

Added tests for "Using C++ in Cython" part 5
parents 4d7de983 444c11c4
# distutils: language = c++
from libcpp.string cimport string
from libcpp.vector cimport vector
py_bytes_object = b'The knights who say ni'
py_unicode_object = u'Those who hear them seldom live to tell the tale.'
cdef string s = py_bytes_object
print(s) # b'The knights who say ni'
cdef string cpp_string = <string> py_unicode_object.encode('utf-8')
print(cpp_string) # b'Those who hear them seldom live to tell the tale.'
cdef vector[int] vect = range(1, 10, 2)
print(vect) # [1, 3, 5, 7, 9]
cdef vector[string] cpp_strings = b'It is a good shrubbery'.split()
print(cpp_strings[1]) # b'is'
......@@ -374,20 +374,9 @@ how to declare C++ classes.
Since Cython 0.17, the STL containers coerce from and to the
corresponding Python builtin types. The conversion is triggered
either by an assignment to a typed variable (including typed function
arguments) or by an explicit cast, e.g.::
arguments) or by an explicit cast, e.g.:
from libcpp.string cimport string
from libcpp.vector cimport vector
cdef string s = py_bytes_object
print(s)
cpp_string = <string> py_unicode_object.encode('utf-8')
cdef vector[int] vect = xrange(1, 10, 2)
print(vect) # [1, 3, 5, 7, 9]
cdef vector[string] cpp_strings = b'ab cd ef gh'.split()
print(cpp_strings[1]) # b'cd'
.. literalinclude:: ../../examples/userguide/wrapping_CPlusPlus/python_to_cpp.pyx
The following coercions are available:
......
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