8000 Merge pull request #2853 from certik/forwardport2797 · numpy/numpy@8093d5a · GitHub
[go: up one dir, main page]

Skip to content

Commit 8093d5a

Browse files
committed
Merge pull request #2853 from certik/forwardport2797
Forwardports #2797
2 parents 9fba6c5 + 1f1537e commit 8093d5a

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

numpy/core/src/multiarray/conversion_utils.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,9 @@ NPY_NO_EXPORT int
954954
PyArray_TypestrConvert(int itemsize, int gentype)
955955
{
956956
int newtype = NPY_NOTYPE;
957+
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.";
957960

958961
switch (gentype) {
959962
case NPY_GENBOOLLTR:
@@ -1106,6 +1109,32 @@ PyArray_TypestrConvert(int itemsize, int gentype)
11061109
}
11071110
break;
11081111
}
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+
Py_DECREF(temp);
1125+
return -1;
1126+
}
1127+
1128+
newtype = gentype;
1129+
}
1130+
else {
1131+
newtype = gentype;
1132+
}
1133+
1134+
Py_DECREF(temp);
1135+
}
1136+
}
1137+
11091138
return newtype;
11101139
}
11111140

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: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,38 @@ 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]']:
55-
#print typestr
56-
assert_raises(TypeError, np.dtype, typestr)
50+
# Make sure invalid type strings raise a warning.
51+
# For now, display a deprecation warning for invalid
52+
# type sizes. In the future this should be changed
53+
# to an exception.
54+
55+
assert_warns(DeprecationWarning, np.dtype, 'O3')
56+
assert_warns(DeprecationWarning, np.dtype, 'O5')
57+
assert_warns(DeprecationWarning, np.dtype, 'O7')
58+
assert_warns(DeprecationWarning, np.dtype, 'b3')
59+
assert_warns(DeprecationWarning, np.dtype, 'h4')
60+
assert_warns(DeprecationWarning, np.dtype, 'I5')
61+
assert_warns(DeprecationWarning, np.dtype, 'e3')
62+
assert_warns(DeprecationWarning, np.dtype, 'f5')
63+
64+
if np.dtype('g').itemsize == 8 or np.dtype('g').itemsize == 16:
65+
assert_warns(DeprecationWarning, np.dtype, 'g12')
66+
elif np.dtype('g').itemsize == 12:
67+
assert_warns(DeprecationWarning, np.dtype, 'g16')
68+
69+
if np.dtype('l').itemsize == 8:
70+
assert_warns(DeprecationWarning, np.dtype, 'l4')
71+
assert_warns(DeprecationWarning, np.dtype, 'L4')
72+
else:
73+
assert_warns(DeprecationWarning, np.dtype, 'l8')
74+
assert_warns(DeprecationWarning, np.dtype, 'L8')
75+
76+
if np.dtype('q').itemsize == 8:
77+
assert_warns(DeprecationWarning, np.dtype, 'q4')
78+
assert_warns(DeprecationWarning, np.dtype, 'Q4')
79+
else:
80+
assert_warns(DeprecationWarning, np.dtype, 'q8')
81+
assert_warns(DeprecationWarning, np.dtype, 'Q8')
5782

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

0 commit comments

Comments
 (0)
0