Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d288e39

Browse files
committed
Refactor to cover more cases; modify unit tests
1 parent 9340dd4 commit d288e39

3 files changed

Lines changed: 38 additions & 30 deletions

File tree

numpy/core/src/multiarray/conversion_utils.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ PyArray_TypestrConvert(int itemsize, int gentype)
955955
{
956956
int newtype = NPY_NOTYPE;
957957
PyArray_Descr *temp;
958+
const char *msg = "Specified size is invalid for this data type.\n"
959+
"Size will be ignored in NumPy 1.7 but may throw an exception in future versions.";
958960

959961
switch (gentype) {
960962
case NPY_GENBOOLLTR:
@@ -1106,28 +1108,32 @@ PyArray_TypestrConvert(int itemsize, int gentype)
11061108
newtype = NPY_TIMEDELTA;
11071109
}
11081110
break;
1109-
default:
1110-
temp = PyArray_DescrFromType(gentype);
1111-
if (temp != NULL) {
1112-
if (temp->elsize != itemsize) {
1113-
1114-
const char *msg = "Specified size is invalid for this data type.\n"
1115-
"Size will be ignored in NumPy 1.7 but may throw an exception in future versions.";
1116-
if (DEPRECATE_FUTUREWARNING(msg) < 0) {
1117-
return -1;
1118-
}
1119-
1120-
newtype = gentype;
1121-
}
1122-
else {
1123-
newtype = gentype;
1111+
}
1112+
1113+
/*
1114+
* Raise deprecate warning if new type hasn't been
1115+
* set yet and size char is invalid.
1116+
* This should eventually be changed to an error in
1117+
* future NumPy versions.
1118+
*/
1119+
if (newtype == NPY_NOTYPE) {
1120+
temp = PyArray_DescrFromType(gentype);
1121+
if (temp != NULL) {
1122+
if (temp->elsize != itemsize) {
1123+
if (DEPRECATE(msg) < 0) {
1124+
return -1;
11241125
}
11251126

1126-
Py_DECREF(temp);
1127+
newtype = gentype;
11271128
}
1128-
break;
1129+
else {
1130+
newtype = gentype;
1131+
}
1132+
1133+
Py_DECREF(temp);
1134+
}
11291135
}
1130-
1136+
11311137
return newtype;
11321138
}
11331139

numpy/core/tests/test_datetime.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ def test_datetime_dtype_creation(self):
4848
assert_raises(TypeError, np.dtype, 'm8[badunit]')
4949
assert_raises(TypeError, np.dtype, 'M8[YY]')
5050
assert_raises(TypeError, np.dtype, 'm8[YY]')
51-
assert_raises(TypeError, np.dtype, 'M4')
52-
assert_raises(TypeError, np.dtype, 'm4')
53-
assert_raises(TypeError, np.dtype, 'M7')
54-
assert_raises(TypeError, np.dtype, 'm7')
55-
assert_raises(TypeError, np.dtype, 'M16')
56-
assert_raises(TypeError, np.dtype, 'm16')
51+
assert_warns(DeprecationWarning, np.dtype, 'm4')
52+
assert_warns(DeprecationWarning, np.dtype, 'M7')
53+
assert_warns(DeprecationWarning, np.dtype, 'm7')
54+
assert_warns(DeprecationWarning, np.dtype, 'M16')
55+
assert_warns(DeprecationWarning, np.dtype, 'm16')
5756

5857
def test_datetime_casting_rules(self):
5958
# Cannot cast safely/same_kind between timedelta and datetime

numpy/core/tests/test_dtype.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ def test_equivalent_dtype_hashing(self):
4747
self.assertTrue(hash(left) == hash(right))
4848

4949
def test_invalid_types(self):
50-
# Make sure invalid type strings raise exceptions
51-
for typestr in ['O3', 'O5', 'O7', 'b3', 'h4', 'I5', 'l4', 'l8',
52-
'L4', 'L8', 'q8', 'q16', 'Q8', 'Q16', 'e3',
53-
'f5', 'd8', 't8', 'g12', 'g16',
54-
'NA[u4,0xffffffff]']:
50+
# Make sure invalid type strings raise exceptions.
51+
# For now, display a deprecation warning for invalid
52+
# type sizes. In the future this should be changed
53+
# to an exception.
54+
for typestr in ['O3', 'O5', 'O7', 'b3', 'h4', 'I5', 'l4',
55+
'L4', 'q16', 'Q16', 'e3', 'f5', 'g12']:
5556
#print typestr
56-
assert_raises(TypeError, np.dtype, typestr)
57+
assert_warns(DeprecationWarning, np.dtype, typestr)
58+
59+
assert_raises(TypeError, np.dtype, 't8', 'NA[u4,0xffffffff]')
5760

5861
def test_bad_param(self):
5962
# Can't give a size that's too small

0 commit comments

Comments
 (0)