Skip to content

[mypyc] Fix unchecked results of bytes operations on overriding subclasses - #22431

Merged
JukkaL merged 2 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-bytes-subclass-result
Nov 11, 2026
Merged

JukkaL merged 2 commits into
python:masterfrom
Jahongir-Qurbonov:mypyc-bytes-subclass-result

Conversation

@Jahongir-Qurbonov

@Jahongir-Qurbonov Jahongir-Qurbonov commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #22416, which fixed this for list, tuple, dict and str.

bytes * int, int * bytes, bytes.join(), bytes.translate() and bytes[i:j] (with indices that don't fit in a short int) call the overriding method when the object is a bytes subclass. The primitives declare a bytes result, but the result was never checked, so an override that returns another type was used as bytes:

# sub.py (not compiled)
class B(bytes):
    def __mul__(self, n): return 42

# compiled
def f() -> None:
    b: bytes = B(b"x")
    r = b * 2
    print(len(r))  # interpreted: TypeError; compiled: prints garbage (reads an int as bytes)

Same approach as for lists and tuples in #22416: the override is still called (the existing testBytesJoin test relies on this), but a non-bytes result raises TypeError. int * bytes now calls __rmul__ on subclasses, not __mul__, matching CPython.

…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.

@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! 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.
@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor Author

Done, CPyBytes_GetSlice now checks the result of the generic slicing operation, which is used for indices that don't fit in a short int. Short indices already use the fast path, which doesn't call __getitem__ and always returns an exact bytes object.

@JukkaL
JukkaL merged commit d5eee85 into python:master Nov 11, 2026
18 checks passed
@Jahongir-Qurbonov
Jahongir-Qurbonov deleted the mypyc-bytes-subclass-result branch November 14, 2026 22:04
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