summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2020-01-24 18:53:17 +0000
committerSebastian Berg <sebastian@sipsolutions.net>2020-01-24 10:53:17 -0800
commit5d9f15bd054f8c6b6bfdd1b452f0a0644e2e3d71 (patch)
tree772163253b347cbb3c3f2d0145b4beb94a701259
parentd5a444e9f263a1594643e64c37cb709074c59172 (diff)
downloadnumpy-5d9f15bd054f8c6b6bfdd1b452f0a0644e2e3d71.tar.gz
MAINT: Use the PyArrayScalar_VAL macro where possible (gh-15426)
If we have this macro, we may as well use it. This changes all occurences except the Void ones, since there other fields that don't have accessor macros are also used.
-rw-r--r--numpy/core/src/multiarray/_multiarray_tests.c.src8
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src6
-rw-r--r--numpy/core/src/multiarray/dragon4.c16
-rw-r--r--numpy/core/src/multiarray/scalartypes.c.src44
4 files changed, 37 insertions, 37 deletions
diff --git a/numpy/core/src/multiarray/_multiarray_tests.c.src b/numpy/core/src/multiarray/_multiarray_tests.c.src
index 7007dd204..ec2928c8f 100644
--- a/numpy/core/src/multiarray/_multiarray_tests.c.src
+++ b/numpy/core/src/multiarray/_multiarray_tests.c.src
@@ -1877,21 +1877,21 @@ PrintFloat_Printf_g(PyObject *obj, int precision)
char str[1024];
if (PyArray_IsScalar(obj, Half)) {
- npy_half x = ((PyHalfScalarObject *)obj)->obval;
+ npy_half x = PyArrayScalar_VAL(obj, Half);
PyOS_snprintf(str, sizeof(str), "%.*g", precision,
npy_half_to_double(x));
}
else if (PyArray_IsScalar(obj, Float)) {
- npy_float x = ((PyFloatScalarObject *)obj)->obval;
+ npy_float x = PyArrayScalar_VAL(obj, Float);
PyOS_snprintf(str, sizeof(str), "%.*g", precision, x);
}
else if (PyArray_IsScalar(obj, Double)) {
- npy_double x = ((PyDoubleScalarObject *)obj)->obval;
+ npy_double x = PyArrayScalar_VAL(obj, Double);
PyOS_snprintf(str, sizeof(str), "%.*g", precision, x);
/* would be better to use lg, but not available in C90 */
}
else if (PyArray_IsScalar(obj, LongDouble)) {
- npy_longdouble x = ((PyLongDoubleScalarObject *)obj)->obval;
+ npy_longdouble x = PyArrayScalar_VAL(obj, LongDouble);
PyOS_snprintf(str, sizeof(str), "%.*Lg", precision, x);
}
else{
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index 808cfaa14..61270dbef 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -210,7 +210,7 @@ static int
@type@ temp; /* ensures alignment */
if (PyArray_IsScalar(op, @kind@)) {
- temp = ((Py@kind@ScalarObject *)op)->obval;
+ temp = PyArrayScalar_VAL(op, @kind@);
}
else {
temp = (@type@)@func2@(op);
@@ -291,7 +291,7 @@ static int
}
if (PyArray_IsScalar(op, @kind@)){
- temp = ((Py@kind@ScalarObject *)op)->obval;
+ temp = PyArrayScalar_VAL(op, @kind@);
}
else {
if (op == Py_None) {
@@ -406,7 +406,7 @@ LONGDOUBLE_setitem(PyObject *op, void *ov, void *vap)
}
if (PyArray_IsScalar(op, LongDouble)) {
- temp = ((PyLongDoubleScalarObject *)op)->obval;
+ temp = PyArrayScalar_VAL(op, LongDouble);
}
else {
/* In case something funny happened in PyArray_IsScalar */
diff --git a/numpy/core/src/multiarray/dragon4.c b/numpy/core/src/multiarray/dragon4.c
index d14b8e638..282cdad28 100644
--- a/numpy/core/src/multiarray/dragon4.c
+++ b/numpy/core/src/multiarray/dragon4.c
@@ -3183,19 +3183,19 @@ Dragon4_Positional(PyObject *obj, DigitMode digit_mode, CutoffMode cutoff_mode,
opt.exp_digits = -1;
if (PyArray_IsScalar(obj, Half)) {
- npy_half x = ((PyHalfScalarObject *)obj)->obval;
+ npy_half x = PyArrayScalar_VAL(obj, Half);
return Dragon4_Positional_Half_opt(&x, &opt);
}
else if (PyArray_IsScalar(obj, Float)) {
- npy_float x = ((PyFloatScalarObject *)obj)->obval;
+ npy_float x = PyArrayScalar_VAL(obj, Float);
return Dragon4_Positional_Float_opt(&x, &opt);
}
else if (PyArray_IsScalar(obj, Double)) {
- npy_double x = ((PyDoubleScalarObject *)obj)->obval;
+ npy_double x = PyArrayScalar_VAL(obj, Double);
return Dragon4_Positional_Double_opt(&x, &opt);
}
else if (PyArray_IsScalar(obj, LongDouble)) {
- npy_longdouble x = ((PyLongDoubleScalarObject *)obj)->obval;
+ npy_longdouble x = PyArrayScalar_VAL(obj, LongDouble);
return Dragon4_Positional_LongDouble_opt(&x, &opt);
}
@@ -3224,19 +3224,19 @@ Dragon4_Scientific(PyObject *obj, DigitMode digit_mode, int precision,
opt.exp_digits = exp_digits;
if (PyArray_IsScalar(obj, Half)) {
- npy_half x = ((PyHalfScalarObject *)obj)->obval;
+ npy_half x = PyArrayScalar_VAL(obj, Half);
return Dragon4_Scientific_Half_opt(&x, &opt);
}
else if (PyArray_IsScalar(obj, Float)) {
- npy_float x = ((PyFloatScalarObject *)obj)->obval;
+ npy_float x = PyArrayScalar_VAL(obj, Float);
return Dragon4_Scientific_Float_opt(&x, &opt);
}
else if (PyArray_IsScalar(obj, Double)) {
- npy_double x = ((PyDoubleScalarObject *)obj)->obval;
+ npy_double x = PyArrayScalar_VAL(obj, Double);
return Dragon4_Scientific_Double_opt(&x, &opt);
}
else if (PyArray_IsScalar(obj, LongDouble)) {
- npy_longdouble x = ((PyLongDoubleScalarObject *)obj)->obval;
+ npy_longdouble x = PyArrayScalar_VAL(obj, LongDouble);
return Dragon4_Scientific_LongDouble_opt(&x, &opt);
}
diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src
index 383cef5bd..d23a8ea1a 100644
--- a/numpy/core/src/multiarray/scalartypes.c.src
+++ b/numpy/core/src/multiarray/scalartypes.c.src
@@ -318,7 +318,7 @@ gentype_format(PyObject *self, PyObject *args)
* because it throws away precision.
*/
if (Py_TYPE(self) == &PyBoolArrType_Type) {
- obj = PyBool_FromLong(((PyBoolScalarObject *)self)->obval);
+ obj = PyBool_FromLong(PyArrayScalar_VAL(self, Bool));
}
else if (PyArray_IsScalar(self, Integer)) {
obj = Py_TYPE(self)->tp_as_number->nb_int(self);
@@ -903,7 +903,7 @@ static PyObject *
static PyObject *
@name@type_@kind@(PyObject *self)
{
- return @name@type_@kind@_either(((Py@Name@ScalarObject *)self)->obval,
+ return @name@type_@kind@_either(PyArrayScalar_VAL(self, @Name@),
TrimMode_LeaveOneZero, TrimMode_DptZeros, 0);
}
@@ -911,7 +911,7 @@ static PyObject *
c@name@type_@kind@(PyObject *self)
{
PyObject *rstr, *istr, *ret;
- npy_c@name@ val = ((PyC@Name@ScalarObject *)self)->obval;
+ npy_c@name@ val = PyArrayScalar_VAL(self, C@Name@);
TrimMode trim = TrimMode_DptZeros;
if (npy_legacy_print_mode == 113) {
@@ -975,7 +975,7 @@ c@name@type_@kind@(PyObject *self)
static PyObject *
halftype_@kind@(PyObject *self)
{
- npy_half val = ((PyHalfScalarObject *)self)->obval;
+ npy_half val = PyArrayScalar_VAL(self, Half);
float floatval = npy_half_to_float(val);
float absval;
@@ -1318,7 +1318,7 @@ gentype_real_get(PyObject *self)
return ret;
}
else if (PyArray_IsScalar(self, Object)) {
- PyObject *obj = ((PyObjectScalarObject *)self)->obval;
+ PyObject *obj = PyArrayScalar_VAL(self, Object);
ret = PyObject_GetAttrString(obj, "real");
if (ret != NULL) {
return ret;
@@ -1343,7 +1343,7 @@ gentype_imag_get(PyObject *self)
ret = PyArray_Scalar(ptr + typecode->elsize, typecode, NULL);
}
else if (PyArray_IsScalar(self, Object)) {
- PyObject *obj = ((PyObjectScalarObject *)self)->obval;
+ PyObject *obj = PyArrayScalar_VAL(self, Object);
PyArray_Descr *newtype;
ret = PyObject_GetAttrString(obj, "imag");
if (ret == NULL) {
@@ -1760,7 +1760,7 @@ gentype_reduce(PyObject *self, PyObject *NPY_UNUSED(args))
PyTuple_SET_ITEM(ret, 0, obj);
obj = PyObject_GetAttrString((PyObject *)self, "dtype");
if (PyArray_IsScalar(self, Object)) {
- PyObject *val = ((PyObjectScalarObject *)self)->obval;
+ PyObject *val = PyArrayScalar_VAL(self, Object);
PyObject *tup = Py_BuildValue("NO", obj, val);
if (tup == NULL) {
return NULL;
@@ -2550,7 +2550,7 @@ void_dealloc(PyVoidScalarObject *v)
static void
object_arrtype_dealloc(PyObject *v)
{
- Py_XDECREF(((PyObjectScalarObject *)v)->obval);
+ Py_XDECREF(PyArrayScalar_VAL(v, Object));
Py_TYPE(v)->tp_free(v);
}
@@ -2635,7 +2635,7 @@ static PyObject *
Py_DECREF(typecode);
return NULL;
}
- memset(&((Py@Name@ScalarObject *)robj)->obval, 0, sizeof(npy_@name@));
+ memset(&PyArrayScalar_VAL(robj, @Name@), 0, sizeof(npy_@name@));
#elif @default@ == 1
robj = PyArray_Scalar(NULL, typecode, NULL);
#elif @default@ == 2
@@ -2992,7 +2992,7 @@ void_arrtype_new(PyTypeObject *type, PyObject *args, PyObject *NPY_UNUSED(kwds))
static npy_hash_t
@lname@_arrtype_hash(PyObject *obj)
{
- return (npy_hash_t)(((Py@name@ScalarObject *)obj)->obval);
+ return (npy_hash_t)(PyArrayScalar_VAL(obj, @name@));
}
/**end repeat**/
@@ -3003,7 +3003,7 @@ static npy_hash_t
static npy_hash_t
@lname@_arrtype_hash(PyObject *obj)
{
- npy_hash_t x = (npy_hash_t)(((Py@name@ScalarObject *)obj)->obval);
+ npy_hash_t x = (npy_hash_t)(PyArrayScalar_VAL(obj, @name@));
if (x == -1) {
x = -2;
}
@@ -3014,7 +3014,7 @@ static npy_hash_t
static npy_hash_t
ulong_arrtype_hash(PyObject *obj)
{
- PyObject * l = PyLong_FromUnsignedLong(((PyULongScalarObject*)obj)->obval);
+ PyObject * l = PyLong_FromUnsignedLong(PyArrayScalar_VAL(obj, ULong));
npy_hash_t x = PyObject_Hash(l);
Py_DECREF(l);
return x;
@@ -3023,7 +3023,7 @@ ulong_arrtype_hash(PyObject *obj)
static npy_hash_t
int_arrtype_hash(PyObject *obj)
{
- npy_hash_t x = (npy_hash_t)(((PyIntScalarObject *)obj)->obval);
+ npy_hash_t x = (npy_hash_t)(PyArrayScalar_VAL(obj, Int));
if (x == -1) {
x = -2;
}
@@ -3033,7 +3033,7 @@ int_arrtype_hash(PyObject *obj)
static npy_hash_t
long_arrtype_hash(PyObject *obj)
{
- PyObject * l = PyLong_FromLong(((PyLongScalarObject*)obj)->obval);
+ PyObject * l = PyLong_FromLong(PyArrayScalar_VAL(obj, Long));
npy_hash_t x = PyObject_Hash(l);
Py_DECREF(l);
return x;
@@ -3048,7 +3048,7 @@ static NPY_INLINE npy_hash_t
@char@longlong_arrtype_hash(PyObject *obj)
{
PyObject * l = PyLong_From@Word@LongLong(
- ((Py@Char@LongLongScalarObject*)obj)->obval);
+ PyArrayScalar_VAL(obj, @Char@LongLong));
npy_hash_t x = PyObject_Hash(l);
Py_DECREF(l);
return x;
@@ -3064,7 +3064,7 @@ static NPY_INLINE npy_hash_t
static npy_hash_t
@lname@_arrtype_hash(PyObject *obj)
{
- npy_hash_t x = (npy_hash_t)(((Py@name@ScalarObject *)obj)->obval);
+ npy_hash_t x = (npy_hash_t)(PyArrayScalar_VAL(obj, @name@));
if (x == -1) {
x = -2;
}
@@ -3075,7 +3075,7 @@ static npy_hash_t
@lname@_arrtype_hash(PyObject *obj)
{
npy_hash_t y;
- npy_longlong x = (((Py@name@ScalarObject *)obj)->obval);
+ npy_longlong x = (PyArrayScalar_VAL(obj, @name@));
if ((x <= LONG_MAX)) {
y = (npy_hash_t) x;
@@ -3108,7 +3108,7 @@ static npy_hash_t
static npy_hash_t
@lname@_arrtype_hash(PyObject *obj)
{
- return _Py_HashDouble((double) ((Py@name@ScalarObject *)obj)->obval);
+ return _Py_HashDouble((double) PyArrayScalar_VAL(obj, @name@));
}
/* borrowed from complex_hash */
@@ -3117,13 +3117,13 @@ c@lname@_arrtype_hash(PyObject *obj)
{
npy_hash_t hashreal, hashimag, combined;
hashreal = _Py_HashDouble((double)
- (((PyC@name@ScalarObject *)obj)->obval).real);
+ PyArrayScalar_VAL(obj, C@name@).real);
if (hashreal == -1) {
return -1;
}
hashimag = _Py_HashDouble((double)
- (((PyC@name@ScalarObject *)obj)->obval).imag);
+ PyArrayScalar_VAL(obj, C@name@).imag);
if (hashimag == -1) {
return -1;
}
@@ -3138,13 +3138,13 @@ c@lname@_arrtype_hash(PyObject *obj)
static npy_hash_t
half_arrtype_hash(PyObject *obj)
{
- return _Py_HashDouble(npy_half_to_double(((PyHalfScalarObject *)obj)->obval));
+ return _Py_HashDouble(npy_half_to_double(PyArrayScalar_VAL(obj, Half)));
}
static npy_hash_t
object_arrtype_hash(PyObject *obj)
{
- return PyObject_Hash(((PyObjectScalarObject *)obj)->obval);
+ return PyObject_Hash(PyArrayScalar_VAL(obj, Object));
}
/* we used to just hash the pointer */