[Zope3-checkins] CVS: Zope3/lib/python/ZODB - Connection.py:1.73 FileStorage.py:1.95 POSException.py:1.14 TmpStore.py:1.10

Jeremy Hylton jeremy@zope.com
Thu, 25 Jul 2002 16:56:57 -0400


Update of /cvs-repository/Zope3/lib/python/ZODB
In directory cvs.zope.org:/tmp/cvs-serv12872/ZODB

Modified Files:
	Connection.py FileStorage.py POSException.py TmpStore.py 
Log Message:
Implement Rollback() for Connection.

XXX Not sure if it should be possible to rollback more than once.

Add RollbackError to POSException and do a lot of reformatting there.

In testTransaction(), make sure any current transaction is aborted
when the test ends.





=== Zope3/lib/python/ZODB/Connection.py 1.72 => 1.73 ===
 class Rollback:
     """Rollback changes associated with savepoint"""
 
-    # XXX This doesn't work yet.
+    # In order to rollback changes for a savepoint(), we must remove
+    # the logged changes from the TmpStore and invalidate any object
+    # that has been changed since the rolledback transaction started.
 
-    # It needs to invalidate objects modified after the previous
-    # savepoint or the start of the transaction if it is the first
-    # savepoint.
+    # XXX Should it be possible to rollback() to the same savepoint
+    # more than once?
 
     def __init__(self, conn, tmp_undo):
         self._conn = conn
@@ -660,7 +661,7 @@
 
     def rollback(self):
         if not self._tmp_undo.current(self._conn._storage):
-            # need better error
-            raise RuntimeError, "savepoint has already been committed"
+            msg = "savepoint has already been committed"
+            raise POSException.RollbackError(msg)
         self._tmp_undo.rollback()
-        
+        self._conn._cache.invalidateMany(self._conn._modified)


=== Zope3/lib/python/ZODB/FileStorage.py 1.94 => 1.95 ===
         self._file.close()
         if hasattr(self,'_lock_file'):
             self._lock_file.close()
-        if self._tfile:
+        if self._tfile is not None:
             self._tfile.close()
         try:
             self._save_index()
@@ -802,8 +802,8 @@
             self._tfile.seek(0)
 
     def _begin(self, tid, u, d, e):
-        self._thl=TRANS_HDR_LEN+len(u)+len(d)+len(e)
-        self._nextpos=0
+        self._thl = TRANS_HDR_LEN + len(u) + len(d) + len(e)
+        self._nextpos = 0
 
     def tpc_vote(self, transaction):
         self._lock_acquire()


=== Zope3/lib/python/ZODB/POSException.py 1.13 => 1.14 ===
 $Id$
 """
 
-import Transaction.Exceptions
-from Transaction.Exceptions import TransactionError
+from Transaction.Exceptions \
+     import TransactionError, RollbackError, ConflictError as _ConflictError
 
-from string import join
 from types import StringType, DictType
 from ZODB import utils
 
-class POSError(Exception):
-    """Persistent object system error
-    """
+class POSError(StandardError):
+    """Persistent object system error."""
 
 class POSKeyError(KeyError, POSError):
-    """Key not found in database
-    """
+    """Key not found in database."""
 
     def __str__(self):
         return "%016x" % utils.U64(self.args[0])
 
-class ConflictError(Transaction.Exceptions.ConflictError):
-    """Two transactions tried to modify the same object at once.  This
-    transaction should be resubmitted.
+class ConflictError(_ConflictError):
+    """Two transactions tried to modify the same object at once.
+
+    This transaction should be resubmitted.
 
     Instance attributes:
       oid : string
@@ -107,9 +105,10 @@
 
 
 class ReadConflictError(ConflictError):
-    """A conflict detected at read time -- attempt to read an object
-    that has changed in another transaction (eg. another thread
-    or process).
+    """Conflict detected when object was loaded.
+
+    There was an attempt to read an object that has changed in another
+    transaction (eg. another thread or process).
     """
     def __init__(self, message=None, object=None, serials=None):
         if message is None:
@@ -118,23 +117,21 @@
                                serials=serials)
 
 class VersionError(POSError):
-    """An error in handling versions occurred
-    """
+    """An error in handling versions occurred."""
 
 class VersionCommitError(VersionError):
-    """An invalid combination of versions was used in a version commit
-    """
+    """An invalid combination of versions was used in a version commit."""
 
 class VersionLockError(VersionError, TransactionError):
-    """An attempt was made to modify an object that has
-    been modified in an unsaved version"""
+    """Can't modify an object that is modified in unsaved version."""
 
 class UndoError(POSError):
-    """An attempt was made to undo a non-undoable transaction.
-    """
+    """An attempt was made to undo a non-undoable transaction."""
+    
     def __init__(self, *reason):
-        if len(reason) == 1: reason=reason[0]
-        self.__reason=reason
+        if len(reason) == 1:
+            reason = reason[0]
+        self.__reason = reason
 
     def __repr__(self):
         reason=self.__reason
@@ -149,7 +146,7 @@
             else:
                 r.append("Couldn't undo change to %s" % (`oid`))
 
-        return join(r,'\n')
+        return "\n".join(r)
 
     __str__=__repr__
 
@@ -157,27 +154,20 @@
     pass
 
 class StorageTransactionError(StorageError):
-    """An operation was invoked for an invalid transaction or state
-    """
+    """An operation was invoked for an invalid transaction or state."""
 
 class StorageSystemError(StorageError):
-    """Panic! Internal storage error!
-    """
+    """Panic! Internal storage error!"""
 
 class MountedStorageError(StorageError):
-    """Unable to access mounted storage.
-    """
+    """Unable to access mounted storage."""
 
 class ReadOnlyError(StorageError):
-    """Unable to modify objects in a read-only storage.
-    """
+    """Unable to modify objects in a read-only storage."""
 
 class ExportError(POSError):
-    """An export file doesn't have the right format.
-    """
-    pass
+    """An export file doesn't have the right format."""
 
 class Unsupported(POSError):
-    """An feature that is unsupported bt the storage was used.
-    """
+    """An feature that is unsupported bt the storage was used."""
     


=== Zope3/lib/python/ZODB/TmpStore.py 1.9 => 1.10 ===
         self._file.seek(pos)
         h = self._file.read(24)
         if h[:8] != oid:
-            raise POSException.StorageSystemError, 'Bad temporary storage'
+            raise POSException.StorageSystemError('Bad temporary storage')
         size = u64(h[16:])
         serial = h[8:16]
         return self._file.read(size), serial
@@ -120,9 +120,9 @@
             return len(self._index)
 
     def rollback(self, pos, index):
-        if not (pos < self._tpos <= self._pos):
-            # XXX need to make this pos exception
-            raise RuntimeError("transaction rolled back to early point")
+        if not (pos <= self._tpos <= self._pos):
+            msg = "transaction rolled back to early point"
+            raise POSException.RollbackError(msg)
         self._tpos = self._pos = pos
         self._index = index
         self._tindex.clear()