diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 5cdd9a0fa0668..4f9d6b2649270 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -30,6 +30,7 @@ #include "paddle/fluid/imperative/tracer.h" #include "paddle/fluid/imperative/type_defs.h" #include "paddle/fluid/operators/ops_extra_info.h" +#include "paddle/fluid/pybind/eager.h" #include "paddle/fluid/pybind/imperative.h" #include "paddle/phi/common/complex.h" @@ -70,7 +71,8 @@ bool PyObject_CheckLongOrToLong(PyObject** obj) { if ((PyLong_Check(*obj) && !PyBool_Check(*obj)) || PyObject_IsInstance(*obj, (PyObject*)g_vartype_pytype) || // NOLINT PyObject_IsInstance(*obj, (PyObject*)g_varbase_pytype) || // NOLINT - PyObject_IsInstance(*obj, (PyObject*)p_tensor_type)) { // NOLINT + (PyObject_IsInstance(*obj, (PyObject*)p_tensor_type) && // NOLINT + (((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT return true; } diff --git a/python/paddle/fluid/tests/unittests/test_flip.py b/python/paddle/fluid/tests/unittests/test_flip.py index 1807199821eb7..2bcc8a3dd4fc2 100644 --- a/python/paddle/fluid/tests/unittests/test_flip.py +++ b/python/paddle/fluid/tests/unittests/test_flip.py @@ -198,6 +198,23 @@ def test_grad(self): self.func(p) +class TestFlipError(unittest.TestCase): + def test_axis(self): + paddle.enable_static() + + def test_axis_rank(): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.flip(input, axis=[[0]]) + + self.assertRaises(TypeError, test_axis_rank) + + def test_axis_rank2(): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.flip(input, axis=[[0, 0], [1, 1]]) + + self.assertRaises(TypeError, test_axis_rank2) + + if __name__ == "__main__": paddle.enable_static() unittest.main()