[Checkins] SVN: grok/trunk/src/grok/ grok.context can't be called
from function or method
Philipp von Weitershausen
philikon at philikon.de
Sat Oct 14 15:14:37 EDT 2006
Log message for revision 70629:
grok.context can't be called from function or method
Changed:
U grok/trunk/src/grok/_grok.py
U grok/trunk/src/grok/adapter.txt
A grok/trunk/src/grok/tests/functioncontext.py
-=-
Modified: grok/trunk/src/grok/_grok.py
===================================================================
--- grok/trunk/src/grok/_grok.py 2006-10-14 18:47:41 UTC (rev 70628)
+++ grok/trunk/src/grok/_grok.py 2006-10-14 19:14:37 UTC (rev 70629)
@@ -66,11 +66,15 @@
raise GrokError("Ambiguous contexts, please use grok.context.")
component.provideAdapter(factory, adapts=(adapter_context,))
-def context(obj):
- locals = sys._getframe(1).f_locals
+def context(obj):
+ frame = sys._getframe(1)
- if '__grok_context__' in locals:
+ is_module = frame.f_locals is frame.f_globals
+ is_class = '__module__' in frame.f_locals
+ if not (is_module or is_class):
+ raise GrokError("grok.context can only be used on class or module level.")
+
+ if '__grok_context__' in frame.f_locals:
raise GrokError("grok.context can only be called once per class "
"or module.")
-
- locals['__grok_context__'] = obj
+ frame.f_locals['__grok_context__'] = obj
Modified: grok/trunk/src/grok/adapter.txt
===================================================================
--- grok/trunk/src/grok/adapter.txt 2006-10-14 18:47:41 UTC (rev 70628)
+++ grok/trunk/src/grok/adapter.txt 2006-10-14 19:14:37 UTC (rev 70629)
@@ -179,3 +179,22 @@
Traceback (most recent call last):
...
GrokError: grok.context can only be called once per class or module.
+
+Clean up:
+
+ >>> cleanUp()
+
+You can't call grok.context from a function:
+
+ >>> from grok.tests.functioncontext import func, SomeClass
+ >>> func()
+ Traceback (most recent call last):
+ ...
+ GrokError: grok.context can only be used on class or module level.
+
+You can't call grok.context from a method either:
+
+ >>> SomeClass().meth()
+ Traceback (most recent call last):
+ ...
+ GrokError: grok.context can only be used on class or module level.
Added: grok/trunk/src/grok/tests/functioncontext.py
===================================================================
--- grok/trunk/src/grok/tests/functioncontext.py 2006-10-14 18:47:41 UTC (rev 70628)
+++ grok/trunk/src/grok/tests/functioncontext.py 2006-10-14 19:14:37 UTC (rev 70629)
@@ -0,0 +1,14 @@
+import grok
+from grok.tests.adapter import Cave
+
+def func():
+ """We don't allow calling `grok.context` from anything else than a
+ module or a class"""
+ grok.context(Cave)
+
+class SomeClass(object):
+
+ def meth(self):
+ """We don't allow calling `grok.context` from anything else
+ than a module or a class"""
+ grok.context(Cave)
Property changes on: grok/trunk/src/grok/tests/functioncontext.py
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the Checkins
mailing list