Make bytes() on a proxy match bytes() on the wrapped object#345
Merged
GrahamDumpleton merged 1 commit intoJul 13, 2026
Merged
Conversation
The C extension implementation of ObjectProxy.__bytes__() called PyObject_Bytes() on the wrapped object, which only honours the __bytes__() protocol. As the proxy always defines __bytes__(), bytes() on a proxy routed through it, so bytes() on a proxy wrapping an object without __bytes__() (e.g. an int) raised TypeError even though bytes() on the wrapped object works. The pure Python implementation already used the bytes() constructor and behaved correctly. Use the bytes() constructor in the C extension too so both implementations return the same result as calling bytes() on the wrapped object directly.
Owner
|
Merged and available via |
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.
The C extension
ObjectProxy.__bytes__()usesPyObject_Bytes(wrapped), which only honours the__bytes__()protocol. Since the proxy always defines__bytes__(),bytes()on a proxy routes through it, sobytes(wrapt.ObjectProxy(3))raisesTypeErroreven thoughbytes(3)returns a zero-filled buffer. The pure Python implementation already uses thebytes()constructor and behaves correctly.This switches the C extension to the
bytes()constructor as well, so both implementations matchbytes()on the wrapped object directly (the__bytes__()case andbytes()on astrwithout an encoding are unchanged). Regression test added; it fails before the change with theTypeErrorand passes after.