Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions paddle/fluid/pybind/eager_math_op_patch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2283,8 +2283,16 @@ static PyObject* tensor__eq__method(TensorObject* self,
other_tensor = paddle::empty({}, phi::DataType::FLOAT32, place);
InitTensorWithNumpyValue(numpy_value, place, &other_tensor);
} else {
paddle::experimental::Scalar value =
CastPyArg2Scalar(other_obj, "__eq__", 0);
paddle::experimental::Scalar value;

// return False if other_obj is unsupported type
try {
value = CastPyArg2Scalar(other_obj, "__eq__", 0);
} catch (const ::common::enforce::EnforceNotMet& e) {
Py_INCREF(Py_False);
return Py_False;
}

if (PyComplex_Check(other_obj)) {
eager_gil_scoped_release guard;
other_tensor =
Expand Down
17 changes: 16 additions & 1 deletion test/legacy_test/test_eager_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ def test_tensor_pin_memory_and_device(self):

with self.assertRaises(RuntimeError) as context:
paddle.tensor(
self.array, device="cpu", pin_memory=True # no support
self.array,
device="cpu",
pin_memory=True, # no support
)
self.assertIn(
"Pinning memory is not supported",
Expand Down Expand Up @@ -1529,6 +1531,19 @@ def test_tensor__format__(self):
paddle_scalar = paddle.uniform([], min=-100, max=100)
self.assertRaises(ValueError, paddle_scalar.__format__, "3d")

def test_tensor_eq_unsupported_type(self):
a = paddle.empty([2])

# Compare with None
self.assertFalse(a == None) # noqa: E711
self.assertTrue(a != None) # noqa: E711

# Compare with other obj
self.assertFalse(a == "a string")
self.assertTrue(a != "a string")
self.assertFalse(a == object())
self.assertTrue(a != object())


class TestEagerTensorSetitem(unittest.TestCase):
def func_setUp(self):
Expand Down
Loading