[Checkins] SVN: zope.component/branches/wosc-test-stacking/src/zope/component/ experiments with persistence

Wolfgang Schnerring wosc at wosc.de
Wed Nov 16 08:12:56 UTC 2011


Log message for revision 123354:
  experiments with persistence
  

Changed:
  U   zope.component/branches/wosc-test-stacking/src/zope/component/stackable.py
  U   zope.component/branches/wosc-test-stacking/src/zope/component/tests.py

-=-
Modified: zope.component/branches/wosc-test-stacking/src/zope/component/stackable.py
===================================================================
--- zope.component/branches/wosc-test-stacking/src/zope/component/stackable.py	2011-11-15 17:52:18 UTC (rev 123353)
+++ zope.component/branches/wosc-test-stacking/src/zope/component/stackable.py	2011-11-16 08:12:53 UTC (rev 123354)
@@ -1,12 +1,13 @@
 import copy
 import inspect
+import persistent # XXX conditional import
+import persistent.list # XXX conditional import
 
 
-class StackableBase(object):
+class StackableBase(persistent.Persistent):
 
-    def __init__(self, original=None):
-        if original is not None:
-            self.stack = [original]
+    def __init__(self, original):
+        self.stack = persistent.list.PersistentList([original])
 
     def push(self):
         self.stack.append(copy.copy(self.stack[-1]))
@@ -23,9 +24,13 @@
     def __repr__(self):
         return 'stackable:%r' % self.stack[-1]
 
-    def __reduce_ex__(self, protocol):
-        return (self.__class__, (), dict(stack=self.stack))
+    # def __reduce_ex__(self, protocol):
+    #     return (self.__class__, (), dict(stack=self.stack))
 
+    # def __setstate__(self, state):
+    #     import pdb; pdb.set_trace();
+    #     super(StackableBase, self).__setstate__(state)
+
     @classmethod
     def delegate(cls, name):
         def inner(self, *args, **kw):
@@ -34,16 +39,18 @@
 
     @classmethod
     def create_for(cls, type_):
-        exclude_methods = ['__getattribute__', '__setattr__']
+        exclude_methods = ['__getattribute__']
         overridden_methods = dict(
-            inspect.getmembers(cls, predicate=inspect.ismethod)).keys()
+            inspect.getmembers(cls, predicate=cls.ismethod)).keys()
+        overridden_methods += dict(
+            inspect.getmembers(
+                persistent.Persistent, predicate=cls.ismethod)).keys()
+        # XXX should we allow copying over of __getattr__?
         exclude_methods.extend(overridden_methods)
 
         copied_methods = {}
-        # XXX ismethoddescriptor is correct for type_ in [dict, list], but
-        # probably not in general
         for name in dict(
-            inspect.getmembers(type_, predicate=inspect.ismethoddescriptor)):
+            inspect.getmembers(type_, predicate=cls.ismethod)):
             if name in exclude_methods:
                 continue
             copied_methods[name] = cls.delegate(name)
@@ -51,9 +58,14 @@
         return type(
             'Stackable%s' % type_.__name__.title(), (cls,), copied_methods)
 
+    @staticmethod
+    def ismethod(x):
+        return inspect.ismethoddescriptor(x) or inspect.ismethod(x)
 
+
 SUPPORTED_TYPES = dict(
-    (type_, StackableBase.create_for(type_)) for type_ in [dict, list])
+    (type_, StackableBase.create_for(type_)) for type_ in [
+        dict, list, persistent.list.PersistentList])
 
 for type_ in SUPPORTED_TYPES.values():
     locals()[type_.__name__] = type_

Modified: zope.component/branches/wosc-test-stacking/src/zope/component/tests.py
===================================================================
--- zope.component/branches/wosc-test-stacking/src/zope/component/tests.py	2011-11-15 17:52:18 UTC (rev 123353)
+++ zope.component/branches/wosc-test-stacking/src/zope/component/tests.py	2011-11-16 08:12:53 UTC (rev 123354)
@@ -1729,8 +1729,9 @@
         # DemoStorage instead
         tmpfile = tempfile.NamedTemporaryFile()
         db = ZODB.DB(tmpfile.name)
-        root = db.open().root()
-        orig = [1]
+        conn = db.open()
+        root = conn.root()
+        orig = persistent.list.PersistentList([1])
         stack = zope.component.stackable.stackable(orig)
         root['stack'] = stack
         zope.component.stackable.push()
@@ -1738,6 +1739,7 @@
         transaction.commit()
 
         # force deserialization
+        conn.close()
         db.close()
         db = ZODB.DB(tmpfile.name)
         root = db.open().root()
@@ -1745,6 +1747,7 @@
         stack = root['stack']
         self.assertEqual([1, 2], stack)
         zope.component.stackable.pop()
+        transaction.commit()
         self.assertEqual([1], stack)
 
 



More information about the checkins mailing list