Skip to content

Commit

Permalink
Added proxy support for __format__() dunder method.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamDumpleton committed Oct 6, 2024
1 parent 555865b commit e684ff9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Version 1.17.0
Note that version 1.17.0 drops support for Python 3.6 and 3.7. Python version
3.8 or later is required.

**New Features**

* Add `__format__()` method to `ObjectProxy` class to allow formatting of
wrapped object.

**Bugs Fixed**

* When a normal function or builtin function which had `wrapt.decorator` or a
Expand Down
19 changes: 19 additions & 0 deletions src/wrapt/_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,24 @@ static PyObject *WraptObjectProxy_bytes(

/* ------------------------------------------------------------------------- */

static PyObject *WraptObjectProxy_format(
WraptObjectProxyObject *self, PyObject *args)
{
PyObject *format_spec = NULL;

if (!self->wrapped) {
PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized");
return NULL;
}

if (!PyArg_ParseTuple(args, "|O:format", &format_spec))
return NULL;

return PyObject_Format(self->wrapped, format_spec);
}

/* ------------------------------------------------------------------------- */

static PyObject *WraptObjectProxy_reversed(
WraptObjectProxyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -1797,6 +1815,7 @@ static PyMethodDef WraptObjectProxy_methods[] = {
{ "__getattr__", (PyCFunction)WraptObjectProxy_getattr,
METH_VARARGS , 0 },
{ "__bytes__", (PyCFunction)WraptObjectProxy_bytes, METH_NOARGS, 0 },
{ "__format__", (PyCFunction)WraptObjectProxy_format, METH_VARARGS, 0 },
{ "__reversed__", (PyCFunction)WraptObjectProxy_reversed, METH_NOARGS, 0 },
#if PY_MAJOR_VERSION >= 3
{ "__round__", (PyCFunction)WraptObjectProxy_round, METH_NOARGS, 0 },
Expand Down
3 changes: 3 additions & 0 deletions src/wrapt/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def __repr__(self):
type(self.__wrapped__).__name__,
id(self.__wrapped__))

def __format__(self, format_spec):
return format(self.__wrapped__, format_spec)

def __reversed__(self):
return reversed(self.__wrapped__)

Expand Down
7 changes: 7 additions & 0 deletions tests/test_object_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,13 @@ def test_repr(self):

self.assertNotEqual(repr(value).find('ObjectProxy at'), -1)

def test_format(self):
value = 1

proxy = wrapt.ObjectProxy(1)

self.assertEqual("{:0>3}".format(proxy), "{:0>3}".format(value))

class TestDerivedClassCreation(unittest.TestCase):

def test_derived_new(self):
Expand Down

0 comments on commit e684ff9

Please sign in to comment.