[Checkins] SVN: grok/trunk/martian/src/martian/ Make sure of old-style class support.

Martijn Faassen faassen at infrae.com
Tue May 1 16:37:22 EDT 2007


Log message for revision 74972:
  Make sure of old-style class support.
  

Changed:
  U   grok/trunk/martian/src/martian/README.txt
  U   grok/trunk/martian/src/martian/core.py
  U   grok/trunk/martian/src/martian/tests/test_all.py

-=-
Modified: grok/trunk/martian/src/martian/README.txt
===================================================================
--- grok/trunk/martian/src/martian/README.txt	2007-05-01 20:08:13 UTC (rev 74971)
+++ grok/trunk/martian/src/martian/README.txt	2007-05-01 20:37:22 UTC (rev 74972)
@@ -207,7 +207,7 @@
   >>> module_martian = ModuleMartian(filetype_martian)
 
 Note that directly putting a martian into a ``ModuleMartian`` is
-typically not recommended - typically you would put in a multi martian
+typically not recommended - normally you would put in a multi martian
 (see the examples for multi martians). We can do it here as the only
 thing we want to grok are functions and other objects are rejected on
 a name basis.
@@ -661,3 +661,50 @@
 GlobalMartian
 -------------
 
+Old-style class support
+-----------------------
+
+So far we have only grokked either new-style classes or instances of
+new-style classes. It is also possible to grok old-style classes and
+their instances::
+
+  >>> class oldstyle(FakeModule):
+  ...   class Machine:
+  ...     pass
+  ...   all_machines = {}
+  ...   all_machine_instances = {}
+  >>> oldstyle = fake_import(oldstyle)
+
+Let's make a martian for the old style class::
+
+  >>> class MachineMartian(ClassMartian):
+  ...   component_class = oldstyle.Machine
+  ...   def grok(self, name, obj):
+  ...     oldstyle.all_machines[name] = obj
+  ...     return True
+
+And another martian for old style instances::
+
+  >>> class MachineInstanceMartian(InstanceMartian):
+  ...   component_class = oldstyle.Machine
+  ...   def grok(self, name, obj):
+  ...     oldstyle.all_machine_instances[name] = obj
+  ...     return True
+
+The multi martian should succesfully grok the old-style ``Machine`` class
+and instances of it::
+
+  >>> multi = MultiMartian()
+  >>> multi.register(MachineMartian())
+  >>> multi.register(MachineInstanceMartian())
+  >>> class Robot(oldstyle.Machine):
+  ...   pass
+  >>> multi.grok('Robot', Robot)
+  True
+  >>> oldstyle.all_machines.keys()
+  ['Robot']
+  >>> robot = Robot()
+  >>> multi.grok('robot', robot)
+  True
+  >>> oldstyle.all_machine_instances.keys()
+  ['robot']

Modified: grok/trunk/martian/src/martian/core.py
===================================================================
--- grok/trunk/martian/src/martian/core.py	2007-05-01 20:08:13 UTC (rev 74971)
+++ grok/trunk/martian/src/martian/core.py	2007-05-01 20:37:22 UTC (rev 74972)
@@ -1,4 +1,4 @@
-import types
+import types, inspect
 
 from zope.interface import implements
 
@@ -66,31 +66,19 @@
     
 class MultiInstanceMartian(MultiMartianBase):
     def get_bases(self, obj):
-        # XXX how to work with old-style classes?
-        return obj.__class__.__mro__
+        return inspect.getmro(obj.__class__)
 
-class MultiOldStyleInstanceMartian(MultiMartianBase):
-    # XXX to be written
-    pass
-
 class MultiClassMartian(MultiMartianBase):
     def get_bases(self, obj):
-        # XXX how to work with old-style classes?
-        return obj.__mro__
-
-class MultiOldStyleClassMartian(MultiMartianBase):
-    # XXX to be written
-    pass
-
+        return inspect.getmro(obj)
+    
 class MultiMartian(MartianBase):
     implements(IMultiMartian)
-
-    # XXX extend with old-style class support
     
     def __init__(self):
         self._multi_instance_martian = MultiInstanceMartian()
         self._multi_class_martian = MultiClassMartian()
-        
+
     def register(self, martian):
         if isinstance(martian, InstanceMartian):
             self._multi_instance_martian.register(martian)
@@ -100,7 +88,7 @@
             assert 0, "Unknown type of martian: %r" % martian
 
     def grok(self, name, obj, **kw):
-        if type(obj) is type:
+        if type(obj) in (type, types.ClassType):
             return self._multi_class_martian.grok(name, obj, **kw)
         else:
             return self._multi_instance_martian.grok(name, obj, **kw)

Modified: grok/trunk/martian/src/martian/tests/test_all.py
===================================================================
--- grok/trunk/martian/src/martian/tests/test_all.py	2007-05-01 20:08:13 UTC (rev 74971)
+++ grok/trunk/martian/src/martian/tests/test_all.py	2007-05-01 20:37:22 UTC (rev 74972)
@@ -1,6 +1,6 @@
 import unittest
 from zope.testing import doctest
-import new
+import new, types
 
 class FakeModule(object):
     pass
@@ -38,7 +38,7 @@
             except AttributeError:
                 pass
         setattr(module, name, obj)
-        glob[name] = obj
+
     # provide correct globals for functions
     for name in dir(module):
         if name.startswith('__'):
@@ -46,12 +46,12 @@
         obj = getattr(module, name)
         try:
             code = obj.func_code
+            new_func = new.function(code, glob, name)
+            new_func.__module__ = module.__name__
+            setattr(module, name, new_func)
+            glob[name] = new_func
         except AttributeError:
-            continue
-        new_func = new.function(code, glob, name)
-        new_func.__module__ = module.__name__
-        setattr(module, name, new_func)
-        glob[name] = new_func
+            pass
     return module
 
 optionflags = doctest.NORMALIZE_WHITESPACE + doctest.ELLIPSIS



More information about the Checkins mailing list