Skip to content

Fix slice repr leaking the internal storage representation. - #1073

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:GH1071-slice-repr
Open

Fix slice repr leaking the internal storage representation.#1073
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:GH1071-slice-repr

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Fixes #1071

slice.__repr__ formatted its three members with Java's String.valueOf instead of Python repr(), so any member whose debug toString() differs from its repr leaked the internal representation. int and None members were unaffected, which is why the common case looked correct.

AS-IS

slice(()), slice([]), slice(""), slice(True, 2):

slice(None, tuple(EmptySequenceStorage[]), None)
slice(None, list(EmptySequenceStorage[]), None)
slice(None, , None)
slice(true, 2, None)

TO-BE

slice(None, (), None)
slice(None, [], None)
slice(None, '', None)
slice(True, 2, None)

(matching CPython)

Changes

  • Rewrite SliceBuiltins.ReprNode to apply PyObjectReprAsTruffleStringNode to getStart() / getStop() / getStep() and assemble the result with SimpleTruffleStringFormatNode, matching CPython's slice(%R, %R, %R). RangeBuiltins.ReprNode already uses this shape for range, whose CPython repr formats its three members with %R in the same way. The specialization no longer needs @TruffleBoundary. Unlike range, whose members are always integers, a slice member can be an arbitrary object with a user-defined __repr__, so the repr node is called with the frame rather than null.
  • Add test_repr to test_slice.py, covering the member types whose Java toString() diverges from their repr, and an object with a user-defined __repr__ to pin that the member is formatted by Python repr().

Testing

mx python-jvm on linux-aarch64, then mx graalpytest test_slice.py test_list.py test_tuple.py test_bytes.py test_memoryview.py test_range.py — 224 tests, all pass. repr() of 15 slice forms (one-, two- and three-argument, with int / None / bool / float / str / bytes / tuple / list / dict members and an object with a user-defined __repr__) was compared against CPython 3.13.5 and matches on every one.

The tp_repr slot delegated to the Java debug toString() of PSlice, which
appends each member with String.valueOf. CPython's slice_repr formats all
three with %R, so a non-integer member leaked its internal representation:
slice(()) printed slice(None, tuple(EmptySequenceStorage[]), None).

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: repr(slice(...)) leaks the internal storage representation for non-integer members

1 participant