[Zodb-checkins] CVS: ZODB3/ZODB - Transaction.py:1.53

Tim Peters tim.one at comcast.net
Thu Oct 2 18:48:08 EDT 2003


Update of /cvs-repository/ZODB3/ZODB
In directory cvs.zope.org:/tmp/cvs-serv30459/ZODB

Modified Files:
	Transaction.py 
Log Message:
get_transaction(), free_transaction():  Recent removal of default arg
tricks left name thread undefined at runtime.  Repair that.  Plus
add comments, use meaningful variable names, stop pretending we can run
on a system without threads, and don't use "id" (a builtin name) as a
local vrbl name.


=== ZODB3/ZODB/Transaction.py 1.52 => 1.53 ===
--- ZODB3/ZODB/Transaction.py:1.52	Thu Oct  2 18:11:28 2003
+++ ZODB3/ZODB/Transaction.py	Thu Oct  2 18:48:07 2003
@@ -16,6 +16,7 @@
 $Id$
 """
 import sys
+from thread import get_ident as _get_ident
 
 from zLOG import LOG, ERROR, PANIC, INFO, BLATHER, WARNING
 from ZODB.POSException import ConflictError, TransactionError
@@ -453,39 +454,26 @@
 ############################################################################
 # install get_transaction:
 
-try:
-    import thread
+# Map thread ident to its Transaction instance.
+_tid2tran = {}
 
-except:
-    _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(id, None)
-        if t is None:
-            _t[id] = t = Transaction(id)
-        return t
-
-    def free_transaction():
-        id = thread.get_ident()
-        try:
-            del _t[id]
-        except KeyError:
-            pass
-
-    del thread
-
-del _t
+# Get Transaction associated with current thread; if none, create a
+# new Transaction and return it.
+def get_transaction():
+    tid = _get_ident()
+    result = _tid2tran.get(tid)
+    if result is None:
+        _tid2tran[tid] = result = Transaction(tid)
+    return result
+
+# Forget whatever Transaction (if any) is associated with current thread.
+def free_transaction():
+    tid = _get_ident()
+    try:
+        del _tid2tran[tid]
+    except KeyError:
+        pass
 
 import __builtin__
-__builtin__.get_transaction=get_transaction
+__builtin__.get_transaction = get_transaction
 del __builtin__




More information about the Zodb-checkins mailing list