[Zope-Checkins] CVS: Zope3/lib/python/Transaction - _defaultTransaction.py:1.1.2.7

Jeremy Hylton jeremy@zope.com
Thu, 6 Jun 2002 19:01:35 -0400


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

Modified Files:
      Tag: Zope-3x-branch
	_defaultTransaction.py 
Log Message:
Many small cleanups.

Add some whitespace.
In get_transaction() et al. rely on nested scopes instead of default args.
Change the log subsystem from ZODB to Transaction.
Make the __str__ more useful.
Use string methods instead of string module.
Remove unused import of struct.pack.


=== Zope3/lib/python/Transaction/_defaultTransaction.py 1.1.2.6 => 1.1.2.7 ===
 """Transaction management
 
-$Id$"""
-__version__='$Revision$'[11:-2]
-
+$Id$
+"""
 import sys
-from struct import pack
-from string import split, strip, join
-from zLOG import LOG, ERROR, PANIC
 
-from Exceptions import ConflictError, TransactionError
+from Transaction.Exceptions import ConflictError, TransactionError
+from zLOG import LOG, ERROR, PANIC
 
 # Flag indicating whether certain errors have occurred.
-hosed=0
+hosed = 0
 
 class Transaction:
-    'Simple transaction objects for single-threaded applications.'
-    user=''
-    description=''
-    _connections=None
-    _extension=None
-    _sub=None # This is a subtrasaction flag
+    """Simple transaction objects for single-threaded applications."""
+    
+    user = ""
+    description = ""
+    _connections = None
+    _extension = None
+    _sub = None # This is a subtrasaction flag
 
     # The _non_st_objects variable is either None or a list
     # of jars that do not support subtransactions. This is used to
     # manage non-subtransaction-supporting jars during subtransaction
     # commits and aborts to ensure that they are correctly committed
     # or aborted in the "outside" transaction.
-    _non_st_objects=None
+    _non_st_objects = None
     
     def __init__(self, id=None):
         self._id = id
@@ -47,7 +45,7 @@
 
     def _init(self):
         self._objects = []
-        self.user = self.description = ''
+        self.user = self.description = ""
         if self._connections:
             for c in self._connections.values():
                 c.close()
@@ -55,24 +53,25 @@
 
     def sub(self):
         # Create a manually managed subtransaction for internal use
-        r=self.__class__()
-        r.user=self.user
-        r.description=self.description
-        r._extension=self._extension
+        r = self.__class__()
+        r.user = self.user
+        r.description = self.description
+        r._extension = self._extension
         return r
         
-    def __str__(self): return "%.3f\t%s" % (self._id or 0, self.user)
+    def __str__(self):
+        return "Transaction %s %s" % (self._id or 0, self.user)
 
     def __del__(self):
-        if self._objects: self.abort(freeme=0)
+        if self._objects:
+            self.abort(freeme=0)
 
     def abort(self, subtransaction=0, freeme=1):
-        '''Abort the transaction.
+        """Abort the transaction.
 
-        This is called from the application.  This means that we haven\'t
+        This is called from the application.  This means that we haven't
         entered two-phase commit yet, so no tpc_ messages are sent.
-        '''
-
+        """
         if subtransaction and (self._non_st_objects is not None):
             raise TransactionError, (
                 """Attempted to abort a sub-transaction, but a participating
@@ -128,18 +127,19 @@
                 self._init()
 
     def begin(self, info=None, subtransaction=None):
-        '''Begin a new transaction.
+        """Begin a new transaction.
 
         This aborts any transaction in progres.
-        '''
-        if self._objects: self.abort(subtransaction, 0)
+        """
+        if self._objects:
+            self.abort(subtransaction, 0)
         if info:
-            info=split(info,'\t')
-            self.user=strip(info[0])
-            self.description=strip(join(info[1:],'\t'))
+            info = info.split("\t")
+            self.user = info[0].strip()
+            self.description = ("\t".join(info[1:])).strip()
 
     def commit(self, subtransaction=None):
-        'Finalize the transaction'
+        """Finalize the transaction."""
 
         global hosed
         
@@ -256,7 +256,7 @@
                         jarsv.pop() # It didn't, so it's taken care of.
                 except:
                     # Bug if it does, we need to keep track of it
-                    LOG('ZODB', ERROR,
+                    LOG("Transaction", ERROR,
                         "A storage error occurred in the last phase of a "
                         "two-phase commit.  This shouldn\'t happen. ",
                         error=sys.exc_info())
@@ -271,9 +271,9 @@
                     # Someone finished, so don't allow any more
                     # work without at least a restart!
                     hosed=1
-                    LOG('ZODB', PANIC,
+                    LOG("Transaction", PANIC,
                         "A storage error occurred in the last phase of a "
-                        "two-phase commit.  This shouldn\'t happen. "
+                        "two-phase commit.  This shouldn't happen. "
                         "The application may be in a hosed state, so "
                         "transactions will not be allowed to commit "
                         "until the site/storage is reset by a restart. ",
@@ -281,7 +281,7 @@
                     raise
                 
             except:
-                t,v,tb=sys.exc_info()
+                t, v, tb = sys.exc_info()
 
                 # Ugh, we got an got an error during commit, so we
                 # have to clean up.
@@ -299,9 +299,9 @@
                 for j in jarsv:
                     try: j.tpc_abort(self) # This should never fail
                     except:     
-                        LOG('ZODB', ERROR,
+                        LOG("Transaction", ERROR,
                             "A storage error occured during object abort "
-                            "This shouldn\'t happen. ",
+                            "This shouldn't happen. ",
                             error=sys.exc_info())
                 
                 # Ugh, we need to abort work done in sub-transactions.
@@ -312,28 +312,29 @@
                 raise t,v,tb
 
         finally:
-            tb=None
+            tb = None
             del objects[:] # clear registered
-            if not subtransaction and self._id is not None: free_transaction()
+            if not subtransaction and self._id is not None:
+                free_transaction()
 
     def register(self, object):
-        'Register the given object for transaction control.'
+        """Register the given object for transaction control."""
+
         self._objects.append(object)
 
     def note(self, text):
         if self.description:
-            self.description = "%s\n\n%s" % (self.description, strip(text))
+            self.description = "%s\n\n%s" % (self.description, text.strip())
         else: 
-            self.description = strip(text)
+            self.description = text.strip()
     
     def setUser(self, user_name, path='/'):
-        self.user="%s %s" % (path, user_name)
+        self.user = "%s %s" % (path, user_name)
 
     def setExtendedInfo(self, name, value):
-        ext=self._extension
-        if ext is None:
-            ext=self._extension={}
-        ext[name]=value
+        if self._extension is None:
+            self._extension = {}
+        self._extension[name] = value
 
 
 ############################################################################
@@ -341,25 +342,29 @@
 
 try:
     import thread
-
 except:
-    _t=Transaction(None)
-    def get_transaction(_t=_t): return _t
-    def free_transaction(_t=_t): _t.__init__()
-
+    _t = Transaction(None)
+    
+    def get_transaction():
+        return _t
+    
+    def free_transaction():
+        _t.__init__()
+        
 else:
-    _t={}
-    def get_transaction(_id=thread.get_ident, _t=_t, get=_t.get):
-        id=_id()
-        t=get(id)
-        if t is None: _t[id]=t=Transaction(id)
+    _t = {}
+    def get_transaction():
+        id = thread.get_ident()
+        t = _t.get(id)
+        if t is None:
+            _t[id] = t = Transaction(id)
         return t
 
-    def free_transaction(_id=thread.get_ident, _t=_t):
-        id=_id()
-        try: del _t[id]
-        except KeyError: pass
-
-    del thread
+    def free_transaction():
+        id = thread.get_ident()
+        try:
+            del _t[id]
+        except KeyError:
+            pass
 
-del _t
+__all__ = ["Transaction", "get_transaction", "free_transaction"]