Title: [280413] trunk/Tools
Revision
280413
Author
jbed...@apple.com
Date
2021-07-28 19:31:53 -0700 (Wed, 28 Jul 2021)

Log Message

[webkitcorepy] Add hybridmethod decorator
https://bugs.webkit.org/show_bug.cgi?id=225991
<rdar://problem/78230701>

Reviewed by Dewei Zhu.

* Scripts/libraries/webkitcorepy/setup.py: Bump version.
* Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Ditto.
* Scripts/libraries/webkitcorepy/webkitcorepy/decorators.py:
(hybridmethod): Similar to the "classmethod" decorator, except this decorator allows the
decorated functions to differentiate between being called by the class and an instance
of the class.
* Scripts/libraries/webkitcorepy/webkitcorepy/tests/decorators_unittest.py:
(TestHybrid):
(TestHybrid.is_type):
(TestHybrid.test_type):
(TestHybrid.test_instance):

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (280412 => 280413)


--- trunk/Tools/ChangeLog	2021-07-29 01:48:45 UTC (rev 280412)
+++ trunk/Tools/ChangeLog	2021-07-29 02:31:53 UTC (rev 280413)
@@ -1,5 +1,25 @@
 2021-07-28  Jonathan Bedard  <jbed...@apple.com>
 
+        [webkitcorepy] Add hybridmethod decorator
+        https://bugs.webkit.org/show_bug.cgi?id=225991
+        <rdar://problem/78230701>
+
+        Reviewed by Dewei Zhu.
+
+        * Scripts/libraries/webkitcorepy/setup.py: Bump version.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Ditto.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/decorators.py:
+        (hybridmethod): Similar to the "classmethod" decorator, except this decorator allows the
+        decorated functions to differentiate between being called by the class and an instance
+        of the class.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/tests/decorators_unittest.py:
+        (TestHybrid):
+        (TestHybrid.is_type):
+        (TestHybrid.test_type):
+        (TestHybrid.test_instance):
+
+2021-07-28  Jonathan Bedard  <jbed...@apple.com>
+
         [webkitcorepy] Fix race condition in TaskPool unittests (follow-up fix)
         https://bugs.webkit.org/show_bug.cgi?id=227455
         <rdar://problem/79873003>

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/setup.py (280412 => 280413)


--- trunk/Tools/Scripts/libraries/webkitcorepy/setup.py	2021-07-29 01:48:45 UTC (rev 280412)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/setup.py	2021-07-29 02:31:53 UTC (rev 280413)
@@ -30,7 +30,7 @@
 
 setup(
     name='webkitcorepy',
-    version='0.7.1',
+    version='0.7.2',
     description='Library containing various Python support classes and functions.',
     long_description=readme(),
     classifiers=[

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py (280412 => 280413)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py	2021-07-29 01:48:45 UTC (rev 280412)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py	2021-07-29 02:31:53 UTC (rev 280413)
@@ -39,7 +39,7 @@
 from webkitcorepy.measure_time import MeasureTime
 from webkitcorepy.nested_fuzzy_dict import NestedFuzzyDict
 
-version = Version(0, 7, 1)
+version = Version(0, 7, 2)
 
 from webkitcorepy.autoinstall import Package, AutoInstall
 if sys.version_info > (3, 0):

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/decorators.py (280412 => 280413)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/decorators.py	2021-07-29 01:48:45 UTC (rev 280412)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/decorators.py	2021-07-29 02:31:53 UTC (rev 280413)
@@ -64,3 +64,21 @@
     def clear(self):
         self._cache = defaultdict(dict)
         self._last_called = defaultdict(dict)
+
+
+class hybridmethod(object):
+    def __init__(self, function):
+        self.function = function
+
+    def __get__(self, obj, cls):
+        context = obj if obj is not None else cls
+
+        def wrapper(*args, **kwargs):
+            return self.function(context, *args, **kwargs)
+
+        wrapper.__name__ = self.function.__name__
+        wrapper.__doc__ = self.function.__doc__
+        wrapper.__func__ = wrapper.im_func = self.function
+        wrapper.__self__ = wrapper.im_self = context
+
+        return wrapper

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/decorators_unittest.py (280412 => 280413)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/decorators_unittest.py	2021-07-29 01:48:45 UTC (rev 280412)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/decorators_unittest.py	2021-07-29 02:31:53 UTC (rev 280413)
@@ -120,3 +120,16 @@
         self.assertEqual(TestMemoize.increment_with_arg(arg='x'), 1)
         self.assertEqual(TestMemoize.increment_with_arg(arg='x'), 2)
         self.assertEqual(TestMemoize.increment_with_arg(arg='x', cached=True), 2)
+
+
+class TestHybrid(unittest.TestCase):
+
+    @decorators.hybridmethod
+    def is_type(context):
+        return isinstance(context, type)
+
+    def test_type(self):
+        self.assertTrue(TestHybrid.is_type())
+
+    def test_instance(self):
+        self.assertFalse(self.is_type())
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to