~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/cleanup.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-11 11:47:36 UTC
  • mfrom: (5200.3.8 lock_return)
  • Revision ID: pqm@pqm.ubuntu.com-20100511114736-mc1sq9zyo3vufec7
(lifeless) Provide a consistent interface to Tree, Branch,
 Repository where lock methods return an object with an unlock method to
 unlock the lock. This breaks the API for Branch,
 Repository on their lock_write methods. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
        _run_cleanup(func, *args, **kwargs)
79
79
 
80
80
 
81
 
class ObjectWithCleanups(object):
82
 
    """A mixin for objects that hold a cleanup list.
83
 
 
84
 
    Subclass or client code can call add_cleanup and then later `cleanup_now`.
85
 
    """
86
 
    def __init__(self):
87
 
        self.cleanups = deque()
88
 
 
89
 
    def add_cleanup(self, cleanup_func, *args, **kwargs):
90
 
        """Add a cleanup to run.
91
 
 
92
 
        Cleanups may be added at any time.  
93
 
        Cleanups will be executed in LIFO order.
94
 
        """
95
 
        self.cleanups.appendleft((cleanup_func, args, kwargs))
96
 
 
97
 
    def cleanup_now(self):
98
 
        _run_cleanups(self.cleanups)
99
 
        self.cleanups.clear()
100
 
 
101
 
 
102
 
class OperationWithCleanups(ObjectWithCleanups):
 
81
class OperationWithCleanups(object):
103
82
    """A way to run some code with a dynamic cleanup list.
104
83
 
105
84
    This provides a way to add cleanups while the function-with-cleanups is
123
102
    """
124
103
 
125
104
    def __init__(self, func):
126
 
        super(OperationWithCleanups, self).__init__()
127
105
        self.func = func
 
106
        self.cleanups = deque()
 
107
 
 
108
    def add_cleanup(self, cleanup_func, *args, **kwargs):
 
109
        """Add a cleanup to run.
 
110
 
 
111
        Cleanups may be added at any time before or during the execution of
 
112
        self.func.  Cleanups will be executed in LIFO order.
 
113
        """
 
114
        self.cleanups.appendleft((cleanup_func, args, kwargs))
128
115
 
129
116
    def run(self, *args, **kwargs):
130
117
        return _do_with_cleanups(
134
121
        return _do_with_cleanups(
135
122
            self.cleanups, self.func, *args, **kwargs)
136
123
 
 
124
    def cleanup_now(self):
 
125
        _run_cleanups(self.cleanups)
 
126
        self.cleanups.clear()
 
127
 
137
128
 
138
129
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
139
130
    """Run `func`, then call all the cleanup_funcs.
188
179
                # but don't propagate them.
189
180
                _run_cleanup(cleanup, *c_args, **kwargs)
190
181
        if exc_info is not None:
191
 
            try:
192
 
                raise exc_info[0], exc_info[1], exc_info[2]
193
 
            finally:
194
 
                del exc_info
 
182
            raise exc_info[0], exc_info[1], exc_info[2]
195
183
        # No error, so we can return the result
196
184
        return result
197
185