From eef2bdf8bcdcacc690078f44cdcb0b8e103983d3 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Mon, 9 Nov 2026 12:48:22 +0500 Subject: [PATCH 1/2] [mypyc] Add primitives for list and variable-length tuple equality Use specialized C functions for == and != when both operands are lists or both are variable-length tuples, instead of PyObject_RichCompare followed by a truth test of the result. Subclasses use the generic path. --- mypyc/doc/list_operations.rst | 1 + mypyc/doc/tuple_operations.rst | 1 + mypyc/irbuild/ll_builder.py | 24 ++++- mypyc/lib-rt/CPy.h | 2 + mypyc/lib-rt/list_ops.c | 49 +++++++++++ mypyc/lib-rt/tuple_ops.c | 35 ++++++++ mypyc/primitives/list_ops.py | 8 ++ mypyc/primitives/tuple_ops.py | 18 +++- mypyc/test-data/irbuild-lists.test | 26 ++++++ mypyc/test-data/irbuild-tuple.test | 26 ++++++ mypyc/test-data/run-lists.test | 136 +++++++++++++++++++++++++++++ mypyc/test-data/run-tuples.test | 122 ++++++++++++++++++++++++++ 12 files changed, 446 insertions(+), 2 deletions(-) diff --git a/mypyc/doc/list_operations.rst b/mypyc/doc/list_operations.rst index bb4681266cabf..f18ee5c2bf0ae 100644 --- a/mypyc/doc/list_operations.rst +++ b/mypyc/doc/list_operations.rst @@ -32,6 +32,7 @@ Operators * ``lst[n]`` (get item by integer index) * ``lst[n:m]``, ``lst[n:]``, ``lst[:m]``, ``lst[:]`` (slicing) +* ``lst1 == lst2``, ``lst1 != lst2`` * ``lst1 + lst2``, ``lst += iter`` * ``lst * n``, ``n * lst``, ``lst *= n`` * ``obj in lst`` diff --git a/mypyc/doc/tuple_operations.rst b/mypyc/doc/tuple_operations.rst index 4c9da9b894af6..02ad80c2f006f 100644 --- a/mypyc/doc/tuple_operations.rst +++ b/mypyc/doc/tuple_operations.rst @@ -21,6 +21,7 @@ Operators * ``tup[n]`` (integer index) * ``tup[n:m]``, ``tup[n:]``, ``tup[:m]`` (slicing) +* ``tup1 == tup2``, ``tup1 != tup2`` (variable-length tuples) * ``tup1 + tup2`` * ``tup * n``, ``n * tup`` diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 08bebbe8aab7c..8f89858a060bd 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -182,7 +182,13 @@ uint64_overflow, uint64_to_int_op, ) -from mypyc.primitives.list_ops import list_build_op, list_extend_op, list_items, new_list_op +from mypyc.primitives.list_ops import ( + list_build_op, + list_equal_op, + list_extend_op, + list_items, + new_list_op, +) from mypyc.primitives.misc_ops import ( bool_op, buf_init_item, @@ -217,6 +223,7 @@ new_tuple_op, new_tuple_with_length_op, sequence_tuple_op, + tuple_equal_op, ) from mypyc.rt_expandtype import expand_rtype from mypyc.rt_subtype import is_runtime_subtype @@ -2012,6 +2019,17 @@ def compare_bytes(self, lhs: Value, rhs: Value, op: str, line: int) -> Value: op_type = ComparisonOp.EQ if op == "==" else ComparisonOp.NEQ return self.add(ComparisonOp(compare_result, Integer(1, c_int_rprimitive), op_type, line)) + def compare_sequences( + self, equal_op: CFunctionDescription, lhs: Value, rhs: Value, op: str, line: int + ) -> Value: + """Compare two lists or two variable-length tuples for (in)equality. + + The equal_op returns 1 if equal, 0 if not equal and -1 on error. + """ + compare_result = self.call_c(equal_op, [lhs, rhs], line) + op_type = ComparisonOp.EQ if op == "==" else ComparisonOp.NEQ + return self.add(ComparisonOp(compare_result, Integer(1, c_int_rprimitive), op_type, line)) + def compare_tuples(self, lhs: Value, rhs: Value, op: str, line: int = -1) -> Value: """Compare two tuples item by item""" # type cast to pass mypy check @@ -3139,6 +3157,10 @@ def translate_eq_cmp(self, lreg: Value, rreg: Value, expr_op: str, line: int) -> return self.compare_strings(lreg, rreg, expr_op, line) if is_bytes_rprimitive(ltype) and is_bytes_rprimitive(rtype): return self.compare_bytes(lreg, rreg, expr_op, line) + if is_list_rprimitive(ltype) and is_list_rprimitive(rtype): + return self.compare_sequences(list_equal_op, lreg, rreg, expr_op, line) + if is_tuple_rprimitive(ltype) and is_tuple_rprimitive(rtype): + return self.compare_sequences(tuple_equal_op, lreg, rreg, expr_op, line) lopt = optional_value_type(ltype) ropt = optional_value_type(rtype) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index d4bd2d8938ce1..505704e927aad 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -1000,6 +1000,7 @@ CPyTagged CPyList_Count(PyObject *obj, PyObject *value); int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value); PyObject *CPyList_Extend(PyObject *o1, PyObject *o2); int CPyList_Remove(PyObject *list, PyObject *obj); +int CPyList_Equal(PyObject *left, PyObject *right); CPyTagged CPyList_Index(PyObject *list, PyObject *obj); PyObject *CPySequence_Sort(PyObject *seq); PyObject *CPySequence_Concat(PyObject *seq, PyObject *other); @@ -1134,6 +1135,7 @@ bool CPySet_Remove(PyObject *set, PyObject *key); PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end); PyObject *CPySequenceTuple_GetItem_(PyObject *tuple, CPyTagged index); +int CPyTuple_Equal(PyObject *left, PyObject *right); static inline PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index) { diff --git a/mypyc/lib-rt/list_ops.c b/mypyc/lib-rt/list_ops.c index 86caa34a3f5a5..bdb5559279f72 100644 --- a/mypyc/lib-rt/list_ops.c +++ b/mypyc/lib-rt/list_ops.c @@ -468,6 +468,55 @@ CPyTagged CPyList_Index(PyObject *list, PyObject *obj) { return retval; } +// Compare two lists for equality. Return 1 if equal, 0 if not equal, -1 on error. +// +// The caller must hold a critical section on both lists. +static int _CPyList_Equal(PyObject *left, PyObject *right) { + if (Py_SIZE(left) != Py_SIZE(right)) { + return 0; + } + Py_ssize_t i; + for (i = 0; i < Py_SIZE(left) && i < Py_SIZE(right); i++) { + PyObject *left_item = PyList_GET_ITEM(left, i); + PyObject *right_item = PyList_GET_ITEM(right, i); + if (left_item == right_item) { + continue; + } + Py_INCREF(left_item); + Py_INCREF(right_item); + int cmp = PyObject_RichCompareBool(left_item, right_item, Py_EQ); + Py_DECREF(left_item); + Py_DECREF(right_item); + if (cmp <= 0) { + return cmp; + } + } + // An item comparison may have changed the lengths + return Py_SIZE(left) == Py_SIZE(right); +} + +int CPyList_Equal(PyObject *left, PyObject *right) { + if (likely(PyList_CheckExact(left) && PyList_CheckExact(right))) { + if (left == right) { + // All items are identical, so they compare equal + return 1; + } + int result; + Py_BEGIN_CRITICAL_SECTION2(left, right); + result = _CPyList_Equal(left, right); + Py_END_CRITICAL_SECTION2(); + return result; + } + // Subclasses may override __eq__ + PyObject *res = PyObject_RichCompare(left, right, Py_EQ); + if (res == NULL) { + return -1; + } + int result = PyObject_IsTrue(res); + Py_DECREF(res); + return result; +} + PyObject *CPySequence_Sort(PyObject *seq) { PyObject *newlist = PySequence_List(seq); if (newlist == NULL) diff --git a/mypyc/lib-rt/tuple_ops.c b/mypyc/lib-rt/tuple_ops.c index 3d4442e61b1b0..0bddad28e8871 100644 --- a/mypyc/lib-rt/tuple_ops.c +++ b/mypyc/lib-rt/tuple_ops.c @@ -40,3 +40,38 @@ PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged en } return CPyObject_GetSlice(obj, start, end); } + +// Compare two tuples for equality. Return 1 if equal, 0 if not equal, -1 on error. +int CPyTuple_Equal(PyObject *left, PyObject *right) { + if (likely(PyTuple_CheckExact(left) && PyTuple_CheckExact(right))) { + if (left == right) { + // All items are identical, so they compare equal + return 1; + } + Py_ssize_t size = PyTuple_GET_SIZE(left); + if (size != PyTuple_GET_SIZE(right)) { + return 0; + } + Py_ssize_t i; + for (i = 0; i < size; i++) { + PyObject *left_item = PyTuple_GET_ITEM(left, i); + PyObject *right_item = PyTuple_GET_ITEM(right, i); + if (left_item == right_item) { + continue; + } + int cmp = PyObject_RichCompareBool(left_item, right_item, Py_EQ); + if (cmp <= 0) { + return cmp; + } + } + return 1; + } + // Subclasses may override __eq__ + PyObject *res = PyObject_RichCompare(left, right, Py_EQ); + if (res == NULL) { + return -1; + } + int result = PyObject_IsTrue(res); + Py_DECREF(res); + return result; +} diff --git a/mypyc/primitives/list_ops.py b/mypyc/primitives/list_ops.py index d141a55d057c0..7652506ee70f2 100644 --- a/mypyc/primitives/list_ops.py +++ b/mypyc/primitives/list_ops.py @@ -300,6 +300,14 @@ error_kind=ERR_NEG_INT, ) +# list == list (return -1/0/1) +list_equal_op = custom_op( + arg_types=[list_rprimitive, list_rprimitive], + return_type=c_int_rprimitive, + c_function_name="CPyList_Equal", + error_kind=ERR_NEG_INT, +) + # list.remove(obj) method_op( name="remove", diff --git a/mypyc/primitives/tuple_ops.py b/mypyc/primitives/tuple_ops.py index 09daf5396bf0c..78c977c408d38 100644 --- a/mypyc/primitives/tuple_ops.py +++ b/mypyc/primitives/tuple_ops.py @@ -9,6 +9,7 @@ from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER from mypyc.ir.rtypes import ( bit_rprimitive, + c_int_rprimitive, c_pyssize_t_rprimitive, int_rprimitive, list_rprimitive, @@ -16,7 +17,14 @@ tuple_rprimitive, void_rtype, ) -from mypyc.primitives.registry import binary_op, custom_op, function_op, load_address_op, method_op +from mypyc.primitives.registry import ( + ERR_NEG_INT, + binary_op, + custom_op, + function_op, + load_address_op, + method_op, +) # Get the 'builtins.tuple' type object. load_address_op(name="builtins.tuple", type=object_rprimitive, src="PyTuple_Type") @@ -100,6 +108,14 @@ error_kind=ERR_NEVER, ) +# tuple == tuple (return -1/0/1) +tuple_equal_op = custom_op( + arg_types=[tuple_rprimitive, tuple_rprimitive], + return_type=c_int_rprimitive, + c_function_name="CPyTuple_Equal", + error_kind=ERR_NEG_INT, +) + # tuple + tuple binary_op( name="+", diff --git a/mypyc/test-data/irbuild-lists.test b/mypyc/test-data/irbuild-lists.test index 9587ae1c41e6a..6e99075d742d4 100644 --- a/mypyc/test-data/irbuild-lists.test +++ b/mypyc/test-data/irbuild-lists.test @@ -361,6 +361,32 @@ L0: r3 = truncate r1: i32 to builtins.bool return r3 +[case testListEquality] +from typing import List +def eq(x: List[int], y: List[int]) -> bool: + return x == y +def neq(x: List[int], y: List[str]) -> bool: + return x != y +[out] +def eq(x, y): + x, y :: list + r0 :: i32 + r1, r2 :: bit +L0: + r0 = CPyList_Equal(x, y) + r1 = r0 >= 0 :: signed + r2 = r0 == 1 + return r2 +def neq(x, y): + x, y :: list + r0 :: i32 + r1, r2 :: bit +L0: + r0 = CPyList_Equal(x, y) + r1 = r0 >= 0 :: signed + r2 = r0 != 1 + return r2 + [case testListInsert] from typing import List def f(x: List[int], y: int) -> None: diff --git a/mypyc/test-data/irbuild-tuple.test b/mypyc/test-data/irbuild-tuple.test index 56a57b04a6b07..b86699b7448d9 100644 --- a/mypyc/test-data/irbuild-tuple.test +++ b/mypyc/test-data/irbuild-tuple.test @@ -1156,6 +1156,32 @@ L0: b = r0 return 1 +[case testVariableLengthTupleEquality] +from typing import Tuple +def eq(x: Tuple[int, ...], y: Tuple[int, ...]) -> bool: + return x == y +def neq(x: Tuple[int, ...], y: Tuple[str, ...]) -> bool: + return x != y +[out] +def eq(x, y): + x, y :: tuple + r0 :: i32 + r1, r2 :: bit +L0: + r0 = CPyTuple_Equal(x, y) + r1 = r0 >= 0 :: signed + r2 = r0 == 1 + return r2 +def neq(x, y): + x, y :: tuple + r0 :: i32 + r1, r2 :: bit +L0: + r0 = CPyTuple_Equal(x, y) + r1 = r0 >= 0 :: signed + r2 = r0 != 1 + return r2 + [case testTupleFloatElementComparison] def f(x: tuple[float], y: tuple[float]) -> bool: return x == y diff --git a/mypyc/test-data/run-lists.test b/mypyc/test-data/run-lists.test index 03bc24da0b586..12d36db32689a 100644 --- a/mypyc/test-data/run-lists.test +++ b/mypyc/test-data/run-lists.test @@ -851,3 +851,139 @@ class GoodList(list): return GoodList(list(self) * n + [4]) def copy(self) -> Any: return GoodList(list(self) + [5]) + +[case testListEquality] +from typing import Any, List +from testutil import assertRaises +from leqsubclass import ListSubclass, ListEqOverride + +def eq(x: List[Any], y: List[Any]) -> bool: + return x == y + +def ne(x: List[Any], y: List[Any]) -> bool: + return x != y + +class AlwaysEqual: + def __eq__(self, other: object) -> bool: + return True + +class NeverEqual: + def __eq__(self, other: object) -> bool: + return False + +class RaisesOnEq: + def __eq__(self, other: object) -> bool: + raise ValueError("eq") + +class FalsyEq: + def __eq__(self, other: object) -> Any: + return 0 + +class TruthyEq: + def __eq__(self, other: object) -> Any: + return "yes" + +def test_basic() -> None: + nested = [[1, 2], [3]] + values: List[List[Any]] = [ + [], + [1], + [1, 2], + [1, 2, 3], + [2, 1], + [1, 2, 4], + ["a", "b"], + [1.0, 2.0], + [True, 2], + [[1, 2], [3]], + nested, + [[1, 2], [4]], + [None], + [(1, 2)], + [(1, 2), (3,)], + ] + for x in values: + for y in values: + a: Any = x + b: Any = y + assert eq(x, y) == (a == b), (x, y) + assert ne(x, y) == (a != b), (x, y) + assert eq(x, list(y)) == (a == b), (x, y) + assert ne(x, list(y)) == (a != b), (x, y) + +def test_identity() -> None: + nan: Any = float("nan") + x = [nan] + assert eq(x, x) + assert not ne(x, x) + assert eq(x, [nan]) + assert not eq(x, [float("nan")]) + assert ne(x, [float("nan")]) + y = [NeverEqual()] + assert eq(y, y) + assert not eq(y, [NeverEqual()]) + +def test_custom_eq() -> None: + assert eq([AlwaysEqual()], [1]) + assert eq([1], [AlwaysEqual()]) + assert not eq([AlwaysEqual()], [1, 2]) + assert ne([NeverEqual()], [NeverEqual()]) + assert not eq([FalsyEq()], [1]) + assert ne([FalsyEq()], [1]) + assert eq([TruthyEq()], [1]) + assert not ne([TruthyEq()], [1]) + +def test_error() -> None: + with assertRaises(ValueError, "eq"): + eq([RaisesOnEq()], [1]) + with assertRaises(ValueError, "eq"): + ne([1, RaisesOnEq()], [1, 2]) + # Lengths differ, so items aren't compared + assert not eq([RaisesOnEq()], [1, 2]) + # Items are compared in order, and the first mismatch stops the comparison + assert not eq([1, RaisesOnEq()], [2, 3]) + +class ClearOnEq: + def __init__(self, target: List[Any]) -> None: + self.target = target + + def __eq__(self, other: object) -> bool: + self.target.clear() + return True + +def test_mutation_during_comparison() -> None: + a: List[Any] = [] + a.extend([ClearOnEq(a), 1, 2]) + assert not eq(a, [0, 1, 2]) + assert a == [] + a.append(ClearOnEq(a)) + assert not eq(a, [0]) + b: List[Any] = [0, 1] + b.append(ClearOnEq(b)) + assert not eq([0, 1, 2], b) + assert b == [] + +def test_subclass() -> None: + assert eq(ListSubclass([1, 2]), [1, 2]) + assert eq([1, 2], ListSubclass([1, 2])) + assert ne(ListSubclass([1, 2]), [1]) + assert not eq(ListEqOverride([1]), [1]) + assert not eq([1], ListEqOverride([1])) + assert ne(ListEqOverride([1]), [1]) + assert ne([1], ListEqOverride([1])) + x = ListEqOverride([]) + assert not eq(x, x) + assert ne(x, x) + +[file leqsubclass.py] +from typing import Any + +class ListSubclass(list[Any]): + pass + +class ListEqOverride(list[Any]): + def __eq__(self, other: object) -> bool: + return False + + def __ne__(self, other: object) -> bool: + return True diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index a04b89a5070d3..31e745d0aef2d 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -517,3 +517,125 @@ class GoodTuple(tuple): return GoodTuple(tuple(self) * n + (2,)) def __rmul__(self, n: Any) -> Any: return GoodTuple(tuple(self) * n + (3,)) + +[case testVariableLengthTupleEquality] +from typing import Any, Tuple +from testutil import assertRaises +from teqsubclass import TupleSubclass, TupleEqOverride, NT + +def eq(x: Tuple[Any, ...], y: Tuple[Any, ...]) -> bool: + return x == y + +def ne(x: Tuple[Any, ...], y: Tuple[Any, ...]) -> bool: + return x != y + +class AlwaysEqual: + def __eq__(self, other: object) -> bool: + return True + +class NeverEqual: + def __eq__(self, other: object) -> bool: + return False + +class RaisesOnEq: + def __eq__(self, other: object) -> bool: + raise ValueError("eq") + +class FalsyEq: + def __eq__(self, other: object) -> Any: + return 0 + +class TruthyEq: + def __eq__(self, other: object) -> Any: + return "yes" + +def test_basic() -> None: + nested = ((1, 2), (3,)) + values: list[Tuple[Any, ...]] = [ + (), + (1,), + (1, 2), + (1, 2, 3), + (2, 1), + (1, 2, 4), + ("a", "b"), + (1.0, 2.0), + (True, 2), + ((1, 2), (3,)), + nested, + ((1, 2), (4,)), + (None,), + ([1, 2],), + ([1, 2], [3]), + ] + for x in values: + for y in values: + a: Any = x + b: Any = y + assert eq(x, y) == (a == b), (x, y) + assert ne(x, y) == (a != b), (x, y) + assert eq(x, tuple(list(y))) == (a == b), (x, y) + assert ne(x, tuple(list(y))) == (a != b), (x, y) + +def test_identity() -> None: + nan: Any = float("nan") + x = (nan,) + assert eq(x, x) + assert not ne(x, x) + assert eq(x, (nan,)) + assert not eq(x, (float("nan"),)) + assert ne(x, (float("nan"),)) + y = (NeverEqual(),) + assert eq(y, y) + assert not eq(y, (NeverEqual(),)) + +def test_custom_eq() -> None: + assert eq((AlwaysEqual(),), (1,)) + assert eq((1,), (AlwaysEqual(),)) + assert not eq((AlwaysEqual(),), (1, 2)) + assert ne((NeverEqual(),), (NeverEqual(),)) + assert not eq((FalsyEq(),), (1,)) + assert ne((FalsyEq(),), (1,)) + assert eq((TruthyEq(),), (1,)) + assert not ne((TruthyEq(),), (1,)) + +def test_error() -> None: + with assertRaises(ValueError, "eq"): + eq((RaisesOnEq(),), (1,)) + with assertRaises(ValueError, "eq"): + ne((1, RaisesOnEq()), (1, 2)) + # Lengths differ, so items aren't compared + assert not eq((RaisesOnEq(),), (1, 2)) + # Items are compared in order, and the first mismatch stops the comparison + assert not eq((1, RaisesOnEq()), (2, 3)) + +def test_subclass() -> None: + assert eq(TupleSubclass((1, 2)), (1, 2)) + assert eq((1, 2), TupleSubclass((1, 2))) + assert ne(TupleSubclass((1, 2)), (1,)) + assert eq(NT(1, 2), (1, 2)) + assert ne(NT(1, 2), (2, 1)) + assert not eq(TupleEqOverride((1,)), (1,)) + assert not eq((1,), TupleEqOverride((1,))) + assert ne(TupleEqOverride((1,)), (1,)) + assert ne((1,), TupleEqOverride((1,))) + x = TupleEqOverride(()) + assert not eq(x, x) + assert ne(x, x) + +[file teqsubclass.py] +from typing import Any, NamedTuple + +class TupleSubclass(tuple[Any, ...]): + pass + +class TupleEqOverride(tuple[Any, ...]): + def __eq__(self, other: object) -> bool: + return False + + def __ne__(self, other: object) -> bool: + return True + +class NT(NamedTuple): + x: int + y: int From 25d80a4813d8a4862ea476727128df581471dd54 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Mon, 9 Nov 2026 14:13:26 +0500 Subject: [PATCH 2/2] Match CPython for tuple equality with different lengths and subclass != Tuple equality now compares items even if the lengths differ, like CPython, since item comparisons can have side effects or raise exceptions. Pass the comparison operator to the C functions, so that != on list and tuple subclasses calls __ne__ instead of negating the result of __eq__. --- mypyc/irbuild/ll_builder.py | 30 ++++++++++++---- mypyc/lib-rt/CPy.h | 4 +-- mypyc/lib-rt/list_ops.c | 24 ++++++++----- mypyc/lib-rt/tuple_ops.c | 56 ++++++++++++++++++------------ mypyc/primitives/list_ops.py | 12 ++++++- mypyc/primitives/tuple_ops.py | 12 ++++++- mypyc/test-data/irbuild-lists.test | 6 ++-- mypyc/test-data/irbuild-tuple.test | 6 ++-- mypyc/test-data/run-lists.test | 22 +++++++++++- mypyc/test-data/run-tuples.test | 49 ++++++++++++++++++++++++-- 10 files changed, 168 insertions(+), 53 deletions(-) diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 8f89858a060bd..6f79ab36a7e0b 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -187,6 +187,7 @@ list_equal_op, list_extend_op, list_items, + list_not_equal_op, new_list_op, ) from mypyc.primitives.misc_ops import ( @@ -224,6 +225,7 @@ new_tuple_with_length_op, sequence_tuple_op, tuple_equal_op, + tuple_not_equal_op, ) from mypyc.rt_expandtype import expand_rtype from mypyc.rt_subtype import is_runtime_subtype @@ -2020,15 +2022,25 @@ def compare_bytes(self, lhs: Value, rhs: Value, op: str, line: int) -> Value: return self.add(ComparisonOp(compare_result, Integer(1, c_int_rprimitive), op_type, line)) def compare_sequences( - self, equal_op: CFunctionDescription, lhs: Value, rhs: Value, op: str, line: int + self, + equal_op: CFunctionDescription, + not_equal_op: CFunctionDescription, + lhs: Value, + rhs: Value, + op: str, + line: int, ) -> Value: """Compare two lists or two variable-length tuples for (in)equality. - The equal_op returns 1 if equal, 0 if not equal and -1 on error. + The ops return 1 if the comparison is true, 0 if it's false and -1 on error. + Use a separate op for != instead of negating ==, since a subclass may + override __ne__ (or inherit it while overriding only __eq__). """ - compare_result = self.call_c(equal_op, [lhs, rhs], line) - op_type = ComparisonOp.EQ if op == "==" else ComparisonOp.NEQ - return self.add(ComparisonOp(compare_result, Integer(1, c_int_rprimitive), op_type, line)) + compare_op = equal_op if op == "==" else not_equal_op + compare_result = self.call_c(compare_op, [lhs, rhs], line) + return self.add( + ComparisonOp(compare_result, Integer(1, c_int_rprimitive), ComparisonOp.EQ, line) + ) def compare_tuples(self, lhs: Value, rhs: Value, op: str, line: int = -1) -> Value: """Compare two tuples item by item""" @@ -3158,9 +3170,13 @@ def translate_eq_cmp(self, lreg: Value, rreg: Value, expr_op: str, line: int) -> if is_bytes_rprimitive(ltype) and is_bytes_rprimitive(rtype): return self.compare_bytes(lreg, rreg, expr_op, line) if is_list_rprimitive(ltype) and is_list_rprimitive(rtype): - return self.compare_sequences(list_equal_op, lreg, rreg, expr_op, line) + return self.compare_sequences( + list_equal_op, list_not_equal_op, lreg, rreg, expr_op, line + ) if is_tuple_rprimitive(ltype) and is_tuple_rprimitive(rtype): - return self.compare_sequences(tuple_equal_op, lreg, rreg, expr_op, line) + return self.compare_sequences( + tuple_equal_op, tuple_not_equal_op, lreg, rreg, expr_op, line + ) lopt = optional_value_type(ltype) ropt = optional_value_type(rtype) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 505704e927aad..445d41aa96546 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -1000,7 +1000,7 @@ CPyTagged CPyList_Count(PyObject *obj, PyObject *value); int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value); PyObject *CPyList_Extend(PyObject *o1, PyObject *o2); int CPyList_Remove(PyObject *list, PyObject *obj); -int CPyList_Equal(PyObject *left, PyObject *right); +int CPyList_Compare(PyObject *left, PyObject *right, int op); CPyTagged CPyList_Index(PyObject *list, PyObject *obj); PyObject *CPySequence_Sort(PyObject *seq); PyObject *CPySequence_Concat(PyObject *seq, PyObject *other); @@ -1135,7 +1135,7 @@ bool CPySet_Remove(PyObject *set, PyObject *key); PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end); PyObject *CPySequenceTuple_GetItem_(PyObject *tuple, CPyTagged index); -int CPyTuple_Equal(PyObject *left, PyObject *right); +int CPyTuple_Compare(PyObject *left, PyObject *right, int op); static inline PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index) { diff --git a/mypyc/lib-rt/list_ops.c b/mypyc/lib-rt/list_ops.c index bdb5559279f72..69205e811d70b 100644 --- a/mypyc/lib-rt/list_ops.c +++ b/mypyc/lib-rt/list_ops.c @@ -495,20 +495,26 @@ static int _CPyList_Equal(PyObject *left, PyObject *right) { return Py_SIZE(left) == Py_SIZE(right); } -int CPyList_Equal(PyObject *left, PyObject *right) { +// Compare two lists using == or != (op is Py_EQ or Py_NE). Return 1 if the result is +// true, 0 if it's false, and -1 on error. +int CPyList_Compare(PyObject *left, PyObject *right, int op) { if (likely(PyList_CheckExact(left) && PyList_CheckExact(right))) { + int result; if (left == right) { // All items are identical, so they compare equal - return 1; + result = 1; + } else { + Py_BEGIN_CRITICAL_SECTION2(left, right); + result = _CPyList_Equal(left, right); + Py_END_CRITICAL_SECTION2(); + if (result < 0) { + return -1; + } } - int result; - Py_BEGIN_CRITICAL_SECTION2(left, right); - result = _CPyList_Equal(left, right); - Py_END_CRITICAL_SECTION2(); - return result; + return op == Py_EQ ? result : !result; } - // Subclasses may override __eq__ - PyObject *res = PyObject_RichCompare(left, right, Py_EQ); + // Subclasses may override __eq__ and __ne__ + PyObject *res = PyObject_RichCompare(left, right, op); if (res == NULL) { return -1; } diff --git a/mypyc/lib-rt/tuple_ops.c b/mypyc/lib-rt/tuple_ops.c index 0bddad28e8871..9c7d4b139d46a 100644 --- a/mypyc/lib-rt/tuple_ops.c +++ b/mypyc/lib-rt/tuple_ops.c @@ -41,33 +41,43 @@ PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged en return CPyObject_GetSlice(obj, start, end); } -// Compare two tuples for equality. Return 1 if equal, 0 if not equal, -1 on error. -int CPyTuple_Equal(PyObject *left, PyObject *right) { - if (likely(PyTuple_CheckExact(left) && PyTuple_CheckExact(right))) { - if (left == right) { - // All items are identical, so they compare equal - return 1; +// Compare two exact tuples for equality. Return 1 if equal, 0 if not equal, -1 on error. +static int _CPyTuple_Equal(PyObject *left, PyObject *right) { + if (left == right) { + // All items are identical, so they compare equal + return 1; + } + Py_ssize_t left_size = PyTuple_GET_SIZE(left); + Py_ssize_t right_size = PyTuple_GET_SIZE(right); + // Like CPython, compare items even if the lengths differ, since item + // comparisons can have side effects or raise exceptions + Py_ssize_t i; + for (i = 0; i < left_size && i < right_size; i++) { + PyObject *left_item = PyTuple_GET_ITEM(left, i); + PyObject *right_item = PyTuple_GET_ITEM(right, i); + if (left_item == right_item) { + continue; } - Py_ssize_t size = PyTuple_GET_SIZE(left); - if (size != PyTuple_GET_SIZE(right)) { - return 0; + int cmp = PyObject_RichCompareBool(left_item, right_item, Py_EQ); + if (cmp <= 0) { + return cmp; } - Py_ssize_t i; - for (i = 0; i < size; i++) { - PyObject *left_item = PyTuple_GET_ITEM(left, i); - PyObject *right_item = PyTuple_GET_ITEM(right, i); - if (left_item == right_item) { - continue; - } - int cmp = PyObject_RichCompareBool(left_item, right_item, Py_EQ); - if (cmp <= 0) { - return cmp; - } + } + return left_size == right_size; +} + +// Compare two tuples using == or != (op is Py_EQ or Py_NE). Return 1 if the result is +// true, 0 if it's false, and -1 on error. +int CPyTuple_Compare(PyObject *left, PyObject *right, int op) { + if (likely(PyTuple_CheckExact(left) && PyTuple_CheckExact(right))) { + int result = _CPyTuple_Equal(left, right); + if (result < 0) { + return -1; } - return 1; + return op == Py_EQ ? result : !result; } - // Subclasses may override __eq__ - PyObject *res = PyObject_RichCompare(left, right, Py_EQ); + // Subclasses may override __eq__ and __ne__ + PyObject *res = PyObject_RichCompare(left, right, op); if (res == NULL) { return -1; } diff --git a/mypyc/primitives/list_ops.py b/mypyc/primitives/list_ops.py index 7652506ee70f2..569e0ee6d2894 100644 --- a/mypyc/primitives/list_ops.py +++ b/mypyc/primitives/list_ops.py @@ -304,8 +304,18 @@ list_equal_op = custom_op( arg_types=[list_rprimitive, list_rprimitive], return_type=c_int_rprimitive, - c_function_name="CPyList_Equal", + c_function_name="CPyList_Compare", error_kind=ERR_NEG_INT, + extra_int_constants=[(2, c_int_rprimitive)], # Py_EQ +) + +# list != list (return -1/0/1) +list_not_equal_op = custom_op( + arg_types=[list_rprimitive, list_rprimitive], + return_type=c_int_rprimitive, + c_function_name="CPyList_Compare", + error_kind=ERR_NEG_INT, + extra_int_constants=[(3, c_int_rprimitive)], # Py_NE ) # list.remove(obj) diff --git a/mypyc/primitives/tuple_ops.py b/mypyc/primitives/tuple_ops.py index 78c977c408d38..38ef98397d4eb 100644 --- a/mypyc/primitives/tuple_ops.py +++ b/mypyc/primitives/tuple_ops.py @@ -112,8 +112,18 @@ tuple_equal_op = custom_op( arg_types=[tuple_rprimitive, tuple_rprimitive], return_type=c_int_rprimitive, - c_function_name="CPyTuple_Equal", + c_function_name="CPyTuple_Compare", error_kind=ERR_NEG_INT, + extra_int_constants=[(2, c_int_rprimitive)], # Py_EQ +) + +# tuple != tuple (return -1/0/1) +tuple_not_equal_op = custom_op( + arg_types=[tuple_rprimitive, tuple_rprimitive], + return_type=c_int_rprimitive, + c_function_name="CPyTuple_Compare", + error_kind=ERR_NEG_INT, + extra_int_constants=[(3, c_int_rprimitive)], # Py_NE ) # tuple + tuple diff --git a/mypyc/test-data/irbuild-lists.test b/mypyc/test-data/irbuild-lists.test index 6e99075d742d4..d1258a357dbe6 100644 --- a/mypyc/test-data/irbuild-lists.test +++ b/mypyc/test-data/irbuild-lists.test @@ -373,7 +373,7 @@ def eq(x, y): r0 :: i32 r1, r2 :: bit L0: - r0 = CPyList_Equal(x, y) + r0 = CPyList_Compare(x, y, 2) r1 = r0 >= 0 :: signed r2 = r0 == 1 return r2 @@ -382,9 +382,9 @@ def neq(x, y): r0 :: i32 r1, r2 :: bit L0: - r0 = CPyList_Equal(x, y) + r0 = CPyList_Compare(x, y, 3) r1 = r0 >= 0 :: signed - r2 = r0 != 1 + r2 = r0 == 1 return r2 [case testListInsert] diff --git a/mypyc/test-data/irbuild-tuple.test b/mypyc/test-data/irbuild-tuple.test index b86699b7448d9..e59a0d2cf00a9 100644 --- a/mypyc/test-data/irbuild-tuple.test +++ b/mypyc/test-data/irbuild-tuple.test @@ -1168,7 +1168,7 @@ def eq(x, y): r0 :: i32 r1, r2 :: bit L0: - r0 = CPyTuple_Equal(x, y) + r0 = CPyTuple_Compare(x, y, 2) r1 = r0 >= 0 :: signed r2 = r0 == 1 return r2 @@ -1177,9 +1177,9 @@ def neq(x, y): r0 :: i32 r1, r2 :: bit L0: - r0 = CPyTuple_Equal(x, y) + r0 = CPyTuple_Compare(x, y, 3) r1 = r0 >= 0 :: signed - r2 = r0 != 1 + r2 = r0 == 1 return r2 [case testTupleFloatElementComparison] diff --git a/mypyc/test-data/run-lists.test b/mypyc/test-data/run-lists.test index 12d36db32689a..2f1406fc08764 100644 --- a/mypyc/test-data/run-lists.test +++ b/mypyc/test-data/run-lists.test @@ -855,7 +855,7 @@ class GoodList(list): [case testListEquality] from typing import Any, List from testutil import assertRaises -from leqsubclass import ListSubclass, ListEqOverride +from leqsubclass import ListSubclass, ListEqOverride, CaseInsensitiveList, ListNeOverride def eq(x: List[Any], y: List[Any]) -> bool: return x == y @@ -974,6 +974,17 @@ def test_subclass() -> None: x = ListEqOverride([]) assert not eq(x, x) assert ne(x, x) + # Only __eq__ is overridden, so != uses list.__ne__ + ci = CaseInsensitiveList(["A"]) + assert eq(ci, ["a"]) + assert ne(ci, ["a"]) + assert eq(["a"], ci) + assert ne(["a"], ci) + assert not ne(ci, ["A"]) + y = ListNeOverride([1]) + assert eq(y, [1]) + assert ne(y, [1]) + assert ne([1], y) [file leqsubclass.py] from typing import Any @@ -987,3 +998,12 @@ class ListEqOverride(list[Any]): def __ne__(self, other: object) -> bool: return True + +class CaseInsensitiveList(list[str]): + def __eq__(self, other: object) -> bool: + assert isinstance(other, list) + return [s.lower() for s in self] == [s.lower() for s in other] + +class ListNeOverride(list[Any]): + def __ne__(self, other: object) -> bool: + return True diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index 31e745d0aef2d..17d47873c3c89 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -521,7 +521,9 @@ class GoodTuple(tuple): [case testVariableLengthTupleEquality] from typing import Any, Tuple from testutil import assertRaises -from teqsubclass import TupleSubclass, TupleEqOverride, NT +from teqsubclass import ( + TupleSubclass, TupleEqOverride, TupleEqOnlyOverride, TupleNeOverride, NT +) def eq(x: Tuple[Any, ...], y: Tuple[Any, ...]) -> bool: return x == y @@ -604,10 +606,32 @@ def test_error() -> None: eq((RaisesOnEq(),), (1,)) with assertRaises(ValueError, "eq"): ne((1, RaisesOnEq()), (1, 2)) - # Lengths differ, so items aren't compared - assert not eq((RaisesOnEq(),), (1, 2)) + # Items are compared even if the lengths differ + with assertRaises(ValueError, "eq"): + eq((RaisesOnEq(),), (1, 2)) + with assertRaises(ValueError, "eq"): + ne((1, 2), (RaisesOnEq(),)) # Items are compared in order, and the first mismatch stops the comparison assert not eq((1, RaisesOnEq()), (2, 3)) + assert not eq((1, RaisesOnEq()), (2,)) + +class CountingEq: + def __init__(self) -> None: + self.count = 0 + + def __eq__(self, other: object) -> bool: + self.count += 1 + return True + +def test_different_lengths() -> None: + c = CountingEq() + assert not eq((c,), (1, 2)) + assert ne((1, 2, 3), (c,)) + assert c.count == 2 + a: Any = (c,) + b: Any = (1, 2) + assert a != b + assert c.count == 3 def test_subclass() -> None: assert eq(TupleSubclass((1, 2)), (1, 2)) @@ -622,6 +646,17 @@ def test_subclass() -> None: x = TupleEqOverride(()) assert not eq(x, x) assert ne(x, x) + # Only __eq__ is overridden, so != uses tuple.__ne__ + y = TupleEqOnlyOverride((1,)) + assert eq(y, (2,)) + assert ne(y, (2,)) + assert eq((2,), y) + assert ne((2,), y) + assert not ne(y, (1,)) + z = TupleNeOverride((1,)) + assert eq(z, (1,)) + assert ne(z, (1,)) + assert ne((1,), z) [file teqsubclass.py] from typing import Any, NamedTuple @@ -636,6 +671,14 @@ class TupleEqOverride(tuple[Any, ...]): def __ne__(self, other: object) -> bool: return True +class TupleEqOnlyOverride(tuple[Any, ...]): + def __eq__(self, other: object) -> bool: + return True + +class TupleNeOverride(tuple[Any, ...]): + def __ne__(self, other: object) -> bool: + return True + class NT(NamedTuple): x: int y: int