Skip to content

[mypyc] Fix indexing bytes with a slice object - #22559

Merged
JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-bytes-getitem-slice
Nov 27, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-bytes-getitem-slice

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

The inline bytes.__getitem__ primitives added in #20552 were used for any index type, so indexing bytes with a slice object failed when the index wasn't a slice expression. For example, with s: slice (or int | slice, or Any holding a slice), b[s] raised TypeError: 'slice' object cannot be interpreted as an integer instead of returning a bytes object.

Only use the inline primitives when the index is an int or a native int, and fall back to the generic __getitem__ otherwise. The check is in translate_getitem_with_bounds_check, which is shared with the str and librt.strings writer specializations (those callers already check the index type, or only accept i64 indices).

The new run test fails without the fix.

The inline bytes.__getitem__ primitives were used for any index type,
so b[s] raised TypeError if s was a slice object (e.g. with type slice,
int | slice or Any). Only use them for int and native int indices.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good!

@JukkaL
JukkaL merged commit f79a508 into python:master Nov 27, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants