diff options
author | Ondřej Čertík <ondrej.certik@gmail.com> | 2012-12-26 09:32:17 -0800 |
---|---|---|
committer | Ondřej Čertík <ondrej.certik@gmail.com> | 2012-12-26 09:32:17 -0800 |
commit | 8093d5aaa64c51570bd89fb089fcc1505f6a404d (patch) | |
tree | eb2c6879d6c79ac089a83af1cb4a3659b3de1131 | |
parent | 9fba6c53301852ebf9d238752653a8b4e2247b26 (diff) | |
parent | 1f1537eff87721f9d3829513eadb1fe85fc1d866 (diff) | |
download | numpy-8093d5aaa64c51570bd89fb089fcc1505f6a404d.tar.gz |
Merge pull request #2853 from certik/forwardport2797
Forwardports #2797
-rw-r--r-- | numpy/core/src/multiarray/conversion_utils.c | 29 | ||||
-rw-r--r-- | numpy/core/tests/test_datetime.py | 11 | ||||
-rw-r--r-- | numpy/core/tests/test_dtype.py | 39 |
3 files changed, 66 insertions, 13 deletions
diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 914bc274d..b05314a0a 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -954,6 +954,9 @@ NPY_NO_EXPORT int PyArray_TypestrConvert(int itemsize, int gentype) { int newtype = NPY_NOTYPE; + PyArray_Descr *temp; + const char *msg = "Specified size is invalid for this data type.\n" + "Size will be ignored in NumPy 1.7 but may throw an exception in future versions."; switch (gentype) { case NPY_GENBOOLLTR: @@ -1106,6 +1109,32 @@ PyArray_TypestrConvert(int itemsize, int gentype) } break; } + + /* + * Raise deprecate warning if new type hasn't been + * set yet and size char is invalid. + * This should eventually be changed to an error in + * future NumPy versions. + */ + if (newtype == NPY_NOTYPE) { + temp = PyArray_DescrFromType(gentype); + if (temp != NULL) { + if (temp->elsize != itemsize) { + if (DEPRECATE(msg) < 0) { + Py_DECREF(temp); + return -1; + } + + newtype = gentype; + } + else { + newtype = gentype; + } + + Py_DECREF(temp); + } + } + return newtype; } diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py index fbde86b57..f4a1ee617 100644 --- a/numpy/core/tests/test_datetime.py +++ b/numpy/core/tests/test_datetime.py @@ -48,12 +48,11 @@ class TestDateTime(TestCase): assert_raises(TypeError, np.dtype, 'm8[badunit]') assert_raises(TypeError, np.dtype, 'M8[YY]') assert_raises(TypeError, np.dtype, 'm8[YY]') - assert_raises(TypeError, np.dtype, 'M4') - assert_raises(TypeError, np.dtype, 'm4') - assert_raises(TypeError, np.dtype, 'M7') - assert_raises(TypeError, np.dtype, 'm7') - assert_raises(TypeError, np.dtype, 'M16') - assert_raises(TypeError, np.dtype, 'm16') + assert_warns(DeprecationWarning, np.dtype, 'm4') + assert_warns(DeprecationWarning, np.dtype, 'M7') + assert_warns(DeprecationWarning, np.dtype, 'm7') + assert_warns(DeprecationWarning, np.dtype, 'M16') + assert_warns(DeprecationWarning, np.dtype, 'm16') def test_datetime_casting_rules(self): # Cannot cast safely/same_kind between timedelta and datetime diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 5645a0824..92e335165 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -47,13 +47,38 @@ class TestBuiltin(TestCase): self.assertTrue(hash(left) == hash(right)) def test_invalid_types(self): - # Make sure invalid type strings raise exceptions - for typestr in ['O3', 'O5', 'O7', 'b3', 'h4', 'I5', 'l4', 'l8', - 'L4', 'L8', 'q8', 'q16', 'Q8', 'Q16', 'e3', - 'f5', 'd8', 't8', 'g12', 'g16', - 'NA[u4,0xffffffff]']: - #print typestr - assert_raises(TypeError, np.dtype, typestr) + # Make sure invalid type strings raise a warning. + # For now, display a deprecation warning for invalid + # type sizes. In the future this should be changed + # to an exception. + + assert_warns(DeprecationWarning, np.dtype, 'O3') + assert_warns(DeprecationWarning, np.dtype, 'O5') + assert_warns(DeprecationWarning, np.dtype, 'O7') + assert_warns(DeprecationWarning, np.dtype, 'b3') + assert_warns(DeprecationWarning, np.dtype, 'h4') + assert_warns(DeprecationWarning, np.dtype, 'I5') + assert_warns(DeprecationWarning, np.dtype, 'e3') + assert_warns(DeprecationWarning, np.dtype, 'f5') + + if np.dtype('g').itemsize == 8 or np.dtype('g').itemsize == 16: + assert_warns(DeprecationWarning, np.dtype, 'g12') + elif np.dtype('g').itemsize == 12: + assert_warns(DeprecationWarning, np.dtype, 'g16') + + if np.dtype('l').itemsize == 8: + assert_warns(DeprecationWarning, np.dtype, 'l4') + assert_warns(DeprecationWarning, np.dtype, 'L4') + else: + assert_warns(DeprecationWarning, np.dtype, 'l8') + assert_warns(DeprecationWarning, np.dtype, 'L8') + + if np.dtype('q').itemsize == 8: + assert_warns(DeprecationWarning, np.dtype, 'q4') + assert_warns(DeprecationWarning, np.dtype, 'Q4') + else: + assert_warns(DeprecationWarning, np.dtype, 'q8') + assert_warns(DeprecationWarning, np.dtype, 'Q8') def test_bad_param(self): # Can't give a size that's too small |