Skip to content

MAINT: Use the PyArrayScalar_VAL macro where possible #15426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions numpy/core/src/multiarray/_multiarray_tests.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions numpy/core/src/multiarray/arraytypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 */
Expand Down
16 changes: 8 additions & 8 deletions numpy/core/src/multiarray/dragon4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
44 changes: 22 additions & 22 deletions numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -903,15 +903,15 @@ 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);
}

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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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**/

Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
}
Expand All @@ -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 */
Expand Down