[Checkins] SVN: grok/trunk/src/grok/ make sure that grok.context only accepts interfaces or classes

Philipp von Weitershausen philikon at philikon.de
Sun Oct 15 06:49:17 EDT 2006


Log message for revision 70635:
  make sure that grok.context only accepts interfaces or classes
  

Changed:
  U   grok/trunk/src/grok/_grok.py
  U   grok/trunk/src/grok/adapter.txt
  A   grok/trunk/src/grok/tests/classorinterface.py

-=-
Modified: grok/trunk/src/grok/_grok.py
===================================================================
--- grok/trunk/src/grok/_grok.py	2006-10-15 10:35:26 UTC (rev 70634)
+++ grok/trunk/src/grok/_grok.py	2006-10-15 10:49:16 UTC (rev 70635)
@@ -17,6 +17,7 @@
 import sys
 from zope.dottedname.resolve import resolve
 from zope import component
+from zope.interface.interfaces import IInterface
 
 class Model(object):
     pass
@@ -29,8 +30,12 @@
 class GrokError(Exception):
     pass
 
+def isclass(obj):
+    """We cannot use ``inspect.isclass`` because it will return True for interfaces"""
+    return type(obj) in (types.ClassType, type)
+
 def check_subclass(obj, class_):
-    if type(obj) not in (types.ClassType, type):
+    if not isclass(obj):
         return False
     return issubclass(obj, class_)
 
@@ -67,6 +72,10 @@
         component.provideAdapter(factory, adapts=(adapter_context,))
 
 def context(obj):
+    if not (IInterface.providedBy(obj) or isclass(obj)):
+        raise GrokError("You can only pass classes or interfaces to "
+                        "grok.context.")
+
     frame = sys._getframe(1)
 
     is_module = frame.f_locals is frame.f_globals

Modified: grok/trunk/src/grok/adapter.txt
===================================================================
--- grok/trunk/src/grok/adapter.txt	2006-10-15 10:35:26 UTC (rev 70634)
+++ grok/trunk/src/grok/adapter.txt	2006-10-15 10:49:16 UTC (rev 70635)
@@ -254,3 +254,31 @@
 Clean up:
 
   >>> cleanUp()
+
+You can only use `grok.context` with interfaces or classes and not
+with anything else:
+
+  >>> from grok.tests import classorinterface
+  >>> classorinterface.function_context()
+  Traceback (most recent call last):
+    ...
+  GrokError: You can only pass classes or interfaces to grok.context.
+
+  >>> classorinterface.string_context()
+  Traceback (most recent call last):
+    ...
+  GrokError: You can only pass classes or interfaces to grok.context.
+
+  >>> classorinterface.module_context()
+  Traceback (most recent call last):
+    ...
+  GrokError: You can only pass classes or interfaces to grok.context.
+
+  >>> classorinterface.instance_context()
+  Traceback (most recent call last):
+    ...
+  GrokError: You can only pass classes or interfaces to grok.context.
+
+Clean up:
+
+  >>> cleanUp()

Added: grok/trunk/src/grok/tests/classorinterface.py
===================================================================
--- grok/trunk/src/grok/tests/classorinterface.py	2006-10-15 10:35:26 UTC (rev 70634)
+++ grok/trunk/src/grok/tests/classorinterface.py	2006-10-15 10:49:16 UTC (rev 70635)
@@ -0,0 +1,24 @@
+"""
+All of the below examples should fail
+"""
+import grok
+
+def function_context():
+    def a():
+        pass
+
+    class FunctionContext(object):
+        grok.context(a)
+
+def string_context():
+    class StringContext(object):
+        grok.context('string')
+
+def module_context():
+    class ModuleContext(object):
+        grok.context(grok)
+
+def instance_context():
+    obj = object()
+    class InstanceContext(object):
+        grok.context(obj)


Property changes on: grok/trunk/src/grok/tests/classorinterface.py
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list