From 837901b24102b87c5494465b82119c40b01e03f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Mon, 22 Mar 2021 15:40:02 +0200 Subject: [PATCH] Add __resolved__ property. Closes #41. --- src/lazy_object_proxy/cext.c | 14 ++++++++++++++ src/lazy_object_proxy/simple.py | 4 ++++ src/lazy_object_proxy/slots.py | 10 ++++++++++ tests/test_lazy_object_proxy.py | 20 ++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/lazy_object_proxy/cext.c b/src/lazy_object_proxy/cext.c index 515bb39..da9d7f8 100644 --- a/src/lazy_object_proxy/cext.c +++ b/src/lazy_object_proxy/cext.c @@ -1080,6 +1080,18 @@ static int Proxy_set_annotations(ProxyObject *self, /* ------------------------------------------------------------------------- */ +static PyObject *Proxy_get_resolved( + ProxyObject *self) +{ + PyObject *result; + + result = self->wrapped ? Py_True : Py_False; + Py_INCREF(result); + return result; +} + +/* ------------------------------------------------------------------------- */ + static PyObject *Proxy_get_wrapped( ProxyObject *self) { @@ -1444,6 +1456,8 @@ static PyGetSetDef Proxy_getset[] = { (setter)Proxy_set_wrapped, 0 }, { "__factory__", (getter)Proxy_get_factory, (setter)Proxy_set_factory, 0 }, + { "__resolved__", (getter)Proxy_get_resolved, + NULL, 0 }, { NULL }, }; diff --git a/src/lazy_object_proxy/simple.py b/src/lazy_object_proxy/simple.py index 9fc90d1..ea5cf8a 100644 --- a/src/lazy_object_proxy/simple.py +++ b/src/lazy_object_proxy/simple.py @@ -71,6 +71,10 @@ class Proxy(with_metaclass(_ProxyMetaType)): def __init__(self, factory): self.__dict__['__factory__'] = factory + @property + def __resolved__(self): + return '__wrapped__' in self.__dict__ + @cached_property def __wrapped__(self): self = self.__dict__ diff --git a/src/lazy_object_proxy/slots.py b/src/lazy_object_proxy/slots.py index c7fb235..41cee25 100644 --- a/src/lazy_object_proxy/slots.py +++ b/src/lazy_object_proxy/slots.py @@ -72,6 +72,7 @@ class Proxy(with_metaclass(_ProxyMetaType)): * ``__factory__`` is the callback that "materializes" the object we proxy to. * ``__target__`` will contain the object we proxy to, once it's "materialized". + * ``__resolved__`` is a boolean, `True` if factory was called. * ``__wrapped__`` is a property that does either: * return ``__target__`` if it's set. @@ -83,6 +84,15 @@ class Proxy(with_metaclass(_ProxyMetaType)): def __init__(self, factory): object.__setattr__(self, '__factory__', factory) + @property + def __resolved__(self, __getattr__=object.__getattribute__): + try: + __getattr__(self, '__target__') + except AttributeError: + return False + else: + return True + @property def __wrapped__(self, __getattr__=object.__getattribute__, __setattr__=object.__setattr__, __delattr__=object.__delattr__): diff --git a/tests/test_lazy_object_proxy.py b/tests/test_lazy_object_proxy.py index 879539a..8bd3532 100644 --- a/tests/test_lazy_object_proxy.py +++ b/tests/test_lazy_object_proxy.py @@ -1846,6 +1846,7 @@ def test_proto(benchmark, prototype): def test_subclassing_with_local_attr(lop): class Foo: pass + called = [] class LazyProxy(lop.Proxy): @@ -1897,3 +1898,22 @@ def test_fspath(lop): def test_fspath_method(lop): assert lop.Proxy(FSPathMock).__fspath__() == '/tmp' + + +def test_resolved_new(lop): + obj = lop.Proxy.__new__(lop.Proxy) + assert obj.__resolved__ is False + + +def test_resolved(lop): + obj = lop.Proxy(lambda: None) + assert obj.__resolved__ is False + assert obj.__wrapped__ is None + assert obj.__resolved__ is True + + +def test_resolved_str(lop): + obj = lop.Proxy(lambda: None) + assert obj.__resolved__ is False + str(obj) + assert obj.__resolved__ is True pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy