[PATCH 2 of 6] transaction: ensure finished transactions are not reused
Henrik Stuart
hg at hstuart.dk
Tue Apr 21 17:33:21 UTC 2009
# HG changeset patch
# User Henrik Stuart <hg at hstuart.dk>
# Date 1240219102 -7200
# Node ID ba84ff05efde9ca3cecc2106f8aa5afc53bbf353
# Parent 092a6d08bc115c771c1c51947cc463b34f5fe097
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 092a6d08bc11 -r ba84ff05efde mercurial/transaction.py
--- a/mercurial/transaction.py Mon Apr 20 11:15:38 2009 +0200
+++ b/mercurial/transaction.py Mon Apr 20 11:18:22 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))
@@ -48,11 +59,13 @@
# ensure journal is synced to file system
os.fsync(self.file.fileno())
+ @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)
@@ -60,6 +73,7 @@
self.entries[index] = (file, offset, data)
self._write("%s\0%d\n" % (file, offset))
+ @active
def nest(self):
self.count += 1
return self
@@ -67,6 +81,7 @@
def running(self):
return self.count > 0
+ @active
def close(self):
self.count -= 1
if self.count != 0:
@@ -79,7 +94,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