Commits

Nam Nguyen committed 303a756

Add ``private`` decorator.

  • Participants
  • Parent commits adb8e97

Comments (0)

Files changed (2)

File intfprgm/__init__.py

         return overrides_impl(orig)
     else:
         return overrides_impl(True)(orig)
+
+
+class private(object):
+
+    def __init__(self, fqdn):
+        self.class_fqdn = fqdn
+
+    def __call__(self, orig):
+
+        def private_impl(that_object, *args, **kw_args):
+            that_class = '%s.%s' % (that_object.__class__.__module__,
+                that_object.__class__.__name__)
+            if that_class != self.class_fqdn:
+                raise TypeError('Private method %s.%s is called by %s' % (
+                    self.class_fqdn, orig.func_name, that_class))
+            return orig(that_object, *args, **kw_args)
+
+        return private_impl

File intfprgm/tests.py

 
 import unittest
 
-from intfprgm import interface, abstract, concrete, overrides
+from intfprgm import interface, abstract, concrete, overrides, private
 
 
 class InterfaceTest(unittest.TestCase):
         overrides(Derived_2)
 
 
+class PrivateTest(unittest.TestCase):
+
+    def test_private_okay(self):
+        class Super(object):
+            @private('__main__.Super')
+            def method(self):
+                return 'Super'
+        try:
+            self.assertEqual('Super', Super().method())
+        except TypeError:
+            pass
+
+    def test_private_fail(self):
+        class Super(object):
+            @private('Super')
+            def method(self):
+                return 'Super'
+        self.assertRaises(TypeError, Super().method)
+
+
 if __name__ == '__main__':
     unittest.main()