8000 Forwardport2797 by certik · Pull Request #2853 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Forwardport2797 #2853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 26, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions numpy/core/src/multiarray/conversion_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}

Expand Down
11 changes: 5 additions & 6 deletions numpy/core/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ def test_datetime_dtype_creation(self):
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
Expand Down
39 changes: 32 additions & 7 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,38 @@ def test_equivalent_dtype_hashing(self):
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
Expand Down
0