Blob Blame Raw
From 0c77d4ddfc8c0f720e4d0b7b87621968fa15bc70 Mon Sep 17 00:00:00 2001
From: Nils Philippsen <nils@redhat.com>
Date: Fri, 26 Aug 2011 17:20:29 +0200
Subject: [PATCH] fix content manager interface

Python doesn't allow module-level context managers because the magic
methods __enter__(), __exit__() are looked up on the class (i.e.
module), not on instances. See this for details:

http://bugs.python.org/issue12022#msg136640
---
 transaction/__init__.py           |    3 +--
 transaction/tests/convenience.txt |   33 ++++++---------------------------
 2 files changed, 7 insertions(+), 29 deletions(-)

diff --git a/transaction/__init__.py b/transaction/__init__.py
index 6e2720c..d33af4d 100644
--- a/transaction/__init__.py
+++ b/transaction/__init__.py
@@ -21,11 +21,10 @@ from transaction._manager import TransactionManager
 from transaction._manager import ThreadTransactionManager
 
 manager = ThreadTransactionManager()
-get = __enter__ = manager.get
+get = manager.get
 begin = manager.begin
 commit = manager.commit
 abort = manager.abort
-__exit__ = manager.__exit__
 doom = manager.doom
 isDoomed = manager.isDoomed
 savepoint = manager.savepoint
diff --git a/transaction/tests/convenience.txt b/transaction/tests/convenience.txt
index 050323b..76c4549 100644
--- a/transaction/tests/convenience.txt
+++ b/transaction/tests/convenience.txt
@@ -15,29 +15,8 @@ We can now use the with statement to define transaction boundaries.
     >>> dm.keys()
     []
 
-We can use the transaction module directly:
-
-    >>> with transaction as t:
-    ...     dm['z'] = 1
-    ...     t.note('test 1')
-
-    >>> dm['z']
-    1
-
-    >>> dm.last_note
-    'test 1'
-
-    >>> with transaction:
-    ...     dm['z'] = 2
-    ...     xxx
-    Traceback (most recent call last):
-    ...
-    NameError: name 'xxx' is not defined
-
-    >>> dm['z']
-    1
-
-We can use it with a manager:
+We must use it with a manager, using the module directly doesn't work in recent
+Python versions, see http://bugs.python.org/issue12022#msg136640:
 
     >>> with transaction.manager as t:
     ...     dm['z'] = 3
@@ -49,7 +28,7 @@ We can use it with a manager:
     >>> dm.last_note
     'test 3'
 
-    >>> with transaction:
+    >>> with transaction.manager:
     ...     dm['z'] = 4
     ...     xxx
     Traceback (most recent call last):
@@ -68,7 +47,7 @@ overcome transient failures.  This typically looks something like::
 
     for i in range(3):
         try:
-           with transaction:
+           with transaction.manager:
                ... some something ...
         except SomeTransientException:
            contine
@@ -82,7 +61,7 @@ we'll use a contrived example:
 
 
     >>> ntry = 0
-    >>> with transaction:
+    >>> with transaction.manager:
     ...      dm['ntry'] = 0
 
     >>> import transaction.interfaces
@@ -165,7 +144,7 @@ attempted again.
 
     >>> ntry = 0
     >>> dm2 = DM()
-    >>> with transaction:
+    >>> with transaction.manager:
     ...     dm2['ntry'] = 0
     >>> for attempt in transaction.manager.attempts():
     ...     with attempt:
-- 
1.7.6