Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ Version 2.3.0
argument form works, and the argument to ``__class_getitem__()`` is now
positional only.

**Bugs Fixed**

* Calling ``bytes()`` on an object proxy did not match calling ``bytes()``
on the wrapped object directly when the wrapped object did not implement
``__bytes__()``. The C extension implementation of ``__bytes__()`` used
``PyObject_Bytes()``, which only honours the ``__bytes__()`` protocol, so
``bytes(wrapt.ObjectProxy(3))`` raised ``TypeError`` even though
``bytes(3)`` returns a zero filled buffer. The pure Python implementation
already used the ``bytes()`` constructor and was unaffected. The C
extension now uses the ``bytes()`` constructor as well, so both
implementations yield the same result as using the wrapped object
directly.

Version 2.2.2
-------------

Expand Down
3 changes: 2 additions & 1 deletion src/wrapt/_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,8 @@ static PyObject *WraptObjectProxy_bytes(WraptObjectProxyObject *self,
return NULL;
}

return PyObject_Bytes(self->wrapped);
return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type,
self->wrapped, NULL);
}

/* ------------------------------------------------------------------------- */
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_object_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,16 @@ def __bytes__(self):

self.assertEqual(bytes(instance), bytes(proxy))

def test_int_bytes(self):
# bytes(proxy) should behave like bytes() on the wrapped object even
# when it has no __bytes__ (e.g. an int, where bytes(n) yields a
# zero-filled buffer of length n).
instance = 3

proxy = wrapt.ObjectProxy(instance)

self.assertEqual(bytes(instance), bytes(proxy))

def test_str_format(self):
instance = "abcd"

Expand Down
Loading