Skip to content
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

BUG: ticket #2028: ... #356

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
10 changes: 9 additions & 1 deletion numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ _array_find_python_scalar_type(PyObject *op)
/* if integer can fit into a longlong then return that*/
if ((PyLong_AsLongLong(op) == -1) && PyErr_Occurred()) {
PyErr_Clear();
if((PyLong_AsUnsignedLongLong(op) == -1) && PyErr_Occurred()){
Copy link
Member

Choose a reason for hiding this comment

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

Getting close ;) These lines need an extra indent. Also, checking an unsigned integer for a negative value should raise a compile warning at minimum. I think the value checks need to be done away with in any case as the documentation doesn't indicate any special return value as a possible error code, but perhaps that is a defect of the documentation.

Copy link
Member

Choose a reason for hiding this comment

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

OK, I checked the [http://docs.python.org/c-api/long.html](Python 2.7) documentation and -1 indeed indicates a possible error, however the docs also show a cast for the unsigned types, i.e., (unsigned long long) -1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like i am missing what it is that makes these lines need more indents, can you explain?

Copy link
Member

Choose a reason for hiding this comment

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

The second if is part the body of the first if.

PyErr_Clear();
}
else {
return PyArray_DescrFromType(NPY_ULONGLONG);
}
return PyArray_DescrFromType(NPY_OBJECT);
}
return PyArray_DescrFromType(NPY_LONGLONG);
else {
return PyArray_DescrFromType(NPY_LONGLONG);
}
}
return NULL;
}
Copy link
Member

Choose a reason for hiding this comment

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

The indentation on this does not look quite right. Could you check alignment?

Otherwise, this seems like a useful patch.

Copy link
Member

Choose a reason for hiding this comment

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

They're tabs instead of spaces. Eric, you need to fix your editor.

Also, else like so:

}
else {

Expand Down
23 changes: 23 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,29 @@ def test_complex_warning(self):
finally:
warn_ctx.__exit__()

class TestMinScalarType(object):
def test_usigned_shortshort(self):
dt = np.min_scalar_type(2**8-1)
wanted = np.dtype('uint8')
assert_equal(wanted, dt)
def test_usigned_short(self):
dt = np.min_scalar_type(2**16-1)
wanted = np.dtype('uint16')
assert_equal(wanted, dt)
def test_usigned_int(self):
dt = np.min_scalar_type(2**32-1)
wanted = np.dtype('uint32')
assert_equal(wanted, dt)
def test_usigned_longlong(self):
dt = np.min_scalar_type(2**63-1)
wanted = np.dtype('uint64')
assert_equal(wanted, dt)
def test_object(self):
dt = np.min_scalar_type(2**64)
wanted = np.dtype('O')
assert_equal(wanted, dt)


if sys.version_info >= (2, 6):

if sys.version_info[:2] == (2, 6):
Expand Down