[Checkins] SVN: martian/branches/jw-philipp-using-ndir-directives/src/martian/ Instead of ignoring names starting with __grok_, now ignore names with '.' in them. Because that's what the new-style directives store their data under.

Philipp von Weitershausen philikon at philikon.de
Sat May 3 12:51:35 EDT 2008


Log message for revision 86256:
  Instead of ignoring names starting with __grok_, now ignore names with '.' in them. Because that's what the new-style directives store their data under.

Changed:
  U   martian/branches/jw-philipp-using-ndir-directives/src/martian/core.py
  A   martian/branches/jw-philipp-using-ndir-directives/src/martian/core.txt
  U   martian/branches/jw-philipp-using-ndir-directives/src/martian/tests/test_all.py

-=-
Modified: martian/branches/jw-philipp-using-ndir-directives/src/martian/core.py
===================================================================
--- martian/branches/jw-philipp-using-ndir-directives/src/martian/core.py	2008-05-03 16:50:59 UTC (rev 86255)
+++ martian/branches/jw-philipp-using-ndir-directives/src/martian/core.py	2008-05-03 16:51:35 UTC (rev 86256)
@@ -85,7 +85,10 @@
 
         # try to grok everything in module
         for name in dir(module):
-            if name.startswith('__grok_'):
+            if '.' in name:
+                # This must be a module-level variable that couldn't
+                # have been set by the developer.  It must have been a
+                # module-level directive.
                 continue
             obj = getattr(module, name)
             if not util.defined_locally(obj, module.__name__):

Added: martian/branches/jw-philipp-using-ndir-directives/src/martian/core.txt
===================================================================
--- martian/branches/jw-philipp-using-ndir-directives/src/martian/core.txt	                        (rev 0)
+++ martian/branches/jw-philipp-using-ndir-directives/src/martian/core.txt	2008-05-03 16:51:35 UTC (rev 86256)
@@ -0,0 +1,47 @@
+(Edge-case) tests of Martian core components
+============================================
+
+ModuleGrokker ignores values set by directives
+----------------------------------------------
+
+Consider the following module-level directive:
+
+  >>> import martian
+  >>> class store(martian.Directive):
+  ...     scope = martian.MODULE
+  ...     store = martian.ONCE
+  ...
+  >>> store.__module__ = 'somethingelse'  # just so that it isn't __builtin__
+
+Now let's look at a module that contains a simple function and a call
+to the directive defined above:
+
+  >>> class module_with_directive(FakeModule):
+  ...     fake_module = True
+  ...
+  ...     def some_function():
+  ...         return 11
+  ...
+  ...     store(some_function)
+  ...
+  >>> module_with_directive = fake_import(module_with_directive)
+
+Now imagine we have the following grokker for functions:
+
+  >>> import types
+  >>> class FunctionGrokker(martian.InstanceGrokker):
+  ...     component_class = types.FunctionType
+  ...     def grok(self, name, obj, **kw):
+  ...         print name, obj()
+  ...         return True
+  ...
+  >>> module_grokker = martian.ModuleGrokker()
+  >>> module_grokker.register(FunctionGrokker())
+
+and let it loose on the module, we see that it will only find functions
+set by regular variable assignment, not the ones stored by the
+directive:
+
+  >>> module_grokker.grok('module_with_directive', module_with_directive)
+  some_function 11
+  True


Property changes on: martian/branches/jw-philipp-using-ndir-directives/src/martian/core.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: martian/branches/jw-philipp-using-ndir-directives/src/martian/tests/test_all.py
===================================================================
--- martian/branches/jw-philipp-using-ndir-directives/src/martian/tests/test_all.py	2008-05-03 16:50:59 UTC (rev 86255)
+++ martian/branches/jw-philipp-using-ndir-directives/src/martian/tests/test_all.py	2008-05-03 16:51:35 UTC (rev 86256)
@@ -72,5 +72,9 @@
                              package='martian',
                              globs=globs,
                              optionflags=optionflags),
+        doctest.DocFileSuite('core.txt',
+                             package='martian',
+                             globs=globs,
+                             optionflags=optionflags),
         ])
     return suite



More information about the Checkins mailing list