Skip to content
Open
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/tuple_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Operators
* ``tup1 == tup2``, ``tup1 != tup2`` (variable-length tuples)
* ``tup1 + tup2``
* ``tup * n``, ``n * tup``
* ``obj in tup``

Statements
----------
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 @@ -1037,6 +1037,8 @@ PyObject *CPyList_Build(Py_ssize_t len, ...);
PyObject *CPyList_PopLast(PyObject *obj);
PyObject *CPyList_Pop(PyObject *obj, CPyTagged index);
CPyTagged CPyList_Count(PyObject *obj, PyObject *value);
int CPySequence_ContainsInt(PyObject *seq, CPyTagged value);
int CPySequence_ContainsStr(PyObject *seq, PyObject *str);
int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value);
PyObject *CPyList_Extend(PyObject *o1, PyObject *o2);
int CPyList_Remove(PyObject *list, PyObject *obj);
Expand Down
106 changes: 106 additions & 0 deletions mypyc/lib-rt/list_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,112 @@ CPyTagged CPyList_Count(PyObject *obj, PyObject *value)
return list_count((PyListObject *)obj, value);
}

// Return 1 if an exact list or tuple contains a short int value, 0 if it doesn't, or
// -1 on error. Exact int items are compared without boxing the value. The boxed value
// is only created if there is an item of some other type.
//
// The caller must hold a critical section on a list.
static int _CPySequence_ContainsShortInt(PyObject *seq, Py_ssize_t value) {
PyObject *boxed = NULL;
int result = 0;
// A list may be resized by __eq__, so the size and item array must be read again
// after each comparison
for (Py_ssize_t i = 0; i < Py_SIZE(seq); i++) {
PyObject *item = PySequence_Fast_GET_ITEM(seq, i);
if (PyLong_CheckExact(item)) {
int overflow;
Py_ssize_t n = CPyLong_AsSsize_tAndOverflow(item, &overflow);
if (n == value && !overflow) {
result = 1;
break;
}
continue;
}
if (boxed == NULL) {
boxed = PyLong_FromSsize_t(value);
if (boxed == NULL) {
result = -1;
break;
}
}
Py_INCREF(item);
int cmp = PyObject_RichCompareBool(item, boxed, Py_EQ);
Py_DECREF(item);
if (cmp != 0) {
result = cmp;
break;
}
}
Py_XDECREF(boxed);
return result;
}

int CPySequence_ContainsInt(PyObject *seq, CPyTagged value) {
if (likely(CPyTagged_CheckShort(value))) {
Py_ssize_t n = CPyTagged_ShortAsSsize_t(value);
if (PyList_CheckExact(seq)) {
int result;
Py_BEGIN_CRITICAL_SECTION(seq);
result = _CPySequence_ContainsShortInt(seq, n);
Py_END_CRITICAL_SECTION();
return result;
} else if (PyTuple_CheckExact(seq)) {
return _CPySequence_ContainsShortInt(seq, n);
}
}
PyObject *obj = CPyTagged_AsObject(value);
int result = PySequence_Contains(seq, obj);
Py_DECREF(obj);
return result;
}

// Return 1 if an exact list or tuple contains an exact str, 0 if it doesn't, or -1 on
// error. Exact str items are compared directly.
//
// The caller must hold a critical section on a list.
static int _CPySequence_ContainsExactStr(PyObject *seq, PyObject *str) {
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
int kind = PyUnicode_KIND(str);
const void *data = PyUnicode_DATA(str);
for (Py_ssize_t i = 0; i < Py_SIZE(seq); i++) {
PyObject *item = PySequence_Fast_GET_ITEM(seq, i);
int cmp;
if (item == str) {
return 1;
} else if (PyUnicode_CheckExact(item)) {
// Strings are always in the most compact representation, so equal strings
// have the same kind
cmp = PyUnicode_GET_LENGTH(item) == len
&& PyUnicode_KIND(item) == kind
&& memcmp(PyUnicode_DATA(item), data, len * kind) == 0;
} else {
Py_INCREF(item);
cmp = PyObject_RichCompareBool(item, str, Py_EQ);
Py_DECREF(item);
}
if (cmp != 0) {
return cmp;
}
}
return 0;
}

int CPySequence_ContainsStr(PyObject *seq, PyObject *str) {
// A str subclass could override __eq__, so it must use the generic implementation
if (likely(PyUnicode_CheckExact(str))) {
if (PyList_CheckExact(seq)) {
int result;
Py_BEGIN_CRITICAL_SECTION(seq);
result = _CPySequence_ContainsExactStr(seq, str);
Py_END_CRITICAL_SECTION();
return result;
} else if (PyTuple_CheckExact(seq)) {
return _CPySequence_ContainsExactStr(seq, str);
}
}
return PySequence_Contains(seq, str);
}

int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value)
{
if (CPyTagged_CheckShort(index)) {
Expand Down
24 changes: 24 additions & 0 deletions mypyc/primitives/list_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mypyc.ir.ops import ERR_FALSE, ERR_MAGIC, ERR_NEVER
from mypyc.ir.rtypes import (
bit_rprimitive,
bool_rprimitive,
c_int_rprimitive,
c_pyssize_t_rprimitive,
int64_rprimitive,
Expand All @@ -13,6 +14,7 @@
object_rprimitive,
pointer_rprimitive,
short_int_rprimitive,
str_rprimitive,
uint64_rprimitive,
void_rtype,
)
Expand Down Expand Up @@ -282,6 +284,28 @@
error_kind=ERR_MAGIC,
)

# int in list
binary_op(
name="in",
arg_types=[int_rprimitive, list_rprimitive],
return_type=c_int_rprimitive,
c_function_name="CPySequence_ContainsInt",
error_kind=ERR_NEG_INT,
truncated_type=bool_rprimitive,
ordering=[1, 0],
)

# str in list
binary_op(
name="in",
arg_types=[str_rprimitive, list_rprimitive],
return_type=c_int_rprimitive,
c_function_name="CPySequence_ContainsStr",
error_kind=ERR_NEG_INT,
truncated_type=bool_rprimitive,
ordering=[1, 0],
)

# list.insert(index, obj)
method_op(
name="insert",
Expand Down
24 changes: 24 additions & 0 deletions mypyc/primitives/tuple_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER
from mypyc.ir.rtypes import (
bit_rprimitive,
bool_rprimitive,
c_int_rprimitive,
c_pyssize_t_rprimitive,
c_size_t_rprimitive,
int_rprimitive,
list_rprimitive,
object_rprimitive,
str_rprimitive,
tuple_rprimitive,
void_rtype,
)
Expand Down Expand Up @@ -157,6 +159,28 @@
error_kind=ERR_MAGIC,
)

# int in tuple
binary_op(
name="in",
arg_types=[int_rprimitive, tuple_rprimitive],
return_type=c_int_rprimitive,
c_function_name="CPySequence_ContainsInt",
error_kind=ERR_NEG_INT,
truncated_type=bool_rprimitive,
ordering=[1, 0],
)

# str in tuple
binary_op(
name="in",
arg_types=[str_rprimitive, tuple_rprimitive],
return_type=c_int_rprimitive,
c_function_name="CPySequence_ContainsStr",
error_kind=ERR_NEG_INT,
truncated_type=bool_rprimitive,
ordering=[1, 0],
)

# tuple * int
binary_op(
name="*",
Expand Down
70 changes: 61 additions & 9 deletions mypyc/test-data/irbuild-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -343,22 +343,74 @@ L0:
return r0

[case testListIn]
from typing import List
from typing import List, Optional
def f(x: List[int], y: int) -> bool:
return y in x
def str_in(x: List[str], y: str) -> bool:
return y in x
def not_in(x: List[str], y: str) -> bool:
return y not in x
def generic(x: List[object], y: object, z: Optional[int]) -> bool:
return y in x or z in x
[out]
def f(x, y):
x :: list
y :: int
r0 :: object
r1 :: i32
r2 :: bit
r3 :: bool
r0 :: i32
r1 :: bit
r2 :: bool
L0:
r0 = box(int, y)
r1 = PySequence_Contains(x, r0)
r2 = r1 >= 0 :: signed
r3 = truncate r1: i32 to builtins.bool
r0 = CPySequence_ContainsInt(x, y)
r1 = r0 >= 0 :: signed
r2 = truncate r0: i32 to builtins.bool
return r2
def str_in(x, y):
x :: list
y :: str
r0 :: i32
r1 :: bit
r2 :: bool
L0:
r0 = CPySequence_ContainsStr(x, y)
r1 = r0 >= 0 :: signed
r2 = truncate r0: i32 to builtins.bool
return r2
def not_in(x, y):
x :: list
y :: str
r0 :: i32
r1 :: bit
r2, r3 :: bool
L0:
r0 = CPySequence_ContainsStr(x, y)
r1 = r0 >= 0 :: signed
r2 = truncate r0: i32 to builtins.bool
r3 = r2 ^ 1
return r3
def generic(x, y, z):
x :: list
y :: object
z :: union[int, None]
r0 :: i32
r1 :: bit
r2, r3 :: bool
r4 :: i32
r5 :: bit
r6 :: bool
L0:
r0 = PySequence_Contains(x, y)
r1 = r0 >= 0 :: signed
r2 = truncate r0: i32 to builtins.bool
if r2 goto L1 else goto L2 :: bool
L1:
r3 = r2
goto L3
L2:
r4 = PySequence_Contains(x, z)
r5 = r4 >= 0 :: signed
r6 = truncate r4: i32 to builtins.bool
r3 = r6
L3:
return r3

[case testListEquality]
Expand Down
31 changes: 31 additions & 0 deletions mypyc/test-data/irbuild-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,37 @@ L2:
return r3


[case testVariableLengthTupleIn]
from typing import Tuple
def int_in(x: Tuple[int, ...], y: int) -> bool:
return y in x
def str_not_in(x: Tuple[str, ...], y: str) -> bool:
return y not in x
[out]
def int_in(x, y):
x :: tuple
y :: int
r0 :: i32
r1 :: bit
r2 :: bool
L0:
r0 = CPySequence_ContainsInt(x, y)
r1 = r0 >= 0 :: signed
r2 = truncate r0: i32 to builtins.bool
return r2
def str_not_in(x, y):
x :: tuple
y :: str
r0 :: i32
r1 :: bit
r2, r3 :: bool
L0:
r0 = CPySequence_ContainsStr(x, y)
r1 = r0 >= 0 :: signed
r2 = truncate r0: i32 to builtins.bool
r3 = r2 ^ 1
return r3

[case testTupleOperatorIn]
def f(i: int) -> bool:
return i in [1, 2, 3]
Expand Down
Loading
Loading