Skip to content

Commit

Permalink
MAINT: clean up some macros in scalarapi.c
Browse files Browse the repository at this point in the history
Using a macro in an if without parens is just weird.

Also, simplies macros to use existing macros, and add comments to make clear why `Void` is special.
  • Loading branch information
eric-wieser committed Jan 22, 2020
1 parent e94cec8 commit b7068dd
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions numpy/core/src/multiarray/scalarapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr)
type_num = descr->type_num;
}
switch (type_num) {
#define CASE(ut,lt) case NPY_##ut: return &(((Py##lt##ScalarObject *)scalar)->obval)
#define CASE(ut,lt) case NPY_##ut: return &PyArrayScalar_VAL(scalar, lt)
CASE(BOOL, Bool);
CASE(BYTE, Byte);
CASE(UBYTE, UByte);
Expand Down Expand Up @@ -73,22 +73,22 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr)
case NPY_UNICODE:
return (void *)PyUnicode_AS_DATA(scalar);
case NPY_VOID:
return ((PyVoidScalarObject *)scalar)->obval;
/* Note: no & needed here, so can't use CASE */
return PyArrayScalar_VAL(scalar, Void);
}

/*
* Must be a user-defined type --- check to see which
* scalar it inherits from.
*/

#define _CHK(cls) (PyObject_IsInstance(scalar, \
(PyObject *)&Py##cls##ArrType_Type))
#define _OBJ(lt) &(((Py##lt##ScalarObject *)scalar)->obval)
#define _IFCASE(cls) if _CHK(cls) return _OBJ(cls)
#define _CHK(cls) PyObject_IsInstance(scalar, \
(PyObject *)&Py##cls##ArrType_Type)
#define _IFCASE(cls) if (_CHK(cls)) return &PyArrayScalar_VAL(scalar, cls)

if _CHK(Number) {
if _CHK(Integer) {
if _CHK(SignedInteger) {
if (_CHK(Number)) {
if (_CHK(Integer)) {
if (_CHK(SignedInteger)) {
_IFCASE(Byte);
_IFCASE(Short);
_IFCASE(Int);
Expand All @@ -107,7 +107,7 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr)
}
else {
/* Inexact */
if _CHK(Floating) {
if (_CHK(Floating)) {
_IFCASE(Half);
_IFCASE(Float);
_IFCASE(Double);
Expand All @@ -122,10 +122,10 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr)
}
}
else if (_CHK(Bool)) {
return _OBJ(Bool);
return &PyArrayScalar_VAL(scalar, Bool);
}
else if (_CHK(Datetime)) {
return _OBJ(Datetime);
return &PyArrayScalar_VAL(scalar, Datetime);
}
else if (_CHK(Flexible)) {
if (_CHK(String)) {
Expand All @@ -135,7 +135,8 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr)
return (void *)PyUnicode_AS_DATA(scalar);
}
if (_CHK(Void)) {
return ((PyVoidScalarObject *)scalar)->obval;
/* Note: no & needed here, so can't use _IFCASE */
return PyArrayScalar_VAL(scalar, Void);
}
}
else {
Expand All @@ -156,7 +157,6 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr)
}
return (void *)memloc;
#undef _IFCASE
#undef _OBJ
#undef _CHK
}

Expand Down

0 comments on commit b7068dd

Please sign in to comment.