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
21 changes: 20 additions & 1 deletion graalpython/com.oracle.graal.python.test/src/tests/test_slice.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018, 2025, Oracle and/or its affiliates.
# Copyright (c) 2018, 2026, Oracle and/or its affiliates.
# Copyright (c) 2013, Regents of the University of California
#
# All rights reserved.
Expand Down Expand Up @@ -196,6 +196,25 @@ def __index__(self):
assert "slice indices must be integers" in str(e)


def test_repr():
assert repr(slice(1, 2, 3)) == "slice(1, 2, 3)"
assert repr(slice(1, 2)) == "slice(1, 2, None)"
assert repr(slice(None)) == "slice(None, None, None)"
assert repr(slice(2**70)) == "slice(None, 1180591620717411303424, None)"
# each member is formatted with repr(), not with a debug representation
assert repr(slice(())) == "slice(None, (), None)"
assert repr(slice((1,))) == "slice(None, (1,), None)"
assert repr(slice([])) == "slice(None, [], None)"
assert repr(slice("")) == "slice(None, '', None)"
assert repr(slice(True, 2)) == "slice(True, 2, None)"

class Custom:
def __repr__(self):
return "<custom>"

assert repr(slice(Custom())) == "slice(None, <custom>, None)"


def test_slice_eq():
# Note: large numbers that do not get interned when boxed...
assert slice(2, 10000, 10) == slice(2, 10000, 10)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
* Copyright (c) 2017, 2026, Oracle and/or its affiliates.
* Copyright (c) 2014, Regents of the University of California
*
* All rights reserved.
Expand Down Expand Up @@ -27,7 +27,6 @@

import static com.oracle.graal.python.nodes.SpecialMethodNames.J___REDUCE__;
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;

import java.util.List;

Expand All @@ -46,11 +45,13 @@
import com.oracle.graal.python.builtins.objects.slice.SliceNodes.ComputeIndices;
import com.oracle.graal.python.builtins.objects.slice.SliceNodes.SliceCastToToBigInt;
import com.oracle.graal.python.builtins.objects.slice.SliceNodes.SliceExactCastToInt;
import com.oracle.graal.python.builtins.objects.str.StringUtils.SimpleTruffleStringFormatNode;
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
import com.oracle.graal.python.builtins.objects.type.TpSlots;
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotHashFun.HashBuiltinNode;
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotRichCompare.RichCmpBuiltinNode;
import com.oracle.graal.python.lib.PyObjectHashNode;
import com.oracle.graal.python.lib.PyObjectReprAsTruffleStringNode;
import com.oracle.graal.python.lib.PyObjectRichCompare;
import com.oracle.graal.python.lib.PyObjectRichCompareBool;
import com.oracle.graal.python.lib.PySliceNew;
Expand All @@ -65,7 +66,6 @@
import com.oracle.graal.python.nodes.object.GetClassNode;
import com.oracle.graal.python.runtime.exception.PException;
import com.oracle.graal.python.runtime.object.PFactory;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Exclusive;
Expand Down Expand Up @@ -122,9 +122,14 @@ static Object threeArgs(@SuppressWarnings("unused") Object cls, Object start, Ob
@GenerateNodeFactory
abstract static class ReprNode extends PythonUnaryBuiltinNode {
@Specialization
@TruffleBoundary
public static TruffleString repr(PSlice self) {
return toTruffleStringUncached(self.toString());
static TruffleString repr(VirtualFrame frame, PSlice self,
@Bind Node inliningTarget,
@Cached PyObjectReprAsTruffleStringNode reprNode,
@Cached SimpleTruffleStringFormatNode simpleTruffleStringFormatNode) {
return simpleTruffleStringFormatNode.format("slice(%s, %s, %s)",
reprNode.execute(frame, inliningTarget, self.getStart()),
reprNode.execute(frame, inliningTarget, self.getStop()),
reprNode.execute(frame, inliningTarget, self.getStep()));
}
}

Expand Down