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/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ def try_specialize_str_index_compare(
str_adjust_index_op,
str_range_check_op,
str_get_item_unsafe_as_int_op,
"string index out of range",
)
if codepoint is None:
return None
Expand Down
19 changes: 17 additions & 2 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,21 @@ def coerce(
return tmp
return src

def coerce_int_to_fixed_width(self, src: Value, target_type: RType, line: int) -> Value:
def coerce_int_to_fixed_width(
self,
src: Value,
target_type: RType,
line: int,
*,
slow_path_op: CFunctionDescription | None = None,
) -> Value:
"""Coerce a tagged int to a fixed-width native int.

If slow_path_op is given, use it instead of the default primitive to convert
an int that isn't a short int (only supported for i64 targets).
"""
assert is_fixed_width_rtype(target_type), target_type
assert slow_path_op is None or is_int64_rprimitive(target_type), target_type
assert isinstance(target_type, RPrimitive), target_type

fast, slow, overflow, end = BasicBlock(), BasicBlock(), BasicBlock(), BasicBlock()
Expand Down Expand Up @@ -660,7 +673,9 @@ def coerce_int_to_fixed_width(self, src: Value, target_type: RType, line: int) -
)
ptr2 = Register(c_pointer_rprimitive)
self.add(Assign(ptr2, ptr))
if is_int64_rprimitive(target_type):
if slow_path_op is not None:
conv_op = slow_path_op
elif is_int64_rprimitive(target_type):
conv_op = int_to_int64_op
elif is_uint64_rprimitive(target_type):
conv_op = int_to_uint64_op
Expand Down
28 changes: 24 additions & 4 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
int_abs_op,
int_to_big_endian_op,
int_to_bytes_op,
int_to_index_op,
int_to_little_endian_op,
isinstance_int,
)
Expand Down Expand Up @@ -1614,6 +1615,7 @@ def translate_ord(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value
str_adjust_index_op,
str_range_check_op,
str_get_item_unsafe_as_int_op,
"string index out of range",
)
return result

Expand Down Expand Up @@ -1771,6 +1773,17 @@ def specialize_int_to_bytes(builder: IRBuilder, expr: CallExpr, callee: RefExpr)
)


def coerce_int_index_to_int64(builder: IRBuilder, index: Value, line: int) -> Value:
"""Coerce an int index to i64 for the specialized item access primitives.

Like CPython, raise IndexError (instead of ValueError, as a regular
coercion would) if the index doesn't fit in Py_ssize_t.
"""
return builder.builder.coerce_int_to_fixed_width(
index, int64_rprimitive, line, slow_path_op=int_to_index_op
)


def translate_getitem_with_bounds_check(
builder: IRBuilder,
base_expr: Expression,
Expand All @@ -1779,6 +1792,7 @@ def translate_getitem_with_bounds_check(
adjust_index_op: PrimitiveDescription,
range_check_op: PrimitiveDescription,
get_item_unsafe_op: PrimitiveDescription,
index_error_msg: str = "index out of range",
) -> Value | None:
"""Shared helper for optimized __getitem__ with bounds checking.

Expand Down Expand Up @@ -1809,6 +1823,8 @@ def translate_getitem_with_bounds_check(

# Get the index argument
index = builder.accept(args[0])
if is_int_rprimitive(index.type):
index = coerce_int_index_to_int64(builder, index, ctx_expr.line)

# Adjust the index (handle negative indices)
adjusted_index = builder.primitive_op(adjust_index_op, [obj, index], ctx_expr.line)
Expand All @@ -1824,9 +1840,7 @@ def translate_getitem_with_bounds_check(

# Handle invalid index - raise IndexError
builder.activate_block(invalid_block)
builder.add(
RaiseStandardError(RaiseStandardError.INDEX_ERROR, "index out of range", ctx_expr.line)
)
builder.add(RaiseStandardError(RaiseStandardError.INDEX_ERROR, index_error_msg, ctx_expr.line))
builder.add(Unreachable())

# Handle valid index - get the item
Expand All @@ -1849,6 +1863,7 @@ def translate_bytes_writer_get_item(
bytes_writer_adjust_index_op,
bytes_writer_range_check_op,
bytes_writer_get_item_unsafe_op,
"BytesWriter index out of range",
)


Expand All @@ -1867,6 +1882,8 @@ def translate_bytes_writer_set_item(
# Get the index and value arguments
index = builder.accept(args[0])
value = builder.accept(args[1])
if is_int_rprimitive(index.type):
index = coerce_int_index_to_int64(builder, index, ctx_expr.line)

# Adjust the index (handle negative indices)
adjusted_index = builder.primitive_op(
Expand All @@ -1887,7 +1904,9 @@ def translate_bytes_writer_set_item(
# Handle invalid index - raise IndexError
builder.activate_block(invalid_block)
builder.add(
RaiseStandardError(RaiseStandardError.INDEX_ERROR, "index out of range", ctx_expr.line)
RaiseStandardError(
RaiseStandardError.INDEX_ERROR, "BytesWriter index out of range", ctx_expr.line
)
)
builder.add(Unreachable())

Expand All @@ -1913,6 +1932,7 @@ def translate_string_writer_get_item(
string_writer_adjust_index_op,
string_writer_range_check_op,
string_writer_get_item_unsafe_op,
"StringWriter index out of range",
)


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 @@ -197,6 +197,7 @@ CPyTagged CPyTagged_FromUInt32(uint32_t value);
PyObject *CPyTagged_AsObject(CPyTagged x);
PyObject *CPyTagged_StealAsObject(CPyTagged x);
Py_ssize_t CPyTagged_AsSsize_t(CPyTagged x);
void CPy_LargeIndexError(CPyTagged index, const char *msg);
void CPyTagged_IncRef(CPyTagged x);
void CPyTagged_DecRef(CPyTagged x);
void CPyTagged_XDecRef(CPyTagged x);
Expand Down Expand Up @@ -227,6 +228,7 @@ PyObject *CPyLong_FromStrWithBase(PyObject *o, CPyTagged base);
PyObject *CPyLong_FromStr(PyObject *o);
PyObject *CPyBool_Str(bool b);
int64_t CPyLong_AsInt64_(PyObject *o);
int64_t CPyLong_AsIndex(PyObject *o);
int64_t CPyInt64_Divide(int64_t x, int64_t y);
int64_t CPyInt64_Remainder(int64_t x, int64_t y);
int32_t CPyLong_AsInt32_(PyObject *o);
Expand Down
2 changes: 1 addition & 1 deletion mypyc/lib-rt/bytes_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ CPyTagged CPyBytes_GetItem(PyObject *o, CPyTagged index) {
: ((PyByteArrayObject *)o)->ob_bytes[n];
return num << 1;
} else {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
CPy_LargeIndexError(index, "index out of range");
return CPY_INT_TAG;
}
}
Expand Down
22 changes: 22 additions & 0 deletions mypyc/lib-rt/int_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ Py_ssize_t CPyTagged_AsSsize_t(CPyTagged x) {
}
}

// Set the exception for a sequence index that isn't a short int. Like CPython,
// raise IndexError("cannot fit 'int' into an index-sized integer") if it
// doesn't fit in Py_ssize_t. Otherwise raise IndexError(msg), since an index
// this large is always out of range (no list or tuple can be this long, and
// neither can a str or bytes object on a 64-bit platform).
void CPy_LargeIndexError(CPyTagged index, const char *msg) {
Py_ssize_t n = PyNumber_AsSsize_t(CPyTagged_LongAsObject(index), PyExc_IndexError);
if (n != -1 || !PyErr_Occurred()) {
PyErr_SetString(PyExc_IndexError, msg);
}
}

// Convert an int used as a sequence index to i64. Like CPython, raise
// IndexError if the value doesn't fit in Py_ssize_t.
int64_t CPyLong_AsIndex(PyObject *o) {
Py_ssize_t n = PyNumber_AsSsize_t(o, PyExc_IndexError);
if (n == -1 && PyErr_Occurred()) {
return CPY_LL_INT_ERROR;
}
return n;
}

CPy_NOINLINE
void CPyTagged_IncRef(CPyTagged x) {
if (unlikely(CPyTagged_CheckLong(x))) {
Expand Down
47 changes: 28 additions & 19 deletions mypyc/lib-rt/list_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ PyObject *CPyList_GetItem(PyObject *list, CPyTagged index) {
Py_INCREF(result);
return result;
} else {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
CPy_LargeIndexError(index, "list index out of range");
return NULL;
}
}
Expand Down Expand Up @@ -173,7 +173,7 @@ bool CPyList_SetItem(PyObject *list, CPyTagged index, PyObject *value) {
return true;
} else {
Py_DECREF(value);
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
CPy_LargeIndexError(index, "list assignment index out of range");
return false;
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ PyObject *CPyList_GetItem_(PyObject *list, CPyTagged index) {
}
return PyList_GetItemRef(list, n);
} else {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
CPy_LargeIndexError(index, "list index out of range");
return NULL;
}
}
Expand All @@ -250,7 +250,7 @@ bool CPyList_SetItem_(PyObject *list, CPyTagged index, PyObject *value) {
return PyList_SetItem(list, n, value) >= 0;
} else {
Py_DECREF(value);
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
CPy_LargeIndexError(index, "list assignment index out of range");
return false;
}
}
Expand Down Expand Up @@ -293,7 +293,7 @@ PyObject *CPyList_GetItemBorrow(PyObject *list, CPyTagged index) {
}
return PyList_GET_ITEM(list, n);
} else {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
CPy_LargeIndexError(index, "list index out of range");
return NULL;
}
}
Expand Down Expand Up @@ -377,18 +377,23 @@ PyObject *CPyList_PopLast(PyObject *list)

PyObject *CPyList_Pop(PyObject *obj, CPyTagged index)
{
Py_ssize_t n;
if (CPyTagged_CheckShort(index)) {
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
n = CPyTagged_ShortAsSsize_t(index);
} else {
// Like CPython, raise OverflowError only if the index doesn't fit in
// Py_ssize_t. Other large indexes raise IndexError below.
n = PyLong_AsSsize_t(CPyTagged_LongAsObject(index));
if (n == -1 && PyErr_Occurred()) {
return NULL;
}
}
#ifdef Py_GIL_DISABLED
// We must use a slower implementation on free-threaded builds.
return list_pop_index(obj, n);
// We must use a slower implementation on free-threaded builds.
return list_pop_index(obj, n);
#else
return list_pop_impl((PyListObject *)obj, n);
return list_pop_impl((PyListObject *)obj, n);
#endif
} else {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
return NULL;
}
}

CPyTagged CPyList_Count(PyObject *obj, PyObject *value)
Expand All @@ -398,14 +403,18 @@ CPyTagged CPyList_Count(PyObject *obj, PyObject *value)

int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value)
{
Py_ssize_t n;
if (CPyTagged_CheckShort(index)) {
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
return PyList_Insert(list, n, value);
n = CPyTagged_ShortAsSsize_t(index);
} else {
// Like CPython, raise OverflowError only if the index doesn't fit in
// Py_ssize_t. Other large indexes are clamped to the list bounds.
n = PyLong_AsSsize_t(CPyTagged_LongAsObject(index));
if (n == -1 && PyErr_Occurred()) {
return -1;
}
}
// The max range doesn't exactly coincide with ssize_t, but we still
// want to keep the error message compatible with CPython.
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
return -1;
return PyList_Insert(list, n, value);
}

PyObject *CPyList_Extend(PyObject *o1, PyObject *o2) {
Expand Down
2 changes: 1 addition & 1 deletion mypyc/lib-rt/str_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ PyObject *CPyStr_GetItem(PyObject *str, CPyTagged index) {
}
return unicode;
} else {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
CPy_LargeIndexError(index, "string index out of range");
return NULL;
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion mypyc/lib-rt/tuple_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ PyObject *CPySequenceTuple_GetItem_(PyObject *tuple, CPyTagged index) {
Py_INCREF(result);
return result;
} else {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
CPy_LargeIndexError(index, "tuple index out of range");
return NULL;
}
}
Expand Down
9 changes: 9 additions & 0 deletions mypyc/primitives/int_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription:
error_kind=ERR_MAGIC_OVERLAPPING,
)

# Convert tagged int (as PyObject *) used as a sequence index to i64
# (raise IndexError if it doesn't fit in Py_ssize_t, like CPython)
int_to_index_op = custom_op(
arg_types=[object_rprimitive],
return_type=int64_rprimitive,
c_function_name="CPyLong_AsIndex",
error_kind=ERR_MAGIC_OVERLAPPING,
)

ssize_t_to_int_op = custom_op(
arg_types=[c_pyssize_t_rprimitive],
return_type=int_rprimitive,
Expand Down
5 changes: 5 additions & 0 deletions mypyc/test-data/fixtures/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

FLOAT_MAGIC: Final = -113.0

# Largest Py_ssize_t value (sys.maxsize isn't in the sys stub used by tests)
SSIZE_T_MAX: Final[int] = getattr(sys, "maxsize")
# Smallest positive int that isn't a short int, but fits in Py_ssize_t
SSIZE_T_LONG: Final[int] = SSIZE_T_MAX // 2 + 1

# Various different float values
float_vals = [
float(n) * 0.25 for n in range(-10, 10)
Expand Down
10 changes: 5 additions & 5 deletions mypyc/test-data/irbuild-librt-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ L0:
r1 = CPyBytesWriter_RangeCheck(b, r0)
if r1 goto L2 else goto L1 :: bool
L1:
r2 = raise IndexError('index out of range')
r2 = raise IndexError('BytesWriter index out of range')
unreachable
L2:
r3 = CPyBytesWriter_GetItem(b, r0)
Expand All @@ -88,7 +88,7 @@ L0:
r1 = CPyBytesWriter_RangeCheck(b, r0)
if r1 goto L2 else goto L1 :: bool
L1:
r2 = raise IndexError('index out of range')
r2 = raise IndexError('BytesWriter index out of range')
unreachable
L2:
CPyBytesWriter_SetItem(b, r0, x)
Expand Down Expand Up @@ -356,7 +356,7 @@ L0:
r1 = CPyStringWriter_RangeCheck(s, r0)
if r1 goto L2 else goto L1 :: bool
L1:
r2 = raise IndexError('index out of range')
r2 = raise IndexError('StringWriter index out of range')
unreachable
L2:
r3 = CPyStringWriter_GetItem(s, r0)
Expand Down Expand Up @@ -408,15 +408,15 @@ L1:
L2:
r4 = i ^ 1
r5 = r4
r6 = CPyLong_AsInt64(r5)
r6 = CPyLong_AsIndex(r5)
r3 = r6
keep_alive i
L3:
r7 = CPyStr_AdjustIndex(s, r3)
r8 = CPyStr_RangeCheck(s, r7)
if r8 goto L5 else goto L4 :: bool
L4:
r9 = raise IndexError('index out of range')
r9 = raise IndexError('string index out of range')
unreachable
L5:
r10 = CPyStr_GetItemUnsafeAsInt(s, r7)
Expand Down
Loading
Loading