Skip to content

Commit

Permalink
Refactor to cover more cases; modify unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvius authored and certik committed Dec 26, 2012
1 parent 86dbd45 commit 4237c6e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
42 changes: 24 additions & 18 deletions numpy/core/src/multiarray/conversion_utils.c
Expand Up @@ -955,6 +955,8 @@ 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,28 +1108,32 @@ PyArray_TypestrConvert(int itemsize, int gentype)
newtype = NPY_TIMEDELTA;
}
break;
default:
temp = PyArray_DescrFromType(gentype);
if (temp != NULL) {
if (temp->elsize != itemsize) {

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.";
if (DEPRECATE_FUTUREWARNING(msg) < 0) {
return -1;
}

newtype = gentype;
}
else {
newtype = gentype;
}

/*
* 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) {
return -1;
}

Py_DECREF(temp);
newtype = gentype;
}
break;
else {
newtype = gentype;
}

Py_DECREF(temp);
}
}

return newtype;
}

Expand Down
11 changes: 5 additions & 6 deletions numpy/core/tests/test_datetime.py
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
15 changes: 9 additions & 6 deletions numpy/core/tests/test_dtype.py
Expand Up @@ -47,13 +47,16 @@ 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]']:
# Make sure invalid type strings raise exceptions.
# For now, display a deprecation warning for invalid
# type sizes. In the future this should be changed
# to an exception.
for typestr in ['O3', 'O5', 'O7', 'b3', 'h4', 'I5', 'l4',
'L4', 'q16', 'Q16', 'e3', 'f5', 'g12']:
#print typestr
assert_raises(TypeError, np.dtype, typestr)
assert_warns(DeprecationWarning, np.dtype, typestr)

assert_raises(TypeError, np.dtype, 't8', 'NA[u4,0xffffffff]')

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

0 comments on commit 4237c6e

Please sign in to comment.