[Checkins] SVN: zope.hookable/trunk/ Add a pure-Python reference implementation.

Tres Seaver cvs-admin at zope.org
Mon Jun 4 16:52:12 UTC 2012


Log message for revision 126581:
  Add a pure-Python reference implementation.
  

Changed:
  _U  zope.hookable/trunk/
  U   zope.hookable/trunk/CHANGES.txt
  U   zope.hookable/trunk/src/zope/hookable/__init__.py
  U   zope.hookable/trunk/src/zope/hookable/tests/test_hookable.py

-=-
Modified: zope.hookable/trunk/CHANGES.txt
===================================================================
--- zope.hookable/trunk/CHANGES.txt	2012-06-04 16:52:04 UTC (rev 126580)
+++ zope.hookable/trunk/CHANGES.txt	2012-06-04 16:52:08 UTC (rev 126581)
@@ -4,6 +4,8 @@
 4.0.0 (unreleased)
 ##################
 
+- Added a pure-Python reference implementation.
+
 - Doctests moved to Sphinx documentation.
 
 - 100% unit test coverage.

Modified: zope.hookable/trunk/src/zope/hookable/__init__.py
===================================================================
--- zope.hookable/trunk/src/zope/hookable/__init__.py	2012-06-04 16:52:04 UTC (rev 126580)
+++ zope.hookable/trunk/src/zope/hookable/__init__.py	2012-06-04 16:52:08 UTC (rev 126581)
@@ -14,4 +14,31 @@
 """Hookable object support
 """
 
+class _py_hookable(object):
+    __slots__ = ('_original', '_implementation')
+    
+    def __init__(self, *args, **kw):
+        if len(args) == 0 and 'implementation' in kw:
+            args = (kw.pop('implementation'),)
+        if kw:
+            raise TypeError('Unknown keyword arguments')
+        if len(args) != 1:
+            raise TypeError('Exactly one argument required')
+        self._original = self._implementation = args[0]
+
+    original = property(lambda self: self._original)
+    implementation = property(lambda self: self._implementation)
+
+    def sethook(self, new_callable):
+        old, self._implementation = self._implementation, new_callable
+        return old
+
+    def reset(self):
+        self._implementation = self._original
+
+    def __call__(self, *args, **kw):
+        return self._implementation(*args, **kw)
+
+hookable = _py_hookable
+
 from ._zope_hookable import hookable

Modified: zope.hookable/trunk/src/zope/hookable/tests/test_hookable.py
===================================================================
--- zope.hookable/trunk/src/zope/hookable/tests/test_hookable.py	2012-06-04 16:52:04 UTC (rev 126580)
+++ zope.hookable/trunk/src/zope/hookable/tests/test_hookable.py	2012-06-04 16:52:08 UTC (rev 126581)
@@ -15,11 +15,11 @@
 """
 import unittest
 
-class HookableTests(unittest.TestCase):
+class PyHookableTests(unittest.TestCase):
 
     def _callFUT(self, *args, **kw):
-        from zope.hookable import hookable
-        return hookable(*args, **kw)
+        from zope.hookable import _py_hookable
+        return _py_hookable(*args, **kw)
 
     def test_before_hook(self):
         def _foo():
@@ -92,7 +92,15 @@
         self.assertRaises(TypeError, self._callFUT, nonesuch=_foo)
 
 
+class HookableTests(PyHookableTests):
+
+    def _callFUT(self, *args, **kw):
+        from zope.hookable import hookable
+        return hookable(*args, **kw)
+
+
 def test_suite():
     return unittest.TestSuite((
+        unittest.makeSuite(PyHookableTests),
         unittest.makeSuite(HookableTests),
     ))



More information about the checkins mailing list