diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/getlimits.py | 12 | ||||
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 12 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 4 |
3 files changed, 23 insertions, 5 deletions
diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index f73c21f67..fcb73e8ba 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -262,11 +262,15 @@ def _get_machar(ftype): raise ValueError(repr(ftype)) # Detect known / suspected types key = ftype('-0.1').newbyteorder('<').tobytes() - ma_like = _KNOWN_TYPES.get(key) - # Could be 80 bit == 10 byte extended precision, where last bytes can be - # random garbage. Try comparing first 10 bytes to pattern. - if ma_like is None and ftype == ntypes.longdouble: + ma_like = None + if ftype == ntypes.longdouble: + # Could be 80 bit == 10 byte extended precision, where last bytes can + # be random garbage. + # Comparing first 10 bytes to pattern first to avoid branching on the + # random garbage. ma_like = _KNOWN_TYPES.get(key[:10]) + if ma_like is None: + ma_like = _KNOWN_TYPES.get(key) if ma_like is not None: return ma_like # Fall back to parameter discovery diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index a7c33d88f..67d57975b 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -885,7 +885,17 @@ _try_convert_from_inherit_tuple(PyArray_Descr *type, PyObject *newobj) new->metadata = conv->metadata; Py_XINCREF(new->metadata); } - new->flags = conv->flags; + /* + * Certain flags must be inherited from the fields. This is needed + * only for void dtypes (or subclasses of it such as a record dtype). + * For other dtypes, the field part will only be used for direct field + * access and thus flag inheritance should not be necessary. + * (We only allow object fields if the dtype is object as well.) + * This ensures copying over of the NPY_FROM_FIELDS "inherited" flags. + */ + if (new->type_num == NPY_VOID) { + new->flags = conv->flags; + } Py_DECREF(conv); return new; diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 0b921fcb7..170d20157 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -2325,6 +2325,10 @@ class TestRegression: # allowed as a special case due to existing use, see gh-2798 a = np.ones(1, dtype=('O', [('name', 'O')])) assert_equal(a[0], 1) + # In particular, the above union dtype (and union dtypes in general) + # should mainly behave like the main (object) dtype: + assert a[0] is a.item() + assert type(a[0]) is int def test_correct_hash_dict(self): # gh-8887 - __hash__ would be None despite tp_hash being set |