From a48f4222e1efd2ccf0717bc04a1cb37d514882a9 Mon Sep 17 00:00:00 2001 From: Allan Haldane Date: Sun, 22 May 2016 18:05:22 -0400 Subject: [PATCH] BUG: Temporary fix for str(mvoid) for object field types Fixes #7493, using a temporary hack, to be properly fixed later (eg with #6053) Printing a Masked-Void instance broke if the instance has a field of Object dtype because assignment involving structured dtypes with objects doesn't work. Fix is to use dtype-transfer code which avoid the bug. --- numpy/ma/core.py | 9 ++++++++- numpy/ma/tests/test_core.py | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 447d58884090..18e902bffb4c 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -5954,7 +5954,14 @@ def __str__(self): return self._data.__str__() printopt = masked_print_option rdtype = _recursive_make_descr(self._data.dtype, "O") - res = np.array([self._data]).astype(rdtype) + + # temporary hack to fix gh-7493. A more permanent fix + # is proposed in gh-6053, after which the next two + # lines should be changed to + # res = np.array([self._data], dtype=rdtype) + res = np.empty(1, rdtype) + res[:1] = self._data + _recursive_printoption(res, self._mask, printopt) return str(res[0]) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index c7b8bb3a8d2e..95afe4ce9972 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -757,6 +757,10 @@ def test_mvoid_print(self): finally: masked_print_option.set_display(ini_display) + # also check if there are object datatypes (see gh-7493) + mx = array([(1,), (2,)], dtype=[('a', 'O')]) + assert_equal(str(mx[0]), "(1,)") + def test_mvoid_multidim_print(self): # regression test for gh-6019