Repository navigation
[mypyc] Fix unchecked results of bytes operations on overriding subclasses - #22431
Merged
JukkaL merged 2 commits intoNov 11, 2026
Merged
Conversation
…asses bytes * int, int * bytes, bytes.join() and bytes.translate() call the overriding method if the object is a bytes subclass, but the result was trusted to be bytes without a check. For example, if a bytes subclass defines __mul__ that returns an int, `r = b * 2; len(r)` with `b: bytes` would read the int object as bytes. Keep calling the override, but raise TypeError if the result is not bytes, as is already done for list and tuple operations. Also call __rmul__ instead of __mul__ for `n * b` when b is a bytes subclass.
This was referenced Nov 10, 2026
JukkaL
reviewed
Nov 11, 2026
JukkaL
left a comment
Collaborator
There was a problem hiding this comment.
Thanks! Can you also update CPyBytes_GetSlice, which doesn't check the return value of CPyObject_GetSlice?
CPyBytes_GetSlice falls back to the generic slicing operation for indices that don't fit in a short int, which calls __getitem__ on a bytes subclass. Raise TypeError if the result is not bytes.
Contributor
Author
|
Done, |
JukkaL
approved these changes
Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #22416, which fixed this for list, tuple, dict and str.
bytes * int,int * bytes,bytes.join(),bytes.translate()andbytes[i:j](with indices that don't fit in a short int) call the overriding method when the object is abytessubclass. The primitives declare abytesresult, but the result was never checked, so an override that returns another type was used asbytes:Same approach as for lists and tuples in #22416: the override is still called (the existing
testBytesJointest relies on this), but a non-bytesresult raisesTypeError.int * bytesnow calls__rmul__on subclasses, not__mul__, matching CPython.