summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2020-07-11 16:24:38 -0500
committerSebastian Berg <sebastian@sipsolutions.net>2020-11-12 12:13:04 -0600
commitf165623752a2de2013be627803fc19d734c2650a (patch)
tree172d94dc9dd6472a1ac24d911d8dd9d688f8dd86 /numpy
parent44d66b8a3fbc88998b98d7bffc20bc9fa89251a5 (diff)
downloadnumpy-f165623752a2de2013be627803fc19d734c2650a.tar.gz
MAINT: Remove subclass paths from scalar_value
If a user-dtype uses a subclass of an existing NumPy scalar (which is probably rare or non-existing), it cannot be flexible or object. Thus the alignment logic should always work (as it should work for our scalars as well), and it is unlikely that testing for subclasses is not just a waste of time.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/scalarapi.c81
1 files changed, 4 insertions, 77 deletions
diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c
index 0e93cbbe9..6d1ffe002 100644
--- a/numpy/core/src/multiarray/scalarapi.c
+++ b/numpy/core/src/multiarray/scalarapi.c
@@ -87,83 +87,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
@@ -176,8 +105,6 @@ scalar_value(PyObject *scalar, PyArray_Descr *descr)
memloc = ((memloc + align - 1)/align)*align;
}
return (void *)memloc;
-#undef _IFCASE
-#undef _CHK
}
/*NUMPY_API