# HG changeset patch
# User Haoyu Bai <baihaoyu@gmail.com>
# Date 1269752193 -28800
# Node ID 36fed2932afa6cd4a42641086c8d829670ec9a8d
# Parent 22234d577112606c17cab87c8eff3fa35a940c13
patch for #399
diff -r 22234d577112 -r 36fed2932afa Cython/Compiler/Parsing.py
|
a
|
b
|
|
| 1908 | 1908 | special_basic_c_types = { |
| 1909 | 1909 | # name : (signed, longness) |
| 1910 | 1910 | "Py_ssize_t" : (2, 0), |
| | 1911 | "ssize_t" : (2, 0), |
| 1911 | 1912 | "size_t" : (0, 0), |
| 1912 | 1913 | } |
| 1913 | 1914 | |
diff -r 22234d577112 -r 36fed2932afa Cython/Compiler/PyrexTypes.py
|
a
|
b
|
|
| 861 | 861 | def sign_and_name(self): |
| 862 | 862 | return rank_to_type_name[self.rank] |
| 863 | 863 | |
| | 864 | class CSsizeTType(CIntType): |
| | 865 | |
| | 866 | to_py_function = "PyInt_FromSsize_t" |
| | 867 | from_py_function = "__Pyx_PyIndex_AsSsize_t" |
| | 868 | |
| | 869 | def sign_and_name(self): |
| | 870 | return rank_to_type_name[self.rank] |
| 864 | 871 | |
| 865 | 872 | class CSizeTType(CUIntType): |
| 866 | 873 | |
| … |
… |
|
| 2057 | 2064 | "int", # 2 |
| 2058 | 2065 | "long", # 3 |
| 2059 | 2066 | "Py_ssize_t", # 4 |
| 2060 | | "size_t", # 5 |
| 2061 | | "PY_LONG_LONG", # 6 |
| 2062 | | "float", # 7 |
| 2063 | | "double", # 8 |
| 2064 | | "long double", # 9 |
| | 2067 | "ssize_t", # 5 |
| | 2068 | "size_t", # 6 |
| | 2069 | "PY_LONG_LONG", # 7 |
| | 2070 | "float", # 8 |
| | 2071 | "double", # 9 |
| | 2072 | "long double", # 10 |
| 2065 | 2073 | ) |
| 2066 | 2074 | |
| 2067 | 2075 | py_object_type = PyObjectType() |
| … |
… |
|
| 2074 | 2082 | c_ushort_type = CIntType(1, 0) |
| 2075 | 2083 | c_uint_type = CUIntType(2, 0) |
| 2076 | 2084 | c_ulong_type = CULongType(3, 0) |
| 2077 | | c_ulonglong_type = CULongLongType(6, 0) |
| | 2085 | c_ulonglong_type = CULongLongType(7, 0) |
| 2078 | 2086 | |
| 2079 | 2087 | c_char_type = CIntType(0, 1) |
| 2080 | 2088 | c_short_type = CIntType(1, 1) |
| … |
… |
|
| 2087 | 2095 | c_sshort_type = CIntType(1, 2) |
| 2088 | 2096 | c_sint_type = CIntType(2, 2) |
| 2089 | 2097 | c_slong_type = CLongType(3, 2) |
| 2090 | | c_slonglong_type = CLongLongType(6, 2) |
| | 2098 | c_slonglong_type = CLongLongType(7, 2) |
| 2091 | 2099 | |
| 2092 | 2100 | c_py_ssize_t_type = CPySSizeTType(4, 2) |
| 2093 | | c_size_t_type = CSizeTType(5, 0) |
| | 2101 | c_ssize_t_type = CSsizeTType(5, 2) |
| | 2102 | c_size_t_type = CSizeTType(6, 0) |
| 2094 | 2103 | |
| 2095 | | c_float_type = CFloatType(7, math_h_modifier='f') |
| 2096 | | c_double_type = CFloatType(8) |
| 2097 | | c_longdouble_type = CFloatType(9, math_h_modifier='l') |
| | 2104 | c_float_type = CFloatType(8, math_h_modifier='f') |
| | 2105 | c_double_type = CFloatType(9) |
| | 2106 | c_longdouble_type = CFloatType(10, math_h_modifier='l') |
| 2098 | 2107 | |
| 2099 | 2108 | c_double_complex_type = CComplexType(c_double_type) |
| 2100 | 2109 | |
| … |
… |
|
| 2106 | 2115 | c_char_ptr_ptr_type = CPtrType(c_char_ptr_type) |
| 2107 | 2116 | c_int_ptr_type = CPtrType(c_int_type) |
| 2108 | 2117 | c_py_ssize_t_ptr_type = CPtrType(c_py_ssize_t_type) |
| | 2118 | c_ssize_t_ptr_type = CPtrType(c_ssize_t_type) |
| 2109 | 2119 | c_size_t_ptr_type = CPtrType(c_size_t_type) |
| 2110 | 2120 | |
| 2111 | 2121 | c_returncode_type = CIntType(2, 1, is_returncode = 1) |
| … |
… |
|
| 2142 | 2152 | (0, 4): c_py_ssize_t_type, |
| 2143 | 2153 | (1, 4): c_py_ssize_t_type, |
| 2144 | 2154 | (2, 4): c_py_ssize_t_type, |
| 2145 | | (0, 5): c_size_t_type, |
| 2146 | | (1, 5): c_size_t_type, |
| 2147 | | (2, 5): c_size_t_type, |
| | 2155 | (0, 5): c_ssize_t_type, |
| | 2156 | (1, 5): c_ssize_t_type, |
| | 2157 | (2, 5): c_ssize_t_type, |
| | 2158 | (0, 6): c_size_t_type, |
| | 2159 | (1, 6): c_size_t_type, |
| | 2160 | (2, 6): c_size_t_type, |
| 2148 | 2161 | |
| 2149 | | (1, 7): c_float_type, |
| 2150 | | (1, 8): c_double_type, |
| 2151 | | (1, 9): c_longdouble_type, |
| | 2162 | (1, 8): c_float_type, |
| | 2163 | (1, 9): c_double_type, |
| | 2164 | (1, 10): c_longdouble_type, |
| 2152 | 2165 | # In case we're mixing unsigned ints and floats... |
| 2153 | | (0, 7): c_float_type, |
| 2154 | | (0, 8): c_double_type, |
| 2155 | | (0, 9): c_longdouble_type, |
| | 2166 | (0, 8): c_float_type, |
| | 2167 | (0, 9): c_double_type, |
| | 2168 | (0, 10): c_longdouble_type, |
| 2156 | 2169 | } |
| 2157 | 2170 | |
| 2158 | 2171 | modifiers_and_name_to_type = { |
| … |
… |
|
| 2180 | 2193 | (2, 2, "int"): c_slonglong_type, |
| 2181 | 2194 | |
| 2182 | 2195 | (2, 0, "Py_ssize_t"): c_py_ssize_t_type, |
| | 2196 | (2, 0, "ssize_t"): c_ssize_t_type, |
| 2183 | 2197 | (0, 0, "size_t") : c_size_t_type, |
| 2184 | 2198 | |
| 2185 | 2199 | (1, 0, "long"): c_long_type, |
diff -r 22234d577112 -r 36fed2932afa tests/run/ssize_t.pyx
|
a
|
b
|
|
| | 1 | __doc__ = u""" |
| | 2 | >>> test(-1) |
| | 3 | -1 |
| | 4 | >>> test(0) |
| | 5 | 0 |
| | 6 | >>> test(1) |
| | 7 | 1 |
| | 8 | >>> test(2) |
| | 9 | 2 |
| | 10 | >>> str(test((1<<31)-1)) |
| | 11 | '2147483647' |
| | 12 | |
| | 13 | >>> test(-1<<31) |
| | 14 | -2147483648 |
| | 15 | |
| | 16 | >>> test(1<<31) #doctest: +ELLIPSIS |
| | 17 | Traceback (most recent call last): |
| | 18 | ... |
| | 19 | OverflowError: ... |
| | 20 | |
| | 21 | >>> a = A(1,2) |
| | 22 | >>> a.a == 1 |
| | 23 | True |
| | 24 | >>> a.b == 2 |
| | 25 | True |
| | 26 | >>> a.foo(5) |
| | 27 | 5 |
| | 28 | >>> a.foo(-1) |
| | 29 | -1 |
| | 30 | >>> try: a.foo(-(1<<32)) |
| | 31 | ... except (OverflowError, TypeError): print("ERROR") |
| | 32 | ERROR |
| | 33 | >>> a.foo(1 << 180) #doctest: +ELLIPSIS |
| | 34 | Traceback (most recent call last): |
| | 35 | ... |
| | 36 | OverflowError: ... |
| | 37 | |
| | 38 | >>> a.bar(5) |
| | 39 | 5 |
| | 40 | >>> a.bar(-5) |
| | 41 | -5 |
| | 42 | >>> a.bar(-1<<31) == -1<<31 |
| | 43 | True |
| | 44 | >>> a.bar((1<<31)-1) == (1<<31)-1 |
| | 45 | True |
| | 46 | |
| | 47 | >>> a.bar2(5) |
| | 48 | 5 |
| | 49 | >>> a.bar2(-5) |
| | 50 | -5 |
| | 51 | >>> a.bar2(-1<<31) == -1<<31 |
| | 52 | True |
| | 53 | >>> a.bar2((1<<31)-1) == (1<<31)-1 |
| | 54 | True |
| | 55 | |
| | 56 | """ |
| | 57 | |
| | 58 | def test(ssize_t i): |
| | 59 | return i |
| | 60 | |
| | 61 | cdef class A: |
| | 62 | cdef public ssize_t a |
| | 63 | cdef readonly ssize_t b |
| | 64 | |
| | 65 | def __init__(self, ssize_t a, object b): |
| | 66 | self.a = a |
| | 67 | self.b = b |
| | 68 | |
| | 69 | cpdef ssize_t foo(self, ssize_t x): |
| | 70 | cdef object o = x |
| | 71 | return o |
| | 72 | |
| | 73 | cpdef Py_ssize_t bar(self, ssize_t x): |
| | 74 | cdef Py_ssize_t o = x |
| | 75 | return o |
| | 76 | |
| | 77 | cpdef ssize_t bar2(self, Py_ssize_t x): |
| | 78 | cdef ssize_t o = x |
| | 79 | return o |