Skip to content

Commit a68627c

Browse files
committed
Add test for setting __factory__ attribute. Fix missing getter/setter for __factory__ in the cext.
1 parent 8ac8b6c commit a68627c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/lazy_object_proxy/cext.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,28 @@ static int Proxy_set_wrapped(ProxyObject *self,
10261026

10271027
/* ------------------------------------------------------------------------- */
10281028

1029+
static PyObject *Proxy_get_factory(
1030+
ProxyObject *self)
1031+
{
1032+
Py_INCREF(self->factory);
1033+
return self->factory;
1034+
}
1035+
1036+
/* ------------------------------------------------------------------------- */
1037+
1038+
static int Proxy_set_factory(ProxyObject *self,
1039+
PyObject *value)
1040+
{
1041+
if (value) Py_INCREF(value);
1042+
Py_DECREF(self->factory);
1043+
1044+
self->factory = value;
1045+
1046+
return 0;
1047+
}
1048+
1049+
/* ------------------------------------------------------------------------- */
1050+
10291051
static PyObject *Proxy_getattro(
10301052
ProxyObject *self, PyObject *name)
10311053
{
@@ -1253,6 +1275,8 @@ static PyGetSetDef Proxy_getset[] = {
12531275
(setter)Proxy_set_annotations, 0 },
12541276
{ "__wrapped__", (getter)Proxy_get_wrapped,
12551277
(setter)Proxy_set_wrapped, 0 },
1278+
{ "__factory__", (getter)Proxy_get_factory,
1279+
(setter)Proxy_set_factory, 0 },
12561280
{ NULL },
12571281
};
12581282

tests/test_lazy_object_proxy.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,18 @@ def foo():
16071607
assert proxy.__factory__ is foo
16081608

16091609

1610+
def test_patching_the_factory(lazy_object_proxy):
1611+
def foo():
1612+
raise AttributeError("boom!")
1613+
proxy = lazy_object_proxy.Proxy(foo)
1614+
pytest.raises(AttributeError, lambda: proxy.__wrapped__)
1615+
assert proxy.__factory__ is foo
1616+
1617+
proxy.__factory__ = lambda: foo
1618+
pytest.raises(AttributeError, proxy)
1619+
assert proxy.__wrapped__ is foo
1620+
1621+
16101622
def test_new(lazy_object_proxy):
16111623
a = lazy_object_proxy.Proxy.__new__(lazy_object_proxy.Proxy)
16121624
b = lazy_object_proxy.Proxy.__new__(lazy_object_proxy.Proxy)

0 commit comments

Comments
 (0)