Commit 62e59ef4 authored by Robert Bradshaw's avatar Robert Bradshaw

Remove old type conversion code.

parent f6fb2ff5
......@@ -603,12 +603,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln('static const char * %s= %s;' % (Naming.cfilenm_cname, Naming.file_c_macro))
code.putln('static const char *%s;' % Naming.filename_cname)
# XXX this is a mess
for utility_code in PyrexTypes.c_int_from_py_function.specialize_list:
env.use_utility_code(utility_code)
for utility_code in PyrexTypes.c_long_from_py_function.specialize_list:
env.use_utility_code(utility_code)
def generate_extern_c_macro_definition(self, code):
name = Naming.extern_c_macro
code.putln("#ifndef %s" % name)
......
......@@ -1365,191 +1365,6 @@ class CNumericType(CType):
return "(int, long)"
return "float"
c_int_from_py_function = UtilityCode(
proto="""
static CYTHON_INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject *);
""",
impl="""
static CYTHON_INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) {
const %(type)s neg_one = (%(type)s)-1, const_zero = 0;
const int is_unsigned = neg_one > const_zero;
if (sizeof(%(type)s) < sizeof(long)) {
long val = __Pyx_PyInt_AsLong(x);
if (unlikely(val != (long)(%(type)s)val)) {
if (!unlikely(val == -1 && PyErr_Occurred())) {
PyErr_SetString(PyExc_OverflowError,
(is_unsigned && unlikely(val < 0)) ?
"can't convert negative value to %(type)s" :
"value too large to convert to %(type)s");
}
return (%(type)s)-1;
}
return (%(type)s)val;
}
return (%(type)s)__Pyx_PyInt_As%(SignWord)sLong(x);
}
""") #fool emacs: '
c_long_from_py_function = UtilityCode(
proto="""
static CYTHON_INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject *);
""",
impl="""
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
#include "longintrepr.h"
#endif
#endif
static CYTHON_INLINE %(type)s __Pyx_PyInt_As%(SignWord)s%(TypeName)s(PyObject* x) {
const %(type)s neg_one = (%(type)s)-1, const_zero = 0;
const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
if (likely(PyInt_Check(x))) {
long val = PyInt_AS_LONG(x);
if (is_unsigned && unlikely(val < 0)) {
PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to %(type)s");
return (%(type)s)-1;
}
return (%(type)s)val;
} else
#endif
if (likely(PyLong_Check(x))) {
if (is_unsigned) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
if (sizeof(digit) <= sizeof(%(type)s)) {
switch (Py_SIZE(x)) {
case 0: return 0;
case 1: return (%(type)s) ((PyLongObject*)x)->ob_digit[0];
}
}
#endif
#endif
if (unlikely(Py_SIZE(x) < 0)) {
PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to %(type)s");
return (%(type)s)-1;
}
return (%(type)s)PyLong_AsUnsigned%(TypeName)s(x);
} else {
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
#if CYTHON_USE_PYLONG_INTERNALS
if (sizeof(digit) <= sizeof(%(type)s)) {
switch (Py_SIZE(x)) {
case 0: return 0;
case 1: return +(%(type)s) ((PyLongObject*)x)->ob_digit[0];
case -1: return -(%(type)s) ((PyLongObject*)x)->ob_digit[0];
}
}
#endif
#endif
return (%(type)s)PyLong_As%(TypeName)s(x);
}
} else {
%(type)s val;
PyObject *tmp = __Pyx_PyNumber_Int(x);
if (!tmp) return (%(type)s)-1;
val = __Pyx_PyInt_As%(SignWord)s%(TypeName)s(tmp);
Py_DECREF(tmp);
return val;
}
}
""")
c_typedef_int_from_py_function = UtilityCode(
proto="""
static CYTHON_INLINE %(type)s __Pyx_PyInt_from_py_%(TypeName)s(PyObject *);
""",
impl="""
static CYTHON_INLINE %(type)s __Pyx_PyInt_from_py_%(TypeName)s(PyObject* x) {
const %(type)s neg_one = (%(type)s)-1, const_zero = (%(type)s)0;
const int is_unsigned = const_zero < neg_one;
if (sizeof(%(type)s) == sizeof(char)) {
if (is_unsigned)
return (%(type)s)__Pyx_PyInt_AsUnsignedChar(x);
else
return (%(type)s)__Pyx_PyInt_AsSignedChar(x);
} else if (sizeof(%(type)s) == sizeof(short)) {
if (is_unsigned)
return (%(type)s)__Pyx_PyInt_AsUnsignedShort(x);
else
return (%(type)s)__Pyx_PyInt_AsSignedShort(x);
} else if (sizeof(%(type)s) == sizeof(int)) {
if (is_unsigned)
return (%(type)s)__Pyx_PyInt_AsUnsignedInt(x);
else
return (%(type)s)__Pyx_PyInt_AsSignedInt(x);
} else if (sizeof(%(type)s) == sizeof(long)) {
if (is_unsigned)
return (%(type)s)__Pyx_PyInt_AsUnsignedLong(x);
else
return (%(type)s)__Pyx_PyInt_AsSignedLong(x);
} else if (sizeof(%(type)s) == sizeof(PY_LONG_LONG)) {
if (is_unsigned)
return (%(type)s)__Pyx_PyInt_AsUnsignedLongLong(x);
else
return (%(type)s)__Pyx_PyInt_AsSignedLongLong(x);
} else {
#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)
PyErr_SetString(PyExc_RuntimeError,
"_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers");
#else
%(type)s val;
PyObject *v = __Pyx_PyNumber_Int(x);
#if PY_MAJOR_VERSION < 3
if (likely(v) && !PyLong_Check(v)) {
PyObject *tmp = v;
v = PyNumber_Long(tmp);
Py_DECREF(tmp);
}
#endif
if (likely(v)) {
int one = 1; int is_little = (int)*(unsigned char *)&one;
unsigned char *bytes = (unsigned char *)&val;
int ret = _PyLong_AsByteArray((PyLongObject *)v,
bytes, sizeof(val),
is_little, !is_unsigned);
Py_DECREF(v);
if (likely(!ret))
return val;
}
#endif
return (%(type)s)-1;
}
}
""")
c_typedef_int_to_py_function = UtilityCode(
proto="""
static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_%(TypeName)s(%(type)s);
""",
impl="""
static CYTHON_INLINE PyObject *__Pyx_PyInt_to_py_%(TypeName)s(%(type)s val) {
const %(type)s neg_one = (%(type)s)-1, const_zero = (%(type)s)0;
const int is_unsigned = const_zero < neg_one;
if ((sizeof(%(type)s) == sizeof(char)) ||
(sizeof(%(type)s) == sizeof(short))) {
return PyInt_FromLong((long)val);
} else if ((sizeof(%(type)s) == sizeof(int)) ||
(sizeof(%(type)s) == sizeof(long))) {
if (is_unsigned)
return PyLong_FromUnsignedLong((unsigned long)val);
else
return PyInt_FromLong((long)val);
} else if (sizeof(%(type)s) == sizeof(PY_LONG_LONG)) {
if (is_unsigned)
return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)val);
else
return PyLong_FromLongLong((PY_LONG_LONG)val);
} else {
int one = 1; int little = (int)*(unsigned char *)&one;
unsigned char *bytes = (unsigned char *)&val;
return _PyLong_FromByteArray(bytes, sizeof(%(type)s),
little, !is_unsigned);
}
}
""")
class CIntType(CNumericType):
......@@ -1559,14 +1374,6 @@ class CIntType(CNumericType):
from_py_function = None
exception_value = -1
def __init__(self, rank, signed = 1):
CNumericType.__init__(self, rank, signed)
if self.to_py_function is None:
self.to_py_function = self.get_to_py_type_conversion()
if self.from_py_function is None:
# Inject specializatioin used elsewhere.
self.get_from_py_type_conversion()
def create_to_py_utility_code(self, env):
self.to_py_function = "__Pyx_PyInt_to_py_" + self.specialization_name()
env.use_utility_code(TempitaUtilityCode.load(
......@@ -1779,17 +1586,11 @@ class CSSizeTType(CIntType):
class CSizeTType(CIntType):
to_py_function = "__Pyx_PyInt_FromSize_t"
from_py_function = "__Pyx_PyInt_AsSize_t"
def sign_and_name(self):
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"
......
......@@ -44,11 +44,6 @@ static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);
static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
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
#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
......@@ -267,51 +262,6 @@ static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
return ival;
}
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
#if PY_VERSION_HEX < 0x02050000
if (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(size_t), little, 0);
}
#else
return PyInt_FromSize_t(ival);
#endif
}
static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject* x) {
unsigned PY_LONG_LONG val = __Pyx_PyInt_AsUnsignedLongLong(x);
if (unlikely(val != (unsigned PY_LONG_LONG)(size_t)val)) {
if ((val != (unsigned PY_LONG_LONG)-1) || !PyErr_Occurred())
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to size_t");
return (size_t)-1;
}
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 ///////////////
{{struct_type_decl}};
......
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