diff options
author | Matti Picus <matti.picus@gmail.com> | 2022-03-10 18:44:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-10 18:44:08 +0200 |
commit | 46a61b3aed3cf674586b456b05c6c9b7b9c5020d (patch) | |
tree | 795794f82a7aa959574ed133e6cf15761920b8ae | |
parent | 3135e1f2a3feb318295e780823d5436d1a9224f0 (diff) | |
parent | f165623752a2de2013be627803fc19d734c2650a (diff) | |
download | numpy-46a61b3aed3cf674586b456b05c6c9b7b9c5020d.tar.gz |
Merge pull request #16810 from seberg/simplify-scalar-value
MAINT: Remove subclass paths from scalar_value
-rw-r--r-- | numpy/core/src/multiarray/scalarapi.c | 81 |
1 files changed, 4 insertions, 77 deletions
diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c index 48d86caf1..85ef6d3eb 100644 --- a/numpy/core/src/multiarray/scalarapi.c +++ b/numpy/core/src/multiarray/scalarapi.c @@ -88,83 +88,12 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr) } /* - * Must be a user-defined type --- check to see which - * scalar it inherits from. + * Must be a user defined type with an associated (registered) dtype. + * Thus, it cannot be flexible (user dtypes cannot be), so we can (and + * pretty much have no choice but to) assume the below logic always works. + * I.e. this assumes that the logic would also works for most of our types. */ -#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)) { - _IFCASE(Byte); - _IFCASE(Short); - _IFCASE(Int); - _IFCASE(Long); - _IFCASE(LongLong); - _IFCASE(Timedelta); - } - else { - /* Unsigned Integer */ - _IFCASE(UByte); - _IFCASE(UShort); - _IFCASE(UInt); - _IFCASE(ULong); - _IFCASE(ULongLong); - } - } - else { - /* Inexact */ - if (_CHK(Floating)) { - _IFCASE(Half); - _IFCASE(Float); - _IFCASE(Double); - _IFCASE(LongDouble); - } - else { - /*ComplexFloating */ - _IFCASE(CFloat); - _IFCASE(CDouble); - _IFCASE(CLongDouble); - } - } - } - else if (_CHK(Bool)) { - return &PyArrayScalar_VAL(scalar, Bool); - } - else if (_CHK(Datetime)) { - return &PyArrayScalar_VAL(scalar, Datetime); - } - else if (_CHK(Flexible)) { - if (_CHK(String)) { - return (void *)PyBytes_AS_STRING(scalar); - } - if (_CHK(Unicode)) { - /* Treat this the same as the NPY_UNICODE base class */ - - /* lazy initialization, to reduce the memory used by string scalars */ - if (PyArrayScalar_VAL(scalar, Unicode) == NULL) { - Py_UCS4 *raw_data = PyUnicode_AsUCS4Copy(scalar); - if (raw_data == NULL) { - return NULL; - } - PyArrayScalar_VAL(scalar, Unicode) = raw_data; - return (void *)raw_data; - } - return PyArrayScalar_VAL(scalar, Unicode); - } - if (_CHK(Void)) { - /* Note: no & needed here, so can't use _IFCASE */ - return PyArrayScalar_VAL(scalar, Void); - } - } - else { - _IFCASE(Object); - } - - /* * Use the alignment flag to figure out where the data begins * after a PyObject_HEAD @@ -177,8 +106,6 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr) memloc = ((memloc + align - 1)/align)*align; } return (void *)memloc; -#undef _IFCASE -#undef _CHK } /*NUMPY_API |