From 67f66f0904c4cf234da579f56964a873fdcd17f0 Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Fri, 8 May 2020 21:51:42 +0800 Subject: [PATCH] Fix get item out of range error (#24339) * raise index error when slice out of range; test=develop * add uni test; test=develop * fix format error; test=develop * add comment for py::index_error; test=develop * polish error message; test=develop * polish error message; test=develop --- paddle/fluid/pybind/imperative.cc | 11 +++++++++++ python/paddle/fluid/tests/unittests/test_var_base.py | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/paddle/fluid/pybind/imperative.cc b/paddle/fluid/pybind/imperative.cc index 3b4deb76c8..89b404672c 100644 --- a/paddle/fluid/pybind/imperative.cc +++ b/paddle/fluid/pybind/imperative.cc @@ -256,7 +256,18 @@ static void ParseIndexingSlice(framework::LoDTensor *tensor, PyObject *_index, if (PyNumber_Check(slice_item)) { // integer int start = static_cast(PyLong_AsLong(slice_item)); + auto s_t = start; start = start < 0 ? start + dim_len : start; + if (start >= dim_len) { + std::string str_error_message = + "The starting index " + std::to_string(s_t) + + " of slice is out of bounds in tensor " + std::to_string(dim) + + "-th axis, it shound be in the range of [" + + std::to_string(-dim_len) + ", " + std::to_string(dim_len) + ")"; + // py::index_error is corresponding to IndexError in Python + // Used to indicate out of bounds access in __getitem__, __setitem__ + throw py::index_error(str_error_message); + } slice_axes->push_back(dim); slice_starts->push_back(start); slice_ends->push_back(start + 1); diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index 4dd41a1eb7..617970182a 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -181,14 +181,25 @@ class TestVarBase(unittest.TestCase): self.assertTrue( np.array_equal(local_out[15], tensor_array[::-1, ::-1, ::-1])) + def _test_for_var(self): + np_value = np.random.random((30, 100, 100)).astype('float32') + w = fluid.dygraph.to_variable(np_value) + + for i, e in enumerate(w): + self.assertTrue(np.array_equal(e.numpy(), np_value[i])) + def test_slice(self): with fluid.dygraph.guard(): self._test_slice() + self._test_for_var() var = fluid.dygraph.to_variable(self.array) self.assertTrue(np.array_equal(var[1, :].numpy(), self.array[1, :])) self.assertTrue(np.array_equal(var[::-1].numpy(), self.array[::-1])) + with self.assertRaises(IndexError): + y = var[self.shape[0]] + def test_var_base_to_np(self): with fluid.dygraph.guard(): var = fluid.dygraph.to_variable(self.array) -- GitLab