Title: [281191] trunk/Tools
Revision
281191
Author
jbed...@apple.com
Date
2021-08-18 09:34:05 -0700 (Wed, 18 Aug 2021)

Log Message

[webkitcorepy] Add CallByNeed object
https://bugs.webkit.org/show_bug.cgi?id=229148
<rdar://problem/81817705>

Reviewed by Dewei Zhu.

* Scripts/libraries/webkitcorepy/setup.py: Bump version.
* Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Bump version, export Defer object.
* Scripts/libraries/webkitcorepy/webkitcorepy/call_by_need.py: Added.
(CallByNeed):
(CallByNeed.__init__):
(CallByNeed.__getattribute__): Depending on the request attribute, either return it or attempt to
resolve the underlying value before returning the attribute of the resolved value.
(CallByNeed.value): Resolve and return underlying object.
(CallByNeed.__call__): Ditto.
(CallByNeed.__repr__): Resolve object and return representation.
(CallByNeed.__str__): Resolve object and return string conversion.
* Scripts/libraries/webkitcorepy/webkitcorepy/tests/call_by_need_unittest.py: Added.
(TestCallByNeed):
(TestCallByNeed.test_basic):
(TestCallByNeed.test_string_conversion):
(TestCallByNeed.test_attribute_forwarding):
(TestCallByNeed.test_failed_attribute_forwarding):

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (281190 => 281191)


--- trunk/Tools/ChangeLog	2021-08-18 16:22:18 UTC (rev 281190)
+++ trunk/Tools/ChangeLog	2021-08-18 16:34:05 UTC (rev 281191)
@@ -1,3 +1,29 @@
+2021-08-18  Jonathan Bedard  <jbed...@apple.com>
+
+        [webkitcorepy] Add CallByNeed object
+        https://bugs.webkit.org/show_bug.cgi?id=229148
+        <rdar://problem/81817705>
+
+        Reviewed by Dewei Zhu.
+
+        * Scripts/libraries/webkitcorepy/setup.py: Bump version.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Bump version, export Defer object.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/call_by_need.py: Added.
+        (CallByNeed):
+        (CallByNeed.__init__):
+        (CallByNeed.__getattribute__): Depending on the request attribute, either return it or attempt to
+        resolve the underlying value before returning the attribute of the resolved value.
+        (CallByNeed.value): Resolve and return underlying object.
+        (CallByNeed.__call__): Ditto.
+        (CallByNeed.__repr__): Resolve object and return representation.
+        (CallByNeed.__str__): Resolve object and return string conversion.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/tests/call_by_need_unittest.py: Added.
+        (TestCallByNeed):
+        (TestCallByNeed.test_basic):
+        (TestCallByNeed.test_string_conversion):
+        (TestCallByNeed.test_attribute_forwarding):
+        (TestCallByNeed.test_failed_attribute_forwarding):
+
 2021-08-18  Martin Robinson  <mrobin...@webkit.org>
 
         [Flatpak SDK] Increase the startup performance of webkit-flatpak

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/setup.py (281190 => 281191)


--- trunk/Tools/Scripts/libraries/webkitcorepy/setup.py	2021-08-18 16:22:18 UTC (rev 281190)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/setup.py	2021-08-18 16:34:05 UTC (rev 281191)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020 Apple Inc. All rights reserved.
+# Copyright (C) 2020-2021 Apple Inc. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -30,7 +30,7 @@
 
 setup(
     name='webkitcorepy',
-    version='0.8.3',
+    version='0.9.0',
     description='Library containing various Python support classes and functions.',
     long_description=readme(),
     classifiers=[

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py (281190 => 281191)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py	2021-08-18 16:22:18 UTC (rev 281190)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py	2021-08-18 16:34:05 UTC (rev 281191)
@@ -39,8 +39,9 @@
 from webkitcorepy.credentials import credentials
 from webkitcorepy.measure_time import MeasureTime
 from webkitcorepy.nested_fuzzy_dict import NestedFuzzyDict
+from webkitcorepy.call_by_need import CallByNeed
 
-version = Version(0, 8, 3)
+version = Version(0, 9, 0)
 
 from webkitcorepy.autoinstall import Package, AutoInstall
 if sys.version_info > (3, 0):

Added: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/call_by_need.py (0 => 281191)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/call_by_need.py	                        (rev 0)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/call_by_need.py	2021-08-18 16:34:05 UTC (rev 281191)
@@ -0,0 +1,56 @@
+# Copyright (C) 2021 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1.  Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer.
+# 2.  Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in the
+#     documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
+# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+class CallByNeed(object):
+    def __init__(self, callback, type=None):
+        self._callback = callback
+        self._value = None
+        self.type = type
+
+    def __getattribute__(self, name):
+        if name in dir(type(self)) or name in {'_callback', '_value'}:
+            return object.__getattribute__(self, name)
+        typ = object.__getattribute__(self, 'type')
+        if not typ:
+            raise AttributeError(
+                "'{}' object has no attribute '{},' and doesn't define a target type".format(type(self).__name__, name),
+            )
+        if name in dir(typ):
+            return object.__getattribute__(self, 'value').__getattribute__(name)
+        raise AttributeError("'{}' object has no attribute '{}'".format(typ.__name__, name))
+
+    @property
+    def value(self):
+        if self._callback:
+            self._value = self._callback()
+            self._callback = None
+        return self._value
+
+    def __call__(self):
+        return self.value
+
+    def __repr__(self):
+        return self.value.__repr__()
+
+    def __str__(self):
+        return str(self.value)

Added: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/call_by_need_unittest.py (0 => 281191)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/call_by_need_unittest.py	                        (rev 0)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/call_by_need_unittest.py	2021-08-18 16:34:05 UTC (rev 281191)
@@ -0,0 +1,51 @@
+# Copyright (C) 2021 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1.  Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer.
+# 2.  Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in the
+#     documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
+# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import unittest
+
+from webkitcorepy import CallByNeed
+
+
+class TestCallByNeed(unittest.TestCase):
+    def test_basic(self):
+        obj = CallByNeed(lambda: 'resolved')
+        self.assertIsNone(obj._value)
+        self.assertEqual(obj.value, 'resolved')
+
+    def test_string_conversion(self):
+        self.assertEqual(
+            str(CallByNeed(lambda: 'resolved')),
+            'resolved',
+        )
+
+    def test_attribute_forwarding(self):
+        self.assertEqual(
+            CallByNeed(lambda: 'resolved', type=str).lower(),
+            'resolved',
+        )
+
+    def test_failed_attribute_forwarding(self):
+        with self.assertRaises(AttributeError):
+            CallByNeed(lambda: 'resolved', type=str).bad_attribute
+
+        with self.assertRaises(AttributeError):
+            CallByNeed(lambda: 'resolved').bad_attribute
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to