Trellis patch (py2.6, -OO, Component.__cells__ undo)

Sergey Schetinin <[email protected]> Sat, 4 Apr 2009 15:34:06 +0300
Newsgroups gmane.comp.python.peak
Message-ID <[email protected]>
I've just noticed that some of the bugfixes from last year were
commited this February. Great!
I submit a small patch that
1. makes Trellis import on py2.6 without warning
2. makes Trellis work when running in -OO mode (docstring)
3. undo-logs registration of new cells in Cells(comp)
The last one would need more work once multithreading is supported,
because now setdefault is not enough to ensure thread-safety.
(Rollback in one of the threads can remove cells created by another.)

I'm attaching the test as well.



-- 
Best Regards,
Sergey Schetinin

http://s3bk.com/ -- S3 Backup
http://word-to-html.com/ -- Word to HTML Converter

_______________________________________________
PEAK mailing list
[email protected]
http://www.eby-sarna.com/mailman/listinfo/peak
my.patch (application/octet-stream, 2 KB)
Index: trellis.py
===================================================================
--- trellis.py	(revision 2599)
+++ trellis.py	(working copy)
@@ -1,10 +1,22 @@
 from thread import get_ident
 from weakref import ref
 from peak.util import addons, decorators
-import sys, UserDict, UserList, sets, stm, types, new, weakref, copy
+import sys, UserDict, UserList, stm, types, new, weakref, copy
 from peak.util.extremes import Max
 from peak.util.symbols import Symbol, NOT_GIVEN

+if sys.version >= '2.6':
+    import warnings
+    _filter = ('ignore', None, DeprecationWarning, None, 13) # lineno for 'import sets'
+    warnings.filters.append(_filter)
+try:
+    import sets
+finally:
+    if sys.version >= '2.6':
+        if warnings.filters.pop(-1) is not _filter:
+            raise AssertionError
+
+
 __all__ = [
     'Cell', 'Constant', 'make', 'todo', 'todos', 'modifier',
     'Component', 'repeat', 'poll', 'InputConflict',
@@ -292,8 +308,8 @@
     a rule, reads performed in the function will not become dependencies of the
     caller.
     """
-    def wrap(__func, __module):
-        """
+    def wrap(__func, __module): pass
+    wrap.__doc__ = """
         if not __module.ctrl.active:
             return __module.atomically(__func, $args)
         elif __module.ctrl.current_listener is None:
@@ -748,6 +763,9 @@
         except KeyError:
             name = self.__name__
             cell = cells.setdefault(name, self.make_cell(typ, ob, name))
+            if ctrl.active:
+                on_undo(cells.pop, name)
         return cell.value

     def __repr__(self):
@@ -1240,6 +1258,9 @@
     strategy is used in each recalcultion that changes the list.  If what you
     really want is e.g. a sorted read-only view on a set, don't use this.
     """
+    if hasattr(UserList.UserList, '__metaclass__'):
+        class __metaclass__(Component.__metaclass__, UserList.UserList.__metaclass__):
+            pass

     updated = todo(lambda self: self.data[:])
     future  = updated.future
test-newcell-undo.py (application/octet-stream, 525 B)
from peak.events.trellis import *

class C(Component):
   x = attr(0)
   def __init__(self):
       self.x = 10
       @on_undo
       def undo():
           print 'UNDO'


class TC(Component):
   c = make(C)
   x = attr(0)
   y = attr(0)

   @maintain
   def test(self):
       if self.x:
           self.c
           self.y

   @maintain
   def write(self):
       self.y = self.x


tc = TC()
tc.__cells__['test'].layer = tc.__cells__['write'].layer - 1

@atomically
def test():
   tc.x = 1
   tc.test

assert tc.c.x == 10