8000 Throw errors when indexing into empty array_view objects by jkseppan · Pull Request #5247 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Throw errors when indexing into empty array_view objects #5247

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

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use null data instead of a separate flag
to mark empty array views
  • Loading branch information
jkseppan committed Oct 15, 2015
commit aa38c5b0e338a708db573e47632019bb0bfe3248
57 changes: 29 additions & 28 deletions src/numpy_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class array_view_accessors<AV, T, 1>
T &operator()(npy_intp i)
{
AVC *self = static_cast<AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -252,7 +252,7 @@ class array_view_accessors<AV, T, 1>
const T &operator()(npy_intp i) const
{
const AVC *self = static_cast<const AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -262,7 +262,7 @@ class array_view_accessors<AV, T, 1>
T &operator[](npy_intp i)
{
AVC *self = static_cast<AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -272,7 +272,7 @@ class array_view_accessors<AV, T, 1>
const T &operator[](npy_intp i) const
{
const AVC *self = static_cast<const AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -290,7 +290,7 @@ class array_view_accessors<AV, T, 2>
T &operator()(npy_intp i, npy_intp j)
{
AVC *self = static_cast<AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -301,7 +301,7 @@ class array_view_accessors<AV, T, 2>
const T &operator()(npy_intp i, npy_intp j) const
{
const AVC *self = static_cast<const AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -312,7 +312,7 @@ class array_view_accessors<AV, T, 2>
sub_t operator[](npy_intp i) const
{
const AVC *self = static_cast<const AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -333,7 +333,7 @@ class array_view_accessors<AV, T, 3>
T &operator()(npy_intp i, npy_intp j, npy_intp k)
{
AVC *self = static_cast<AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -344,7 +344,7 @@ class array_view_accessors<AV, T, 3>
const T &operator()(npy_intp i, npy_intp j, npy_intp k) const
{
const AVC *self = static_cast<const AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand All @@ -355,7 +355,7 @@ class array_view_accessors<AV, T, 3>
sub_t operator[](npy_intp i) const
{
const AVC *self = static_cast<const AVC *>(this);
if (unlikely(self->m_empty)) {
if (unlikely(self->m_data == NULL)) {
throw std::out_of_range("indexing into an array_view with no data");
}

Expand Down Expand Up @@ -386,9 +386,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
npy_intp *m_shape;
npy_intp *m_strides;
char *m_data;
// Flag for a limited kind of bounds checking,
// not the same as the empty() method which checks the outer dimension
bool m_empty;

public:
typedef T value_type;
Expand All @@ -401,7 +398,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
{
m_shape = zeros;
m_strides = zeros;
m_empty = true;
}

array_view(PyObject *arr, bool contiguous = false) : m_arr(NULL), m_data(NULL)
Expand All @@ -418,22 +414,26 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
m_data = other.m_data;
m_shape = other.m_shape;
m_strides = other.m_strides;
m_empty = other.m_empty;
}

array_view(PyArrayObject *arr, char *data, npy_intp *shape, npy_intp *strides)
{
m_arr = arr;
Py_XINCREF(arr);
m_data = data;
m_shape = shape;
m_strides = strides;
m_empty = (ND == 0);
bool empty = (ND == 0);
for (size_t i = 0; i < ND; i++) {
if (shape[i] == 0) {
m_empty = true;
empty = true;
}
}

m_arr = arr;
Py_XINCREF(arr);
if (empty) {
m_data = NULL;
} else {
m_data = data;
}
m_shape = shape;
m_strides = strides;
}

array_view(npy_intp shape[ND]) : m_arr(NULL), m_shape(NULL), m_strides(NULL), m_data(NULL)
Expand Down Expand Up @@ -464,7 +464,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
m_data = other.m_data;
m_shape = other.m_shape;
m_strides = other.m_strides;
m_empty = other.m_empty;
}
return *this;
}
Expand All @@ -479,7 +478,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
m_data = NULL;
m_shape = zeros;
m_strides = zeros;
m_empty = true;
} else {
if (contiguous) {
tmp = (PyArrayObject *)PyArray_ContiguousFromAny(arr, type_num_of<T>::value, 0, ND);
Expand All @@ -498,7 +496,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
m_strides = zeros;
if (PyArray_NDIM(tmp) == 0 && ND == 0) {
m_arr = tmp;
m_empty = true;
return 1;
}
}
Expand All @@ -516,13 +513,17 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
m_arr = tmp;
m_shape = PyArray_DIMS(m_arr);
m_strides = PyArray_STRIDES(m_arr);
m_data = (char *)PyArray_BYTES(tmp);
m_empty = (ND == 0);
bool empty = (ND == 0);
for (size_t i = 0; i < ND; i++) {
if (m_shape[i] == 0) {
m_empty = true;
empty = true;
}
}
if (empty) {
m_data = NULL;
} else {
m_data = (char *)PyArray_BYTES(tmp);
}
}

return 1;
Expand Down
0