Skip to content

Make bytes() on a proxy match bytes() on the wrapped object#345

Merged
GrahamDumpleton merged 1 commit into
GrahamDumpleton:developfrom
Sanjays2402:fix/bytes-proxy-int
Jul 13, 2026
Merged

Make bytes() on a proxy match bytes() on the wrapped object#345
GrahamDumpleton merged 1 commit into
GrahamDumpleton:developfrom
Sanjays2402:fix/bytes-proxy-int

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

The C extension ObjectProxy.__bytes__() uses PyObject_Bytes(wrapped), which only honours the __bytes__() protocol. Since the proxy always defines __bytes__(), bytes() on a proxy routes through it, so bytes(wrapt.ObjectProxy(3)) raises TypeError even though bytes(3) returns a zero-filled buffer. The pure Python implementation already uses the bytes() constructor and behaves correctly.

This switches the C extension to the bytes() constructor as well, so both implementations match bytes() on the wrapped object directly (the __bytes__() case and bytes() on a str without an encoding are unchanged). Regression test added; it fails before the change with the TypeError and passes after.

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.
@GrahamDumpleton GrahamDumpleton merged commit 300d23d into GrahamDumpleton:develop Jul 13, 2026
49 checks passed
@GrahamDumpleton

Copy link
Copy Markdown
Owner

Merged and available via wrapt==2.3.0rc2. I will usually leave an RC sit for about a week and presuming no extra changes come in only then do a final release.

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