Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions mypyc/doc/list_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
1 change: 1 addition & 0 deletions mypyc/doc/tuple_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``

Expand Down
40 changes: 39 additions & 1 deletion mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@
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,
list_not_equal_op,
new_list_op,
)
from mypyc.primitives.misc_ops import (
bool_op,
buf_init_item,
Expand Down Expand Up @@ -222,6 +229,8 @@
new_tuple_op,
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
Expand Down Expand Up @@ -2020,6 +2029,27 @@ 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,
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 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_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"""
# type cast to pass mypy check
Expand Down Expand Up @@ -3156,6 +3186,14 @@ 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, 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, tuple_not_equal_op, lreg, rreg, expr_op, line
)

lopt = optional_value_type(ltype)
ropt = optional_value_type(rtype)
Expand Down
2 changes: 2 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,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_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);
Expand Down Expand Up @@ -1186,6 +1187,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_Compare(PyObject *left, PyObject *right, int op);

static inline PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index)
{
Expand Down
55 changes: 55 additions & 0 deletions mypyc/lib-rt/list_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,61 @@ 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);
}

// 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
result = 1;
} else {
Py_BEGIN_CRITICAL_SECTION2(left, right);
result = _CPyList_Equal(left, right);
Py_END_CRITICAL_SECTION2();
if (result < 0) {
return -1;
}
}
return op == Py_EQ ? result : !result;
}
// Subclasses may override __eq__ and __ne__
PyObject *res = PyObject_RichCompare(left, right, op);
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)
Expand Down
45 changes: 45 additions & 0 deletions mypyc/lib-rt/tuple_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,48 @@ PyObject *CPySequenceTuple_SetSize(PyObject *tuple, Py_ssize_t size) {
}
return CPyTuple_MoveItems(tuple, size, size);
}

// 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;
}
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 op == Py_EQ ? result : !result;
}
// Subclasses may override __eq__ and __ne__
PyObject *res = PyObject_RichCompare(left, right, op);
if (res == NULL) {
return -1;
}
int result = PyObject_IsTrue(res);
Py_DECREF(res);
return result;
}
18 changes: 18 additions & 0 deletions mypyc/primitives/list_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,24 @@
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_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)
method_op(
name="remove",
Expand Down
28 changes: 27 additions & 1 deletion mypyc/primitives/tuple_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
c_size_t_rprimitive,
int_rprimitive,
Expand All @@ -17,7 +18,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")
Expand Down Expand Up @@ -122,6 +130,24 @@
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_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
binary_op(
name="+",
Expand Down
26 changes: 26 additions & 0 deletions mypyc/test-data/irbuild-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -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_Compare(x, y, 2)
r1 = r0 >= 0 :: signed
r2 = r0 == 1
return r2
def neq(x, y):
x, y :: list
r0 :: i32
r1, r2 :: bit
L0:
r0 = CPyList_Compare(x, y, 3)
r1 = r0 >= 0 :: signed
r2 = r0 == 1
return r2

[case testListInsert]
from typing import List
def f(x: List[int], y: int) -> None:
Expand Down
26 changes: 26 additions & 0 deletions mypyc/test-data/irbuild-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,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_Compare(x, y, 2)
r1 = r0 >= 0 :: signed
r2 = r0 == 1
return r2
def neq(x, y):
x, y :: tuple
r0 :: i32
r1, r2 :: bit
L0:
r0 = CPyTuple_Compare(x, y, 3)
r1 = r0 >= 0 :: signed
r2 = r0 == 1
return r2

[case testTupleFloatElementComparison]
def f(x: tuple[float], y: tuple[float]) -> bool:
return x == y
Expand Down
Loading
Loading