Skip to content

Commit 29cd77a

Browse files
committed
Add support for __format__.
1 parent 58b0c8f commit 29cd77a

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

Diff for: src/lazy_object_proxy/cext.c

+17
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,22 @@ static PyObject *Proxy_aexit(
11821182

11831183
/* ------------------------------------------------------------------------- */
11841184

1185+
static PyObject *Proxy_format(
1186+
ProxyObject *self, PyObject *args, PyObject *kwds)
1187+
{
1188+
PyObject *format_spec = NULL;
1189+
1190+
Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
1191+
1192+
if (!PyArg_ParseTuple(args, "|O:format", &format_spec))
1193+
return NULL;
1194+
1195+
return PyObject_Format(self->wrapped, format_spec);
1196+
1197+
}
1198+
1199+
/* ------------------------------------------------------------------------- */
1200+
11851201
static PyObject *Proxy_await(ProxyObject *self)
11861202
{
11871203
Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
@@ -1311,6 +1327,7 @@ static PyMethodDef Proxy_methods[] = {
13111327
{ "__round__", (PyCFunction)Proxy_round, METH_NOARGS, 0 },
13121328
{ "__aenter__", (PyCFunction)Proxy_aenter, METH_NOARGS, 0 },
13131329
{ "__aexit__", (PyCFunction)Proxy_aexit, METH_VARARGS | METH_KEYWORDS, 0 },
1330+
{ "__format__", (PyCFunction)Proxy_format, METH_VARARGS, 0 },
13141331
{ NULL, NULL },
13151332
};
13161333

Diff for: src/lazy_object_proxy/simple.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ def __wrapped__(self):
9191

9292
def __repr__(self, __getattr__=object.__getattribute__):
9393
if '__wrapped__' in self.__dict__:
94-
return '<{} at 0x{:x} wrapping {!r} at 0x{:x} with factory {!r}>'.format(
95-
type(self).__name__, id(self), self.__wrapped__, id(self.__wrapped__), self.__factory__
96-
)
94+
return f'<{type(self).__name__} at 0x{id(self):x} wrapping {self.__wrapped__!r} at 0x{id(self.__wrapped__):x} with factory {self.__factory__!r}>'
9795
else:
9896
return f'<{type(self).__name__} at 0x{id(self):x} with factory {self.__factory__!r}>'
9997

@@ -249,6 +247,9 @@ def __reduce__(self):
249247
def __reduce_ex__(self, protocol):
250248
return identity, (self.__wrapped__,)
251249

250+
def __format__(self, format_spec):
251+
return self.__wrapped__.__format__(format_spec)
252+
252253
if await_:
253254
from .utils import __aenter__
254255
from .utils import __aexit__

Diff for: src/lazy_object_proxy/slots.py

+3
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ def __reduce__(self):
429429
def __reduce_ex__(self, protocol):
430430
return identity, (self.__wrapped__,)
431431

432+
def __format__(self, format_spec):
433+
return self.__wrapped__.__format__(format_spec)
434+
432435
if await_:
433436
from .utils import __aenter__
434437
from .utils import __aexit__

Diff for: tests/test_lazy_object_proxy.py

+9
Original file line numberDiff line numberDiff line change
@@ -1955,3 +1955,12 @@ def test_resolved_str(lop):
19551955
assert obj.__resolved__ is False
19561956
str(obj)
19571957
assert obj.__resolved__ is True
1958+
1959+
1960+
def test_format(lop):
1961+
class WithFormat:
1962+
def __format__(self, format_spec):
1963+
return f'spec({format_spec!r})'
1964+
1965+
obj = lop.Proxy(WithFormat)
1966+
assert f'{obj:stuff}' == "spec('stuff')"

0 commit comments

Comments
 (0)