Commit 3087d7fd authored by Robert Bradshaw's avatar Robert Bradshaw

Add ptrdiff_t type to Cython.

parent e4163aac
...@@ -8895,7 +8895,7 @@ class SubNode(NumBinopNode): ...@@ -8895,7 +8895,7 @@ class SubNode(NumBinopNode):
if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum): if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
return type1 return type1
elif (type1.is_ptr or type1.is_array) and (type2.is_ptr or type2.is_array): elif (type1.is_ptr or type1.is_array) and (type2.is_ptr or type2.is_array):
return PyrexTypes.c_int_type return PyrexTypes.c_ptrdiff_t_type
else: else:
return NumBinopNode.compute_c_result_type( return NumBinopNode.compute_c_result_type(
self, type1, type2) self, type1, type2)
......
...@@ -2264,6 +2264,7 @@ special_basic_c_types = { ...@@ -2264,6 +2264,7 @@ special_basic_c_types = {
"Py_ssize_t" : (2, 0), "Py_ssize_t" : (2, 0),
"ssize_t" : (2, 0), "ssize_t" : (2, 0),
"size_t" : (0, 0), "size_t" : (0, 0),
"ptrdiff_t" : (2, 0),
} }
sign_and_longness_words = ("short", "long", "signed", "unsigned") sign_and_longness_words = ("short", "long", "signed", "unsigned")
......
...@@ -1758,6 +1758,14 @@ class CSizeTType(CIntType): ...@@ -1758,6 +1758,14 @@ class CSizeTType(CIntType):
def sign_and_name(self): def sign_and_name(self):
return "size_t" return "size_t"
class CPtrdiffTType(CIntType):
to_py_function = "__Pyx_PyInt_FromPtrdiff_t"
from_py_function = "__Pyx_PyInt_AsPtrdiff_t"
def sign_and_name(self):
return "ptrdiff_t"
class CFloatType(CNumericType): class CFloatType(CNumericType):
...@@ -3448,6 +3456,7 @@ c_py_hash_t_type = CPyHashTType(RANK_LONG+0.5, SIGNED) ...@@ -3448,6 +3456,7 @@ c_py_hash_t_type = CPyHashTType(RANK_LONG+0.5, SIGNED)
c_py_ssize_t_type = CPySSizeTType(RANK_LONG+0.5, SIGNED) c_py_ssize_t_type = CPySSizeTType(RANK_LONG+0.5, SIGNED)
c_ssize_t_type = CSSizeTType(RANK_LONG+0.5, SIGNED) c_ssize_t_type = CSSizeTType(RANK_LONG+0.5, SIGNED)
c_size_t_type = CSizeTType(RANK_LONG+0.5, UNSIGNED) c_size_t_type = CSizeTType(RANK_LONG+0.5, UNSIGNED)
c_ptrdiff_t_type = CPtrdiffTType(RANK_LONG+0.25, SIGNED)
c_null_ptr_type = CNullPtrType(c_void_type) c_null_ptr_type = CNullPtrType(c_void_type)
c_void_ptr_type = CPtrType(c_void_type) c_void_ptr_type = CPtrType(c_void_type)
...@@ -3541,6 +3550,7 @@ modifiers_and_name_to_type = { ...@@ -3541,6 +3550,7 @@ modifiers_and_name_to_type = {
(2, 0, "Py_ssize_t"): c_py_ssize_t_type, (2, 0, "Py_ssize_t"): c_py_ssize_t_type,
(2, 0, "ssize_t") : c_ssize_t_type, (2, 0, "ssize_t") : c_ssize_t_type,
(0, 0, "size_t") : c_size_t_type, (0, 0, "size_t") : c_size_t_type,
(2, 0, "ptrdiff_t") : c_ptrdiff_t_type,
(1, 0, "object"): py_object_type, (1, 0, "object"): py_object_type,
} }
......
...@@ -47,6 +47,9 @@ static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*); ...@@ -47,6 +47,9 @@ static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t); static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject*); static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject*);
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromPtrdiff_t(ptrdiff_t);
static CYTHON_INLINE ptrdiff_t __Pyx_PyInt_AsPtrdiff_t(PyObject*);
#if CYTHON_COMPILING_IN_CPYTHON #if CYTHON_COMPILING_IN_CPYTHON
#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x)) #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
#else #else
...@@ -289,6 +292,27 @@ static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject* x) { ...@@ -289,6 +292,27 @@ static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject* x) {
return (size_t)val; return (size_t)val;
} }
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromPtrdiff_t(ptrdiff_t ival) {
if (LONG_MIN < ival && ival <= LONG_MAX)
return PyInt_FromLong((long)ival);
else {
unsigned char *bytes = (unsigned char *) &ival;
int one = 1; int little = (int)*(unsigned char*)&one;
return _PyLong_FromByteArray(bytes, sizeof(ptrdiff_t), little, 0);
}
}
static CYTHON_INLINE ptrdiff_t __Pyx_PyInt_AsPtrdiff_t(PyObject* x) {
unsigned PY_LONG_LONG val = __Pyx_PyInt_AsLongLong(x);
if (unlikely(val != (unsigned PY_LONG_LONG)(ptrdiff_t)val)) {
if ((val != (unsigned PY_LONG_LONG)-1) || !PyErr_Occurred())
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to size_t");
return (ptrdiff_t)-1;
}
return (ptrdiff_t)val;
}
/////////////// FromPyStructUtility.proto /////////////// /////////////// FromPyStructUtility.proto ///////////////
{{struct_type_decl}}; {{struct_type_decl}};
static {{struct_type_decl}} {{funcname}}(PyObject *); static {{struct_type_decl}} {{funcname}}(PyObject *);
......
...@@ -7,5 +7,5 @@ def f(): ...@@ -7,5 +7,5 @@ def f():
ptr1 = ptr2 - ptr3 # error ptr1 = ptr2 - ptr3 # error
_ERRORS = u""" _ERRORS = u"""
6:13: Invalid operand types for '-' (int; char *) 6:13: Invalid operand types for '-' (int; char *)
7:13: Cannot assign type 'int' to 'char *' 7:13: Cannot assign type 'ptrdiff_t' to 'char *'
""" """
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