8000 Fix invalid typestring size by jayvius · Pull Request #2797 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Fix invalid typestring size #2797

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 21, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension 8000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor to cover more cases; modify unit tests
  • Loading branch information
jayvius committed Dec 21, 2012
commit d288e39ff895504649a990318dbb9496edcc794a
42 changes: 24 additions & 18 deletions numpy/core/src/multiarray/conversion_utils.c
Original file line number Diff line number Diff line change
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a Py_DECREF(temp) here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops...good catch :)

}

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
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
15 changes: 9 additions & 6 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is contradicted directly below... please keep comments consistent with each other and with code :-)

# 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