Skip to content

Commit 62f8121

Browse files
committed
[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): Canonical link: https://commits.webkit.org/240053@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@280413 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent fd6f598 commit 62f8121

5 files changed

Lines changed: 53 additions & 2 deletions

File tree

Tools/ChangeLog

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
2021-07-28 Jonathan Bedard <jbedard@apple.com>
2+
3+
[webkitcorepy] Add hybridmethod decorator
4+
https://bugs.webkit.org/show_bug.cgi?id=225991
5+
<rdar://problem/78230701>
6+
7+
Reviewed by Dewei Zhu.
8+
9+
* Scripts/libraries/webkitcorepy/setup.py: Bump version.
10+
* Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Ditto.
11+
* Scripts/libraries/webkitcorepy/webkitcorepy/decorators.py:
12+
(hybridmethod): Similar to the "classmethod" decorator, except this decorator allows the
13+
decorated functions to differentiate between being called by the class and an instance
14+
of the class.
15+
* Scripts/libraries/webkitcorepy/webkitcorepy/tests/decorators_unittest.py:
16+
(TestHybrid):
17+
(TestHybrid.is_type):
18+
(TestHybrid.test_type):
19+
(TestHybrid.test_instance):
20+
121
2021-07-28 Jonathan Bedard <jbedard@apple.com>
222

323
[webkitcorepy] Fix race condition in TaskPool unittests (follow-up fix)

Tools/Scripts/libraries/webkitcorepy/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def readme():
3030

3131
setup(
3232
name='webkitcorepy',
33-
version='0.7.1',
33+
version='0.7.2',
3434
description='Library containing various Python support classes and functions.',
3535
long_description=readme(),
3636
classifiers=[

Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from webkitcorepy.measure_time import MeasureTime
4040
from webkitcorepy.nested_fuzzy_dict import NestedFuzzyDict
4141

42-
version = Version(0, 7, 1)
42+
version = Version(0, 7, 2)
4343

4444
from webkitcorepy.autoinstall import Package, AutoInstall
4545
if sys.version_info > (3, 0):

Tools/Scripts/libraries/webkitcorepy/webkitcorepy/decorators.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,21 @@ def decorator(*args, **kwargs):
6464
def clear(self):
6565
self._cache = defaultdict(dict)
6666
self._last_called = defaultdict(dict)
67+
68+
69+
class hybridmethod(object):
70+
def __init__(self, function):
71+
self.function = function
72+
73+
def __get__(self, obj, cls):
74+
context = obj if obj is not None else cls
75+
76+
def wrapper(*args, **kwargs):
77+
return self.function(context, *args, **kwargs)
78+
79+
wrapper.__name__ = self.function.__name__
80+
wrapper.__doc__ = self.function.__doc__
81+
wrapper.__func__ = wrapper.im_func = self.function
82+
wrapper.__self__ = wrapper.im_self = context
83+
84+
return wrapper

Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/decorators_unittest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,16 @@ def test_override(self):
120120
self.assertEqual(TestMemoize.increment_with_arg(arg='x'), 1)
121121
self.assertEqual(TestMemoize.increment_with_arg(arg='x'), 2)
122122
self.assertEqual(TestMemoize.increment_with_arg(arg='x', cached=True), 2)
123+
124+
125+
class TestHybrid(unittest.TestCase):
126+
127+
@decorators.hybridmethod
128+
def is_type(context):
129+
return isinstance(context, type)
130+
131+
def test_type(self):
132+
self.assertTrue(TestHybrid.is_type())
133+
134+
def test_instance(self):
135+
self.assertFalse(self.is_type())

0 commit comments

Comments
 (0)