Commit df26fdeb authored by Stefan Behnel's avatar Stefan Behnel

more correct fake implementation of PyNumber_Index() and PyIndex_Check() for Py2.4

parent a3a35f91
...@@ -47,8 +47,11 @@ ...@@ -47,8 +47,11 @@
#define PY_FORMAT_SIZE_T "" #define PY_FORMAT_SIZE_T ""
#define PyInt_FromSsize_t(z) PyInt_FromLong(z) #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
#define PyInt_AsSsize_t(o) __Pyx_PyInt_AsInt(o) #define PyInt_AsSsize_t(o) __Pyx_PyInt_AsInt(o)
#define PyNumber_Index(o) PyNumber_Int(o) #define PyNumber_Index(o) ((PyNumber_Check(o) && !PyFloat_Check(o)) ? \
#define PyIndex_Check(o) PyNumber_Check(o) PyNumber_Int(o) \
: (PyErr_Format(PyExc_TypeError, "expected index value, got %.200s", \
Py_TYPE(o)->tp_name), NULL))
#define PyIndex_Check(o) (PyNumber_Check(o) && !PyFloat_Check(o) && !PyComplex_Check(o))
#define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message) #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)
#define __PYX_BUILD_PY_SSIZE_T "i" #define __PYX_BUILD_PY_SSIZE_T "i"
#else #else
......
# mode: run
# tag: portability
# test some C-API functions that Cython replaces for portability reasons
from cpython.number cimport PyNumber_Index, PyIndex_Check
def number_index(x):
"""
>>> number_index(1)
1
>>> try: number_index(1.1)
... except TypeError: pass
... else: print "FAILED"
>>> try: number_index(1j)
... except TypeError: pass
... else: print "FAILED"
>>> try: number_index('abc')
... except TypeError: pass
... else: print "FAILED"
"""
# was not available in Py2.4
return PyNumber_Index(x)
def index_check(x):
"""
>>> index_check(1)
True
>>> index_check(1.1)
False
>>> index_check(1j)
False
>>> index_check('abc')
False
"""
# was not available in Py2.4
return PyIndex_Check(x)
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