[PATCH 1 of 5] transaction: ensure finished transactions are not reused
Henrik Stuart
hg at hstuart.dk
Fri Apr 24 04:42:33 CDT 2009
# HG changeset patch
# User Henrik Stuart <hg at hstuart.dk>
# Date 1240559813 -7200
# Node ID d8c0013119cfaf73848aeb75169464aa59200cde
# Parent ddbee2d0d634b67d7f6ebea405398c171bbf3a8a
transaction: ensure finished transactions are not reused
All transactional methods on the transaction class have had a decorator
added that ensures the transaction is running.
Co-contributor: Sune Foldager <cryo at cyanite.org>
diff -r ddbee2d0d634 -r d8c0013119cf mercurial/transaction.py
--- a/mercurial/transaction.py Fri Apr 24 10:43:12 2009 +0200
+++ b/mercurial/transaction.py Fri Apr 24 09:56:53 2009 +0200
@@ -13,6 +13,16 @@
from i18n import _
import os, errno
+import error
+
+def active(func):
+ def _active(*args, **kwds):
+ self = args[0]
+ if self.count == 0:
+ raise error.Abort(_(
+ 'cannot use transaction when it is already committed/aborted'))
+ return func(*args, **kwds)
+ return _active
class transaction(object):
def __init__(self, report, opener, journal, after=None, createmode=None):
@@ -32,9 +42,10 @@
def __del__(self):
if self.journal:
- if self.entries: self.abort()
+ if self.entries: self._abort()
self.file.close()
+ @active
def add(self, file, offset, data=None):
if file in self.map: return
self.entries.append((file, offset, data))
@@ -43,11 +54,13 @@
self.file.write("%s\0%d\n" % (file, offset))
self.file.flush()
+ @active
def find(self, file):
if file in self.map:
return self.entries[self.map[file]]
return None
+ @active
def replace(self, file, offset, data=None):
if file not in self.map:
raise KeyError(file)
@@ -56,6 +69,7 @@
self.file.write("%s\0%d\n" % (file, offset))
self.file.flush()
+ @active
def nest(self):
self.count += 1
return self
@@ -63,6 +77,7 @@
def running(self):
return self.count > 0
+ @active
def close(self):
self.count -= 1
if self.count != 0:
@@ -75,7 +90,11 @@
os.unlink(self.journal)
self.journal = None
+ @active
def abort(self):
+ self._abort()
+
+ def _abort(self):
if not self.entries: return
self.report(_("transaction abort!\n"))
More information about the Mercurial-devel
mailing list