Rev 5295: (mbp) add ObjectWithCleanups (Martin Pool) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/
Canonical.com Patch Queue Manager
pqm at pqm.ubuntu.com
Tue Jun 15 08:06:46 BST 2010
At file:///home/pqm/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 5295 [merge]
revision-id: pqm at pqm.ubuntu.com-20100615070643-igufk0cmdpkg4ypc
parent: pqm at pqm.ubuntu.com-20100615043249-b5xx4yeyfyfbfyav
parent: mbp at sourcefrog.net-20100427072911-hagcu863rqbu4mal
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2010-06-15 08:06:43 +0100
message:
(mbp) add ObjectWithCleanups (Martin Pool)
modified:
bzrlib/cleanup.py cleanup.py-20090922032110-mv6i6y8t04oon9np-1
bzrlib/tests/test_cleanup.py test_cleanup.py-20090922032110-mv6i6y8t04oon9np-2
=== modified file 'bzrlib/cleanup.py'
--- a/bzrlib/cleanup.py 2010-03-03 22:59:21 +0000
+++ b/bzrlib/cleanup.py 2010-04-27 07:29:11 +0000
@@ -78,7 +78,28 @@
_run_cleanup(func, *args, **kwargs)
-class OperationWithCleanups(object):
+class ObjectWithCleanups(object):
+ """A mixin for objects that hold a cleanup list.
+
+ Subclass or client code can call add_cleanup and then later `cleanup_now`.
+ """
+ def __init__(self):
+ self.cleanups = deque()
+
+ def add_cleanup(self, cleanup_func, *args, **kwargs):
+ """Add a cleanup to run.
+
+ Cleanups may be added at any time.
+ Cleanups will be executed in LIFO order.
+ """
+ self.cleanups.appendleft((cleanup_func, args, kwargs))
+
+ def cleanup_now(self):
+ _run_cleanups(self.cleanups)
+ self.cleanups.clear()
+
+
+class OperationWithCleanups(ObjectWithCleanups):
"""A way to run some code with a dynamic cleanup list.
This provides a way to add cleanups while the function-with-cleanups is
@@ -102,16 +123,8 @@
"""
def __init__(self, func):
+ super(OperationWithCleanups, self).__init__()
self.func = func
- self.cleanups = deque()
-
- def add_cleanup(self, cleanup_func, *args, **kwargs):
- """Add a cleanup to run.
-
- Cleanups may be added at any time before or during the execution of
- self.func. Cleanups will be executed in LIFO order.
- """
- self.cleanups.appendleft((cleanup_func, args, kwargs))
def run(self, *args, **kwargs):
return _do_with_cleanups(
@@ -121,10 +134,6 @@
return _do_with_cleanups(
self.cleanups, self.func, *args, **kwargs)
- def cleanup_now(self):
- _run_cleanups(self.cleanups)
- self.cleanups.clear()
-
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
"""Run `func`, then call all the cleanup_funcs.
=== modified file 'bzrlib/tests/test_cleanup.py'
--- a/bzrlib/tests/test_cleanup.py 2010-03-03 22:59:21 +0000
+++ b/bzrlib/tests/test_cleanup.py 2010-04-27 07:29:11 +0000
@@ -20,6 +20,7 @@
from bzrlib.cleanup import (
_do_with_cleanups,
_run_cleanup,
+ ObjectWithCleanups,
OperationWithCleanups,
)
from bzrlib.tests import TestCase
@@ -276,3 +277,17 @@
[('func called', 'foo'), 'cleanup 1', 'cleanup 2', 'cleanup 3',
'cleanup 4'], call_log)
+
+class SampleWithCleanups(ObjectWithCleanups):
+
+ pass
+
+
+class TestObjectWithCleanups(TestCase):
+
+ def test_object_with_cleanups(self):
+ a = []
+ s = SampleWithCleanups()
+ s.add_cleanup(a.append, 42)
+ s.cleanup_now()
+ self.assertEqual(a, [42])
More information about the bazaar-commits
mailing list