1# ticket: 400 2 3cimport cython 4 5@cython.test_assert_path_exists("//SingleAssignmentNode/TypecastNode") 6@cython.test_fail_if_path_exists("//SimpleCallNode") 7def double_to_short_int(double x): 8 """ 9 >>> double_to_short_int(4.1) 10 4 11 >>> double_to_short_int(4) 12 4 13 >>> double_to_short_int('4') # doctest: +ELLIPSIS 14 Traceback (most recent call last): 15 TypeError: ... 16 """ 17 cdef short r = int(x) 18 return r 19 20@cython.test_assert_path_exists("//SingleAssignmentNode/TypecastNode") 21@cython.test_fail_if_path_exists("//SimpleCallNode") 22def double_to_pyssizet_int(double x): 23 """ 24 >>> double_to_pyssizet_int(4.1) 25 4 26 >>> double_to_pyssizet_int(4) 27 4 28 >>> double_to_pyssizet_int('4') # doctest: +ELLIPSIS 29 Traceback (most recent call last): 30 TypeError: ... 31 """ 32 cdef Py_ssize_t r = int(x) 33 return r 34 35@cython.test_assert_path_exists("//SingleAssignmentNode/TypecastNode") 36@cython.test_fail_if_path_exists("//SimpleCallNode") 37def int_to_pyssizet_int(int x): 38 """ 39 >>> int_to_pyssizet_int(4.1) 40 4 41 >>> int_to_pyssizet_int(4) 42 4 43 >>> int_to_pyssizet_int('4') # doctest: +ELLIPSIS 44 Traceback (most recent call last): 45 TypeError: ... 46 """ 47 cdef Py_ssize_t r = int(x) 48 return r 49 50## @cython.test_assert_path_exists("//SingleAssignmentNode/TypecastNode") 51## @cython.test_fail_if_path_exists("//SimpleCallNode") 52## def double_to_pyssizet_float(double x): 53## """ 54## >>> double_to_pyssizet_float(4.1) 55## 4 56## >>> double_to_pyssizet_float(4) 57## 4 58## """ 59## cdef Py_ssize_t r = float(x) 60## return r 61 62@cython.test_assert_path_exists("//SingleAssignmentNode/TypecastNode") 63@cython.test_fail_if_path_exists("//SimpleCallNode") 64def int_to_short_int(int x): 65 """ 66 >>> int_to_short_int(4) 67 4 68 >>> int_to_short_int('4') # doctest: +ELLIPSIS 69 Traceback (most recent call last): 70 TypeError: ...integer... 71 """ 72 cdef short r = int(x) 73 return r 74 75@cython.test_assert_path_exists("//SingleAssignmentNode/TypecastNode") 76@cython.test_fail_if_path_exists("//SimpleCallNode") 77def short_to_float_float(short x): 78 """ 79 >>> short_to_float_float(4) 80 4.0 81 >>> short_to_float_float('4') # doctest: +ELLIPSIS 82 Traceback (most recent call last): 83 TypeError: ...integer... 84 """ 85 cdef float r = float(x) 86 return r 87 88@cython.test_assert_path_exists("//SingleAssignmentNode/TypecastNode") 89@cython.test_fail_if_path_exists("//SimpleCallNode") 90def short_to_double_float(short x): 91 """ 92 >>> short_to_double_float(4) 93 4.0 94 >>> short_to_double_float('4') # doctest: +ELLIPSIS 95 Traceback (most recent call last): 96 TypeError: ...integer... 97 """ 98 cdef double r = float(x) 99 return r 100 101@cython.test_assert_path_exists("//SingleAssignmentNode/TypecastNode") 102@cython.test_fail_if_path_exists("//SimpleCallNode") 103def short_to_double_int(short x): 104 """ 105 >>> short_to_double_int(4) 106 4.0 107 >>> short_to_double_int('4') # doctest: +ELLIPSIS 108 Traceback (most recent call last): 109 TypeError: ...integer... 110 """ 111 cdef double r = int(x) 112 return r 113 114@cython.test_fail_if_path_exists("//SimpleCallNode") 115def float_to_float_float(float x): 116 """ 117 >>> 4.05 < float_to_float_float(4.1) < 4.15 118 True 119 >>> float_to_float_float(4) 120 4.0 121 >>> float_to_float_float('4') # doctest: +ELLIPSIS 122 Traceback (most recent call last): 123 TypeError: ... 124 """ 125 cdef float r = float(x) 126 return r 127 128@cython.test_fail_if_path_exists("//SimpleCallNode", 129 "//SingleAssignmentNode//TypecastNode") 130def double_to_double_float(double x): 131 """ 132 >>> 4.05 < double_to_double_float(4.1) < 4.15 133 True 134 >>> double_to_double_float(4) 135 4.0 136 >>> double_to_double_float('4') # doctest: +ELLIPSIS 137 Traceback (most recent call last): 138 TypeError: ... 139 """ 140 cdef double r = float(x) 141 return r 142 143# tests that cannot be optimised 144 145@cython.test_fail_if_path_exists("//TypecastNode") 146@cython.test_assert_path_exists("//PythonCapiCallNode") 147def double_to_py_int(double x): 148 """ 149 >>> double_to_py_int(4.1) 150 4 151 >>> double_to_py_int(4) 152 4 153 >>> double_to_py_int('4') # doctest: +ELLIPSIS 154 Traceback (most recent call last): 155 TypeError: ... 156 """ 157 return int(x) 158 159@cython.test_fail_if_path_exists("//SingleAssignmentNode//TypecastNode") 160@cython.test_assert_path_exists("//PythonCapiCallNode") 161def double_to_double_int(double x): 162 """ 163 >>> double_to_double_int(4.1) 164 4.0 165 >>> double_to_double_int(4) 166 4.0 167 >>> double_to_double_int('4') # doctest: +ELLIPSIS 168 Traceback (most recent call last): 169 TypeError: ... 170 """ 171 cdef double r = int(x) 172 return r 173 174 175@cython.test_fail_if_path_exists("//SingleAssignmentNode//TypecastNode") 176@cython.test_assert_path_exists( 177 "//PythonCapiCallNode", 178 "//PythonCapiCallNode/PythonCapiFunctionNode/@cname = 'truncf'", 179) 180def float_to_float_int(float x): 181 """ 182 >>> float_to_float_int(4.1) 183 4.0 184 >>> float_to_float_int(4) 185 4.0 186 >>> float_to_float_int('4') # doctest: +ELLIPSIS 187 Traceback (most recent call last): 188 TypeError: ... 189 """ 190 cdef float r = int(x) 191 return r 192 193 194@cython.test_fail_if_path_exists("//SingleAssignmentNode//TypecastNode") 195@cython.test_assert_path_exists( 196 "//PythonCapiCallNode", 197 "//PythonCapiCallNode/PythonCapiFunctionNode/@cname = 'truncf'", 198) 199def float_to_double_int(float x): 200 """ 201 >>> float_to_double_int(4.1) 202 4.0 203 >>> float_to_double_int(4) 204 4.0 205 >>> float_to_double_int('4') # doctest: +ELLIPSIS 206 Traceback (most recent call last): 207 TypeError: ... 208 """ 209 cdef double r = int(x) 210 return r 211 212 213@cython.test_fail_if_path_exists("//SingleAssignmentNode//TypecastNode") 214@cython.test_assert_path_exists( 215 "//PythonCapiCallNode", 216 "//PythonCapiCallNode/PythonCapiFunctionNode/@cname = 'trunc'", 217) 218def double_to_float_int(double x): 219 """ 220 >>> double_to_float_int(4.1) 221 4.0 222 >>> double_to_float_int(4) 223 4.0 224 >>> double_to_float_int('4') # doctest: +ELLIPSIS 225 Traceback (most recent call last): 226 TypeError: ... 227 """ 228 cdef float r = int(x) 229 return r 230 231 232@cython.test_fail_if_path_exists("//SimpleCallNode") 233@cython.test_assert_path_exists("//PythonCapiCallNode") 234def object_float(x): 235 """ 236 >>> 4.05 < object_float(4.1) < 4.15 237 True 238 >>> object_float(2**100) == float(2**100) 239 True 240 >>> object_float(2.5**100) == float(2.5**100) 241 True 242 >>> object_float(4) 243 4.0 244 >>> object_float('4') 245 4.0 246 >>> object_float('4.0') 247 4.0 248 >>> object_float('4'.encode('ascii')) 249 4.0 250 >>> object_float('4.0'.encode('ascii')) 251 4.0 252 """ 253 return float(x) 254 255@cython.test_fail_if_path_exists("//SimpleCallNode") 256@cython.test_assert_path_exists("//PythonCapiCallNode") 257def object_int(x): 258 """ 259 >>> object_int(4) 260 4 261 >>> object_int(2**100) == 2**100 or object_int(2**100) 262 True 263 >>> object_int(-(2**100)) == -(2**100) or object_int(-(2**100)) 264 True 265 >>> object_int(4.1) 266 4 267 >>> object_int(4.0) 268 4 269 >>> object_int('4') 270 4 271 >>> object_int('4'.encode('ascii')) 272 4 273 """ 274 return int(x) 275 276 277@cython.test_fail_if_path_exists("//SimpleCallNode", 278 "//CoerceFromPyTypeNode") 279def no_args_int_cint(): 280 """ 281 >>> no_args_int_cint() 282 0 283 """ 284 cdef int x = int() 285 return x 286 287 288@cython.test_fail_if_path_exists("//SimpleCallNode", 289 "//CoerceFromPyTypeNode") 290def no_args_float_cdouble(): 291 """ 292 >>> no_args_float_cdouble() 293 (0.0, 0.0) 294 """ 295 cdef double xd = float() 296 cdef float xf = float() 297 return xd, xf 298