diff -r e898a5663681 Cython/Compiler/ExprNodes.py
|
a
|
b
|
|
| 3892 | 3892 | class SizeofNode(ExprNode): |
| 3893 | 3893 | # Abstract base class for sizeof(x) expression nodes. |
| 3894 | 3894 | |
| 3895 | | type = PyrexTypes.c_int_type |
| | 3895 | type = PyrexTypes.c_size_t_type |
| 3896 | 3896 | |
| 3897 | 3897 | def check_const(self): |
| 3898 | 3898 | pass |
diff -r e898a5663681 Cython/Compiler/Parsing.py
|
a
|
b
|
|
| 1810 | 1810 | return result |
| 1811 | 1811 | else: |
| 1812 | 1812 | return 0 |
| 1813 | | |
| 1814 | | basic_c_type_names = ("void", "char", "int", "float", "double", "Py_ssize_t", "bint") |
| | 1813 | |
| | 1814 | basic_c_type_names = ("void", "char", "int", "float", "double", "Py_ssize_t", "size_t", "bint") |
| 1815 | 1815 | |
| 1816 | 1816 | sign_and_longness_words = ("short", "long", "signed", "unsigned") |
| 1817 | 1817 | |
diff -r e898a5663681 Cython/Compiler/PyrexTypes.py
|
a
|
b
|
|
| 462 | 462 | default_value = "0" |
| 463 | 463 | |
| 464 | 464 | parsetuple_formats = ( # rank -> format |
| 465 | | "BHIkK????", # unsigned |
| 466 | | "bhilL?fd?", # assumed signed |
| 467 | | "bhilL?fd?", # explicitly signed |
| | 465 | "BHIkK?????", # unsigned |
| | 466 | "bhilL??fd?", # assumed signed |
| | 467 | "bhilL??fd?", # explicitly signed |
| 468 | 468 | ) |
| 469 | 469 | |
| 470 | 470 | sign_words = ("unsigned ", "", "signed ") |
| … |
… |
|
| 594 | 594 | from_py_function = "__pyx_PyIndex_AsSsize_t" |
| 595 | 595 | |
| 596 | 596 | |
| | 597 | class CSizeTType(CUIntType): |
| | 598 | |
| | 599 | to_py_function = "__pyx_PyInt_FromSize_t" |
| | 600 | from_py_function = "__pyx_PyInt_AsSize_t" |
| | 601 | |
| | 602 | |
| 597 | 603 | class CFloatType(CNumericType): |
| 598 | 604 | |
| 599 | 605 | is_float = 1 |
| … |
… |
|
| 1153 | 1159 | "long", # 3 |
| 1154 | 1160 | "PY_LONG_LONG", # 4 |
| 1155 | 1161 | "Py_ssize_t", # 5 |
| 1156 | | "float", # 6 |
| 1157 | | "double", # 7 |
| 1158 | | "long double", # 8 |
| | 1162 | "size_t", # 6 |
| | 1163 | "float", # 7 |
| | 1164 | "double", # 8 |
| | 1165 | "long double", # 9 |
| 1159 | 1166 | ) |
| 1160 | 1167 | |
| 1161 | 1168 | py_object_type = PyObjectType() |
| … |
… |
|
| 1175 | 1182 | c_int_type = CIntType(2, 1, "T_INT") |
| 1176 | 1183 | c_long_type = CIntType(3, 1, "T_LONG") |
| 1177 | 1184 | c_longlong_type = CLongLongType(4, 1, "T_LONGLONG") |
| 1178 | | c_py_ssize_t_type = CPySSizeTType(5, 1) |
| 1179 | 1185 | c_bint_type = CBIntType(2, 1, "T_INT") |
| 1180 | 1186 | |
| 1181 | 1187 | c_schar_type = CIntType(0, 2, "T_CHAR") |
| … |
… |
|
| 1184 | 1190 | c_slong_type = CIntType(3, 2, "T_LONG") |
| 1185 | 1191 | c_slonglong_type = CLongLongType(4, 2, "T_LONGLONG") |
| 1186 | 1192 | |
| 1187 | | c_float_type = CFloatType(6, "T_FLOAT") |
| 1188 | | c_double_type = CFloatType(7, "T_DOUBLE") |
| 1189 | | c_longdouble_type = CFloatType(8) |
| | 1193 | c_py_ssize_t_type = CPySSizeTType(5, 1) |
| | 1194 | c_size_t_type = CSizeTType(6, 1) |
| | 1195 | |
| | 1196 | c_float_type = CFloatType(7, "T_FLOAT") |
| | 1197 | c_double_type = CFloatType(8, "T_DOUBLE") |
| | 1198 | c_longdouble_type = CFloatType(9) |
| 1190 | 1199 | |
| 1191 | 1200 | c_null_ptr_type = CNullPtrType(c_void_type) |
| 1192 | 1201 | c_char_array_type = CCharArrayType(None) |
| 1193 | 1202 | c_char_ptr_type = CCharPtrType() |
| 1194 | 1203 | c_utf8_char_array_type = CUTF8CharArrayType(None) |
| 1195 | 1204 | c_char_ptr_ptr_type = CPtrType(c_char_ptr_type) |
| | 1205 | c_int_ptr_type = CPtrType(c_int_type) |
| 1196 | 1206 | c_py_ssize_t_ptr_type = CPtrType(c_py_ssize_t_type) |
| 1197 | | c_int_ptr_type = CPtrType(c_int_type) |
| | 1207 | c_size_t_ptr_type = CPtrType(c_size_t_type) |
| 1198 | 1208 | |
| 1199 | 1209 | c_returncode_type = CIntType(2, 1, "T_INT", is_returncode = 1) |
| 1200 | 1210 | |
| … |
… |
|
| 1207 | 1217 | error_type = ErrorType() |
| 1208 | 1218 | unspecified_type = UnspecifiedType() |
| 1209 | 1219 | |
| 1210 | | lowest_float_rank = 6 |
| 1211 | | |
| 1212 | 1220 | sign_and_rank_to_type = { |
| 1213 | 1221 | #(signed, rank) |
| 1214 | 1222 | (0, 0, ): c_uchar_type, |
| 1215 | 1223 | (0, 1): c_ushort_type, |
| 1216 | 1224 | (0, 2): c_uint_type, |
| 1217 | | (0, 3): c_ulong_type, |
| 1218 | | (0, 4): c_ulonglong_type, |
| 1219 | | (0, 5): c_ulonglong_type, # I'm not sure about this. this should be for size_t Py_ssize_t |
| | 1225 | (0, 3): c_ulong_type, |
| | 1226 | (0, 4): c_ulonglong_type, |
| | 1227 | (0, 5): c_ulonglong_type, |
| | 1228 | |
| 1220 | 1229 | (1, 0): c_char_type, |
| 1221 | 1230 | (1, 1): c_short_type, |
| 1222 | 1231 | (1, 2): c_int_type, |
| 1223 | 1232 | (1, 3): c_long_type, |
| 1224 | 1233 | (1, 4): c_longlong_type, |
| 1225 | | (1, 5): c_py_ssize_t_type, |
| 1226 | 1234 | (2, 0): c_schar_type, |
| 1227 | 1235 | (2, 1): c_sshort_type, |
| 1228 | 1236 | (2, 2): c_sint_type, |
| 1229 | 1237 | (2, 3): c_slong_type, |
| 1230 | 1238 | (2, 4): c_slonglong_type, |
| | 1239 | |
| | 1240 | (1, 5): c_py_ssize_t_type, |
| 1231 | 1241 | (2, 5): c_py_ssize_t_type, |
| | 1242 | (0, 6): c_size_t_type, |
| | 1243 | (1, 6): c_size_t_type, |
| | 1244 | |
| 1232 | 1245 | (1, 6): c_float_type, |
| 1233 | 1246 | (1, 7): c_double_type, |
| 1234 | 1247 | (1, 8): c_longdouble_type, |
| … |
… |
|
| 1251 | 1264 | (1, 0, "int"): c_int_type, |
| 1252 | 1265 | (1, 1, "int"): c_long_type, |
| 1253 | 1266 | (1, 2, "int"): c_longlong_type, |
| 1254 | | (1, 0, "Py_ssize_t"): c_py_ssize_t_type, |
| 1255 | 1267 | (1, 0, "float"): c_float_type, |
| 1256 | 1268 | (1, 0, "double"): c_double_type, |
| 1257 | 1269 | (1, 1, "double"): c_longdouble_type, |
| … |
… |
|
| 1262 | 1274 | (2, 0, "int"): c_sint_type, |
| 1263 | 1275 | (2, 1, "int"): c_slong_type, |
| 1264 | 1276 | (2, 2, "int"): c_slonglong_type, |
| | 1277 | |
| | 1278 | (1, 0, "Py_ssize_t"): c_py_ssize_t_type, |
| 1265 | 1279 | (2, 0, "Py_ssize_t"): c_py_ssize_t_type, |
| | 1280 | (0, 0, "size_t") : c_size_t_type, |
| | 1281 | (1, 0, "size_t") : c_size_t_type, |
| 1266 | 1282 | |
| 1267 | 1283 | (1, 0, "long"): c_long_type, |
| 1268 | 1284 | (1, 0, "short"): c_short_type, |
| … |
… |
|
| 1380 | 1396 | static INLINE unsigned PY_LONG_LONG __pyx_PyInt_AsUnsignedLongLong(PyObject* x); |
| 1381 | 1397 | static INLINE Py_ssize_t __pyx_PyIndex_AsSsize_t(PyObject* b); |
| 1382 | 1398 | |
| | 1399 | static INLINE PyObject * __pyx_PyInt_FromSize_t(size_t); |
| | 1400 | static INLINE size_t __pyx_PyInt_AsSize_t(PyObject*); |
| | 1401 | |
| 1383 | 1402 | #define __pyx_PyInt_AsLong(x) (PyInt_CheckExact(x) ? PyInt_AS_LONG(x) : PyInt_AsLong(x)) |
| 1384 | 1403 | #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x)) |
| 1385 | 1404 | """ + type_conversion_predeclarations |
| … |
… |
|
| 1396 | 1415 | return ival; |
| 1397 | 1416 | } |
| 1398 | 1417 | |
| | 1418 | static INLINE PyObject * __pyx_PyInt_FromSize_t(size_t ival) { |
| | 1419 | #if PY_VERSION_HEX < 0x02050000 |
| | 1420 | if (ival <= (size_t)LONG_MAX) |
| | 1421 | return PyInt_FromLong((long)ival); |
| | 1422 | else { |
| | 1423 | unsigned char *bytes = (unsigned char *) &ival; |
| | 1424 | int one = 1; |
| | 1425 | return _PyLong_FromByteArray(bytes, sizeof(size_t), |
| | 1426 | (int)*(unsigned char*)&one, 0); |
| | 1427 | } |
| | 1428 | #else |
| | 1429 | return PyInt_FromSize_t(ival); |
| | 1430 | #endif |
| | 1431 | } |
| | 1432 | |
| | 1433 | static INLINE size_t __pyx_PyInt_AsSize_t(PyObject* b) { |
| | 1434 | unsigned PY_LONG_LONG val = __pyx_PyInt_AsUnsignedLongLong(b); |
| | 1435 | if (unlikely(val == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())) { |
| | 1436 | return (size_t)-1; |
| | 1437 | } else if (unlikely((unsigned PY_LONG_LONG)(size_t)val != val)) { |
| | 1438 | PyErr_SetString(PyExc_OverflowError, "value too large to convert to size_t"); |
| | 1439 | } |
| | 1440 | return val; |
| | 1441 | } |
| | 1442 | |
| 1399 | 1443 | static INLINE int __Pyx_PyObject_IsTrue(PyObject* x) { |
| 1400 | 1444 | if (x == Py_True) return 1; |
| 1401 | 1445 | else if (x == Py_False) return 0; |
diff -r e898a5663681 Cython/Includes/python2.5.pxd
|
a
|
b
|
|
| 14 | 14 | # - Add unicode calls. |
| 15 | 15 | # - Add setobject calls. |
| 16 | 16 | |
| 17 | | cdef extern from "sys/types.h": |
| 18 | | ctypedef unsigned int size_t |
| 19 | | |
| 20 | 17 | cdef extern from "stdio.h": |
| 21 | 18 | ctypedef struct FILE: |
| 22 | 19 | pass |
diff -r e898a5663681 Cython/Includes/python_mem.pxd
|
a
|
b
|
|
| 1 | 1 | cdef extern from "Python.h": |
| 2 | | ctypedef unsigned long size_t |
| 3 | | |
| | 2 | |
| 4 | 3 | ##################################################################### |
| 5 | 4 | # 9.2 Memory Interface |
| 6 | 5 | ##################################################################### |
diff -r e898a5663681 Cython/Includes/stdlib.pxd
|
a
|
b
|
|
| 1 | 1 | |
| 2 | 2 | cdef extern from "stdlib.h": |
| 3 | | ctypedef unsigned long size_t |
| 4 | 3 | void free(void *ptr) |
| 5 | 4 | void *malloc(size_t size) |
| 6 | 5 | void *realloc(void *ptr, size_t size) |
diff -r e898a5663681 tests/run/size_t.pyx
|
a
|
b
|
|
| | 1 | __doc__ = u""" |
| | 2 | >>> test(0) |
| | 3 | 0 |
| | 4 | >>> test(1) |
| | 5 | 1 |
| | 6 | >>> test(2) |
| | 7 | 2 |
| | 8 | >>> str(test((1<<32)-1)) |
| | 9 | '4294967295' |
| | 10 | |
| | 11 | >>> test(-1) #doctest: +ELLIPSIS |
| | 12 | Traceback (most recent call last): |
| | 13 | ... |
| | 14 | OverflowError: ... |
| | 15 | |
| | 16 | >>> test(1<<128) #doctest: +ELLIPSIS |
| | 17 | Traceback (most recent call last): |
| | 18 | ... |
| | 19 | OverflowError: ... |
| | 20 | """ |
| | 21 | |
| | 22 | def test(size_t i): |
| | 23 | return i |