summaryrefslogtreecommitdiff
path: root/numpy/core/src
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2009-12-06 11:58:40 +0000
committerPauli Virtanen <pav@iki.fi>2009-12-06 11:58:40 +0000
commit0a9e563f4f4ddd0c90feb0f30736795a285ebd9e (patch)
tree570854eaf62f73d6b21b5f042ae828b7164b720f /numpy/core/src
parente96f9e98da9e8e0c045969833345140b3a06b852 (diff)
downloadnumpy-0a9e563f4f4ddd0c90feb0f30736795a285ebd9e.tar.gz
3K: core: adapt to changes in PyNumberMethods
Some entries were removed in Py3K -- easy to adapt to.
Diffstat (limited to 'numpy/core/src')
-rw-r--r--numpy/core/src/multiarray/conversion_utils.c7
-rw-r--r--numpy/core/src/multiarray/number.c20
-rw-r--r--numpy/core/src/multiarray/scalartypes.c.src129
-rw-r--r--numpy/core/src/scalarmathmodule.c.src42
4 files changed, 185 insertions, 13 deletions
diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c
index 43c353a42..d266cdfc3 100644
--- a/numpy/core/src/multiarray/conversion_utils.c
+++ b/numpy/core/src/multiarray/conversion_utils.c
@@ -377,6 +377,7 @@ PyArray_PyIntAsInt(PyObject *o)
long_value = (long) PyLong_AsLong(obj);
Py_DECREF(obj);
}
+#if !defined(NPY_PY3K)
else if (Py_TYPE(o)->tp_as_number != NULL && \
Py_TYPE(o)->tp_as_number->nb_long != NULL) {
obj = Py_TYPE(o)->tp_as_number->nb_long(o);
@@ -386,6 +387,7 @@ PyArray_PyIntAsInt(PyObject *o)
long_value = (long) PyLong_AsLong(obj);
Py_DECREF(obj);
}
+#endif
else {
PyErr_SetString(PyExc_NotImplementedError,"");
}
@@ -465,6 +467,7 @@ PyArray_PyIntAsIntp(PyObject *o)
goto finish;
}
#endif
+#if !defined(NPY_PY3K)
if (Py_TYPE(o)->tp_as_number != NULL && \
Py_TYPE(o)->tp_as_number->nb_long != NULL) {
obj = Py_TYPE(o)->tp_as_number->nb_long(o);
@@ -473,7 +476,9 @@ PyArray_PyIntAsIntp(PyObject *o)
Py_DECREF(obj);
}
}
- else if (Py_TYPE(o)->tp_as_number != NULL && \
+ else
+#endif
+ if (Py_TYPE(o)->tp_as_number != NULL && \
Py_TYPE(o)->tp_as_number->nb_int != NULL) {
obj = Py_TYPE(o)->tp_as_number->nb_int(o);
if (obj != NULL) {
diff --git a/numpy/core/src/multiarray/number.c b/numpy/core/src/multiarray/number.c
index 4d2a5e849..bfb2c816d 100644
--- a/numpy/core/src/multiarray/number.c
+++ b/numpy/core/src/multiarray/number.c
@@ -643,6 +643,8 @@ array_float(PyArrayObject *v)
return pv2;
}
+#if !defined(NPY_PY3K)
+
static PyObject *
array_long(PyArrayObject *v)
{
@@ -718,6 +720,8 @@ array_hex(PyArrayObject *v)
return pv2;
}
+#endif
+
static PyObject *
_array_copy_nice(PyArrayObject *self)
{
@@ -742,7 +746,10 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = {
(binaryfunc)array_add, /*nb_add*/
(binaryfunc)array_subtract, /*nb_subtract*/
(binaryfunc)array_multiply, /*nb_multiply*/
+#if defined(NPY_PY3K)
+#else
(binaryfunc)array_divide, /*nb_divide*/
+#endif
(binaryfunc)array_remainder, /*nb_remainder*/
(binaryfunc)array_divmod, /*nb_divmod*/
(ternaryfunc)array_power, /*nb_power*/
@@ -756,12 +763,22 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = {
(binaryfunc)array_bitwise_and, /*nb_and*/
(binaryfunc)array_bitwise_xor, /*nb_xor*/
(binaryfunc)array_bitwise_or, /*nb_or*/
+#if defined(NPY_PY3K)
+#else
0, /*nb_coerce*/
+#endif
(unaryfunc)array_int, /*nb_int*/
+#if defined(NPY_PY3K)
+ 0, /*nb_reserved*/
+#else
(unaryfunc)array_long, /*nb_long*/
+#endif
(unaryfunc)array_float, /*nb_float*/
+#if defined(NPY_PY3K)
+#else
(unaryfunc)array_oct, /*nb_oct*/
(unaryfunc)array_hex, /*nb_hex*/
+#endif
/*
* This code adds augmented assignment functionality
@@ -770,7 +787,10 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = {
(binaryfunc)array_inplace_add, /*inplace_add*/
(binaryfunc)array_inplace_subtract, /*inplace_subtract*/
(binaryfunc)array_inplace_multiply, /*inplace_multiply*/
+#if defined(NPY_PY3K)
+#else
(binaryfunc)array_inplace_divide, /*inplace_divide*/
+#endif
(binaryfunc)array_inplace_remainder, /*inplace_remainder*/
(ternaryfunc)array_inplace_power, /*inplace_power*/
(binaryfunc)array_inplace_left_shift, /*inplace_lshift*/
diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src
index c46cacee1..4f0b49787 100644
--- a/numpy/core/src/multiarray/scalartypes.c.src
+++ b/numpy/core/src/multiarray/scalartypes.c.src
@@ -212,7 +212,7 @@ gentype_generic_method(PyObject *self, PyObject *args, PyObject *kwds,
/**begin repeat
*
- * #name = add, subtract, divide, remainder, divmod, lshift, rshift,
+ * #name = add, subtract, remainder, divmod, lshift, rshift,
* and, xor, or, floor_divide, true_divide#
*/
static PyObject *
@@ -223,6 +223,18 @@ gentype_@name@(PyObject *m1, PyObject *m2)
/**end repeat**/
+#if !defined(NPY_PY3K)
+/**begin repeat
+ *
+ * #name = divide#
+ */
+static PyObject *
+gentype_@name@(PyObject *m1, PyObject *m2)
+{
+ return PyArray_Type.tp_as_number->nb_@name@(m1, m2);
+}
+/**end repeat**/
+#endif
static PyObject *
gentype_multiply(PyObject *m1, PyObject *m2)
@@ -259,7 +271,27 @@ gentype_multiply(PyObject *m1, PyObject *m2)
/**begin repeat
*
- * #name=positive, negative, absolute, invert, int, long, float, oct, hex#
+ * #name=positive, negative, absolute, invert, int, float#
+ */
+static PyObject *
+gentype_@name@(PyObject *m1)
+{
+ PyObject *arr, *ret;
+
+ arr = PyArray_FromScalar(m1, NULL);
+ if (arr == NULL) {
+ return NULL;
+ }
+ ret = Py_TYPE(arr)->tp_as_number->nb_@name@(arr);
+ Py_DECREF(arr);
+ return ret;
+}
+/**end repeat**/
+
+#if !defined(NPY_PY3K)
+/**begin repeat
+ *
+ * #name=long, oct, hex#
*/
static PyObject *
gentype_@name@(PyObject *m1)
@@ -275,6 +307,7 @@ gentype_@name@(PyObject *m1)
return ret;
}
/**end repeat**/
+#endif
static int
gentype_nonzero_number(PyObject *m1)
@@ -286,7 +319,11 @@ gentype_nonzero_number(PyObject *m1)
if (arr == NULL) {
return -1;
}
+#if defined(NPY_PY3K)
+ ret = Py_TYPE(arr)->tp_as_number->nb_bool(arr);
+#else
ret = Py_TYPE(arr)->tp_as_number->nb_nonzero(arr);
+#endif
Py_DECREF(arr);
return ret;
}
@@ -578,11 +615,11 @@ c@name@type_print(PyObject *v, FILE *fp, int flags)
/**begin repeat
*
- * #name = (int, long, hex, oct, float)*2#
- * #KIND = (Long*4, Float)*2#
- * #char = ,,,,,c*5#
- * #CHAR = ,,,,,C*5#
- * #POST = ,,,,,.real*5#
+ * #name = (int, float)*2#
+ * #KIND = (Long, Float)*2#
+ * #char = ,,c*2#
+ * #CHAR = ,,C*2#
+ * #POST = ,,.real*2#
*/
static PyObject *
@char@longdoubletype_@name@(PyObject *self)
@@ -598,12 +635,40 @@ static PyObject *
}
/**end repeat**/
+#if !defined(NPY_PY3K)
+
+/**begin repeat
+ *
+ * #name = (long, hex, oct)*2#
+ * #KIND = (Long*3)*2#
+ * #char = ,,,c*3#
+ * #CHAR = ,,,C*3#
+ * #POST = ,,,.real*3#
+ */
+static PyObject *
+@char@longdoubletype_@name@(PyObject *self)
+{
+ double dval;
+ PyObject *obj, *ret;
+
+ dval = (double)(((Py@CHAR@LongDoubleScalarObject *)self)->obval)@POST@;
+ obj = Py@KIND@_FromDouble(dval);
+ ret = Py_TYPE(obj)->tp_as_number->nb_@name@(obj);
+ Py_DECREF(obj);
+ return ret;
+}
+/**end repeat**/
+
+#endif /* !defined(NPY_PY3K) */
static PyNumberMethods gentype_as_number = {
(binaryfunc)gentype_add, /*nb_add*/
(binaryfunc)gentype_subtract, /*nb_subtract*/
(binaryfunc)gentype_multiply, /*nb_multiply*/
+#if defined(NPY_PY3K)
+#else
(binaryfunc)gentype_divide, /*nb_divide*/
+#endif
(binaryfunc)gentype_remainder, /*nb_remainder*/
(binaryfunc)gentype_divmod, /*nb_divmod*/
(ternaryfunc)gentype_power, /*nb_power*/
@@ -617,16 +682,29 @@ static PyNumberMethods gentype_as_number = {
(binaryfunc)gentype_and, /*nb_and*/
(binaryfunc)gentype_xor, /*nb_xor*/
(binaryfunc)gentype_or, /*nb_or*/
+#if defined(NPY_PY3K)
+#else
0, /*nb_coerce*/
+#endif
(unaryfunc)gentype_int, /*nb_int*/
+#if defined(NPY_PY3K)
+ 0, /*nb_reserved*/
+#else
(unaryfunc)gentype_long, /*nb_long*/
+#endif
(unaryfunc)gentype_float, /*nb_float*/
+#if defined(NPY_PY3K)
+#else
(unaryfunc)gentype_oct, /*nb_oct*/
(unaryfunc)gentype_hex, /*nb_hex*/
+#endif
0, /*inplace_add*/
0, /*inplace_subtract*/
0, /*inplace_multiply*/
+#if defined(NPY_PY3K)
+#else
0, /*inplace_divide*/
+#endif
0, /*inplace_remainder*/
0, /*inplace_power*/
0, /*inplace_lshift*/
@@ -2137,31 +2215,47 @@ NPY_NO_EXPORT PyNumberMethods bool_arrtype_as_number = {
0, /* nb_add */
0, /* nb_subtract */
0, /* nb_multiply */
+#if defined(NPY_PY3K)
+#else
0, /* nb_divide */
+#endif
0, /* nb_remainder */
0, /* nb_divmod */
0, /* nb_power */
0, /* nb_negative */
0, /* nb_positive */
0, /* nb_absolute */
- (inquiry)bool_arrtype_nonzero, /* nb_nonzero */
+ (inquiry)bool_arrtype_nonzero, /* nb_nonzero / nb_bool */
0, /* nb_invert */
0, /* nb_lshift */
0, /* nb_rshift */
(binaryfunc)bool_arrtype_and, /* nb_and */
(binaryfunc)bool_arrtype_xor, /* nb_xor */
(binaryfunc)bool_arrtype_or, /* nb_or */
+#if defined(NPY_PY3K)
+#else
0, /* nb_coerce */
+#endif
0, /* nb_int */
+#if defined(NPY_PY3K)
+ 0, /* nb_reserved */
+#else
0, /* nb_long */
+#endif
0, /* nb_float */
+#if defined(NPY_PY3K)
+#else
0, /* nb_oct */
0, /* nb_hex */
+#endif
/* Added in release 2.0 */
0, /* nb_inplace_add */
0, /* nb_inplace_subtract */
0, /* nb_inplace_multiply */
+#if defined(NPY_PY3K)
+#else
0, /* nb_inplace_divide */
+#endif
0, /* nb_inplace_remainder */
0, /* nb_inplace_power */
0, /* nb_inplace_lshift */
@@ -2200,7 +2294,11 @@ void_arrtype_new(PyTypeObject *type, PyObject *args, PyObject *NPY_UNUSED(kwds))
PyArray_IsScalar(obj, Integer) ||
(PyArray_Check(obj) && PyArray_NDIM(obj)==0 &&
PyArray_ISINTEGER(obj))) {
+#if defined(NPY_PY3K)
+ new = Py_TYPE(obj)->tp_as_number->nb_int(obj);
+#else
new = Py_TYPE(obj)->tp_as_number->nb_long(obj);
+#endif
}
if (new && PyLong_Check(new)) {
PyObject *ret;
@@ -3103,13 +3201,24 @@ initialize_numeric_types(void)
PyCLongDoubleArrType_Type.tp_as_number = &clongdoubletype_as_number;
/**begin repeat
- * #name = int, long, hex, oct, float, repr, str#
- * #kind = tp_as_number->nb*5, tp*2#
+ * #name = int, float, repr, str#
+ * #kind = tp_as_number->nb*2, tp*2#
*/
PyLongDoubleArrType_Type.@kind@_@name@ = longdoubletype_@name@;
PyCLongDoubleArrType_Type.@kind@_@name@ = clongdoubletype_@name@;
/**end repeat**/
+#if !defined(NPY_PY3K)
+ /**begin repeat
+ * #name = long, hex, oct#
+ * #kind = tp_as_number->nb*3#
+ */
+ PyLongDoubleArrType_Type.@kind@_@name@ = longdoubletype_@name@;
+ PyCLongDoubleArrType_Type.@kind@_@name@ = clongdoubletype_@name@;
+ /**end repeat**/
+
+#endif
+
PyStringArrType_Type.tp_itemsize = sizeof(char);
PyVoidArrType_Type.tp_dealloc = (destructor) void_dealloc;
diff --git a/numpy/core/src/scalarmathmodule.c.src b/numpy/core/src/scalarmathmodule.c.src
index f1f7d5ad2..5dc72a87a 100644
--- a/numpy/core/src/scalarmathmodule.c.src
+++ b/numpy/core/src/scalarmathmodule.c.src
@@ -11,6 +11,7 @@
#include "numpy/ufuncobject.h"
#include "numpy/arrayscalars.h"
+#include "npy_3kcompat.h"
/** numarray adapted routines.... **/
@@ -629,6 +630,9 @@ _@name@_convert2_to_ctypes(PyObject *a, @name@ *arg1,
/**end repeat**/
+#if defined(NPY_PY3K)
+#define CODEGEN_SKIP_divide_FLAG
+#endif
/**begin repeat
#name=(byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong)*13, (float, double, longdouble, cfloat, cdouble, clongdouble)*6, (float, double, longdouble)*2#
@@ -640,6 +644,8 @@ _@name@_convert2_to_ctypes(PyObject *a, @name@ *arg1,
#OName=(Byte, UByte, Short, UShort, Int, UInt, Long, ULong, LongLong, ULongLong)*12, Float*4, Double*6, (Float, Double, LongDouble, CFloat, CDouble, CLongDouble)*6, (Float, Double, LongDouble)*2#
**/
+#if !defined(CODEGEN_SKIP_@oper@_FLAG)
+
static PyObject *
@name@_@oper@(PyObject *a, PyObject *b)
{
@@ -740,8 +746,12 @@ static PyObject *
#endif
return ret;
}
+#endif
+
/**end repeat**/
+#undef CODEGEN_SKIP_divide_FLAG
+
/**begin repeat
#name=byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong,float, double, longdouble, cfloat, cdouble, clongdouble#
#Name=Byte, UByte, Short, UShort, Int, UInt, Long, ULong, LongLong, ULongLong, Float, Double, LongDouble, CFloat, CDouble, CLongDouble#
@@ -924,13 +934,19 @@ static PyObject *
#define @name@_invert NULL
/**end repeat**/
+#if defined(NPY_PY3K)
+#define NONZERO_NAME(prefix, suffix) prefix##bool##suffix
+#else
+#define NONZERO_NAME(prefix, suffix) prefix##nonzero##suffix
+#endif
+
/**begin repeat
* #name = byte, ubyte, short, ushort, int, uint, long, ulong, longlong,
* ulonglong, float, double, longdouble, cfloat, cdouble, clongdouble#
* #simp=1*13,0*3#
*/
static int
-@name@_nonzero(PyObject *a)
+NONZERO_NAME(@name@_,)(PyObject *a)
{
int ret;
@name@ arg1;
@@ -939,7 +955,7 @@ static int
if (PyErr_Occurred()) {
return -1;
}
- return PyGenericArrType_Type.tp_as_number->nb_nonzero(a);
+ return PyGenericArrType_Type.tp_as_number->NONZERO_NAME(nb_,)(a);
}
/*
@@ -1008,6 +1024,7 @@ static PyObject *
}
/**end repeat**/
+#if !defined(NPY_PY3K)
/**begin repeat
*
@@ -1026,6 +1043,7 @@ static PyObject *
}
/**end repeat**/
+#endif
/**begin repeat
* #oper=le,ge,lt,gt,eq,ne#
@@ -1099,30 +1117,50 @@ static PyNumberMethods @name@_as_number = {
(binaryfunc)@name@_add, /*nb_add*/
(binaryfunc)@name@_subtract, /*nb_subtract*/
(binaryfunc)@name@_multiply, /*nb_multiply*/
+#if defined(NPY_PY3K)
+#else
(binaryfunc)@name@_divide, /*nb_divide*/
+#endif
(binaryfunc)@name@_remainder, /*nb_remainder*/
(binaryfunc)@name@_divmod, /*nb_divmod*/
(ternaryfunc)@name@_power, /*nb_power*/
(unaryfunc)@name@_negative,
(unaryfunc)@name@_positive, /*nb_pos*/
(unaryfunc)@name@_absolute, /*nb_abs*/
+#if defined(NPY_PY3K)
+ (inquiry)@name@_bool, /*nb_bool*/
+#else
(inquiry)@name@_nonzero, /*nb_nonzero*/
+#endif
(unaryfunc)@name@_invert, /*nb_invert*/
(binaryfunc)@name@_lshift, /*nb_lshift*/
(binaryfunc)@name@_rshift, /*nb_rshift*/
(binaryfunc)@name@_and, /*nb_and*/
(binaryfunc)@name@_xor, /*nb_xor*/
(binaryfunc)@name@_or, /*nb_or*/
+#if defined(NPY_PY3K)
+#else
0, /*nb_coerce*/
+#endif
(unaryfunc)@name@_int, /*nb_int*/
+#if defined(NPY_PY3K)
+ (unaryfunc)0, /*nb_reserved*/
+#else
(unaryfunc)@name@_long, /*nb_long*/
+#endif
(unaryfunc)@name@_float, /*nb_float*/
+#if defined(NPY_PY3K)
+#else
(unaryfunc)@name@_oct, /*nb_oct*/
(unaryfunc)@name@_hex, /*nb_hex*/
+#endif
0, /*inplace_add*/
0, /*inplace_subtract*/
0, /*inplace_multiply*/
+#if defined(NPY_PY3K)
+#else
0, /*inplace_divide*/
+#endif
0, /*inplace_remainder*/
0, /*inplace_power*/
0, /*inplace_lshift*/