1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
# mode: run
# tag: cpp, werror, cpp11
import sys
from collections import defaultdict
from libcpp.map cimport map
from libcpp.unordered_map cimport unordered_map
from libcpp.set cimport set as cpp_set
from libcpp.unordered_set cimport unordered_set
from libcpp.string cimport string
from libcpp.pair cimport pair
from libcpp.vector cimport vector
from libcpp.list cimport list as cpp_list
py_set = set
py_xrange = xrange
py_unicode = unicode
cdef string add_strings(string a, string b) except *:
return a + b
def normalize(bytes b):
if sys.version_info[0] >= 3:
return b.decode("ascii")
else:
return b
def test_string(o):
"""
>>> normalize(test_string("abc".encode('ascii')))
'abc'
>>> normalize(test_string("abc\\x00def".encode('ascii')))
'abc\\x00def'
"""
cdef string s = o
return s
def test_encode_to_string(o):
"""
>>> normalize(test_encode_to_string('abc'))
'abc'
>>> normalize(test_encode_to_string('abc\\x00def'))
'abc\\x00def'
"""
cdef string s = o.encode('ascii')
return s
def test_encode_to_string_cast(o):
"""
>>> normalize(test_encode_to_string_cast('abc'))
'abc'
>>> normalize(test_encode_to_string_cast('abc\\x00def'))
'abc\\x00def'
"""
s = <string>o.encode('ascii')
return s
def test_unicode_encode_to_string(unicode o):
"""
>>> normalize(test_unicode_encode_to_string(py_unicode('abc')))
'abc'
>>> normalize(test_unicode_encode_to_string(py_unicode('abc\\x00def')))
'abc\\x00def'
"""
cdef string s = o.encode('ascii')
return s
def test_string_call(a, b):
"""
>>> normalize(test_string_call("abc".encode('ascii'), "xyz".encode('ascii')))
'abcxyz'
"""
return add_strings(a, b)
def test_c_string_convert(char *c_string):
"""
>>> normalize(test_c_string_convert("abc".encode('ascii')))
'abc'
"""
cdef string s
with nogil:
s = c_string
return s
def test_bint_vector(o):
"""
https://github.com/cython/cython/issues/5516
Creating the "bint" specialization used to mess up the
"int" specialization.
>>> test_bint_vector([False, True])
[False, True]
>>> test_bint_vector(py_xrange(0,5))
[False, True, True, True, True]
>>> test_bint_vector(["", "hello"])
[False, True]
"""
cdef vector[bint] v = o
return v
def test_int_vector(o):
"""
>>> test_int_vector([1, 2, 3])
[1, 2, 3]
>>> test_int_vector((1, 10, 100))
[1, 10, 100]
>>> test_int_vector(py_xrange(1,10,2))
[1, 3, 5, 7, 9]
>>> test_int_vector([10**20]) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
"""
cdef vector[int] v = o
return v
cdef vector[int] takes_vector(vector[int] x):
return x
def test_list_literal_to_vector():
"""
>>> test_list_literal_to_vector()
[1, 2, 3]
"""
return takes_vector([1, 2, 3])
def test_tuple_literal_to_vector():
"""
>>> test_tuple_literal_to_vector()
[1, 2, 3]
"""
return takes_vector((1, 2, 3))
def test_string_vector(s):
"""
>>> list(map(normalize, test_string_vector('ab cd ef gh'.encode('ascii'))))
['ab', 'cd', 'ef', 'gh']
"""
cdef vector[string] cpp_strings = s.split()
return cpp_strings
cdef list convert_string_vector(vector[string] vect):
return vect
def test_string_vector_temp_funcarg(s):
"""
>>> list(map(normalize, test_string_vector_temp_funcarg('ab cd ef gh'.encode('ascii'))))
['ab', 'cd', 'ef', 'gh']
"""
return convert_string_vector(s.split())
def test_double_vector(o):
"""
>>> test_double_vector([1, 2, 3])
[1.0, 2.0, 3.0]
>>> test_double_vector([10**20])
[1e+20]
"""
cdef vector[double] v = o
return v
def test_repeated_double_vector(a, b, int n):
"""
>>> test_repeated_double_vector(1, 1.5, 3)
[1.0, 1.5, 1.0, 1.5, 1.0, 1.5]
"""
cdef vector[double] v = [a, b] * n
return v
ctypedef int my_int
def test_typedef_vector(o):
"""
>>> test_typedef_vector([1, 2, 3])
[1, 2, 3]
>>> test_typedef_vector([1, 2, 3**100]) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
OverflowError: ...
"TypeError: an integer is required" on CPython
>>> test_typedef_vector([1, 2, None]) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: ...int...
"""
cdef vector[my_int] v = o
return v
def test_pair(o):
"""
>>> test_pair((1, 2))
(1, 2.0)
"""
cdef pair[long, double] p = o
return p
def test_list(o):
"""
>>> test_list([1, 2, 3])
[1, 2, 3]
"""
cdef cpp_list[int] l = o
return l
def test_set(o):
"""
>>> sorted(test_set([1, 2, 3]))
[1, 2, 3]
>>> sorted(test_set([1, 2, 3, 3]))
[1, 2, 3]
>>> type(test_set([])) is py_set
True
"""
cdef cpp_set[long] s = o
return s
def test_unordered_set(o):
"""
>>> sorted(test_unordered_set([1, 2, 3]))
[1, 2, 3]
>>> sorted(test_unordered_set([1, 2, 3, 3]))
[1, 2, 3]
>>> type(test_unordered_set([])) is py_set
True
"""
cdef unordered_set[long] s = o
return s
def test_map(o):
"""
>>> d = {1: 1.0, 2: 0.5, 3: 0.25}
>>> test_map(d)
{1: 1.0, 2: 0.5, 3: 0.25}
>>> dd = defaultdict(float)
>>> dd.update(d)
>>> test_map(dd) # try with a non-dict
{1: 1.0, 2: 0.5, 3: 0.25}
"""
cdef map[int, double] m = o
return m
def test_unordered_map(o):
"""
>>> d = {1: 1.0, 2: 0.5, 3: 0.25}
>>> m = test_map(d)
>>> sorted(m)
[1, 2, 3]
>>> (m[1], m[2], m[3])
(1.0, 0.5, 0.25)
>>> dd = defaultdict(float)
>>> dd.update(d)
>>> m = test_map(dd)
>>> sorted(m)
[1, 2, 3]
>>> (m[1], m[2], m[3])
(1.0, 0.5, 0.25)
"""
cdef unordered_map[int, double] m = o
return m
def test_nested(o):
"""
>>> test_nested({})
{}
>>> d = test_nested({(1.0, 2.0): [1, 2, 3], (1.0, 0.5): [1, 10, 100]})
>>> type(d) is dict or type(d)
True
>>> sorted(d)
[(1.0, 0.5), (1.0, 2.0)]
>>> d[(1.0, 0.5)]
[1, 10, 100]
>>> d[(1.0, 2.0)]
[1, 2, 3]
"""
cdef map[pair[double, double], vector[int]] m = o
return m
cpdef enum Color:
RED = 0
GREEN
BLUE
def test_enum_map(o):
"""
>>> test_enum_map({RED: GREEN})
{<Color.RED: 0>: <Color.GREEN: 1>}
"""
cdef map[Color, Color] m = o
return m
cdef map[unsigned int, unsigned int] takes_map(map[unsigned int, unsigned int] m):
return m
def test_dict_literal_to_map():
"""
>>> test_dict_literal_to_map()
{1: 1}
"""
return takes_map({1: 1}) # https://github.com/cython/cython/pull/4228
# DictNode could not be converted directly
|