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
3 changes: 3 additions & 0 deletions mypyc/lib-rt/bytes_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ PyObject *CPyBytes_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
startn = Clamp(startn, 0, len);
endn = Clamp(endn, 0, len);
Py_ssize_t slice_len = endn - startn;
if (slice_len < 0) {
slice_len = 0;
}
if (PyBytes_Check(obj)) {
return PyBytes_FromStringAndSize(PyBytes_AS_STRING(obj) + startn, slice_len);
} else {
Expand Down
10 changes: 10 additions & 0 deletions mypyc/test-data/run-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ def test_endswith() -> None:
assert not test2.endswith(b'other')

[case testBytesSlicing]
from typing import Any, cast

def test_reversed_slice_bounds() -> None:
bounds = (-100, -3, -1, 0, 1, 2, 3, 100, 2**100)
for value in (b"", b"a", b"abc"):
interpreted = cast(Any, value)
for start in bounds:
for end in bounds:
assert value[start:end] == interpreted[start:end]

def test_bytes_slicing() -> None:
b = b'abcdefg'
zero = int()
Expand Down
Loading