when did transaction lose the ability to be usable as a context manager?
Hi All, I used to do this:
import transaction with transaction: ... print 'hello' ... Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: __exit__
When did that stop working and what should I now do instead? cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk
2013-02-06 09:57:14 Chris Withers napisał(a):
I used to do this:
import transaction with transaction: ... print 'hello' ... Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: __exit__
When did that stop working and what should I now do instead?
http://bugs.python.org/issue9220 -- Arfrever Frehtes Taifersar Arahesis
Chris Withers <chris@simplistix.co.uk> wrote:
Hi All,
I used to do this:
import transaction with transaction: ... print 'hello' ... Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: __exit__
When did that stop working and what should I now do instead?
cheers,
Chris
You can use pure hackery to work around this: ----- t.py ----- def __enter__(*args): print 'enter', args def __exit__(*args): print 'exit', args def fixup(): import sys, types self = sys.modules[__name__] mymod = type(__name__, (types.ModuleType,), globals()) sys.modules[__name__] = mymod(__name__) fixup() ---------------- ------ t1.py --- #!python2.7 import t with t: print "transaction" ---------------- and the output is: C:\Temp>t1.py enter (<module 't' (built-in)>,) transaction exit (<module 't' (built-in)>, None, None, None) This is hackery though, and watch out for globals as the globals() dict known to the functions in t.py is no longer the same dict visible to modules that import it so changes to one won't be visible in the other and any functions in the module will behave like methods (so you have to give them a 'self' parameter). Much better just to change the code to define an object and import that from the module.
On Feb 6, 2013, at 11:54 , Duncan Booth <duncan.booth@suttoncourtenay.org.uk> wrote:
Chris Withers <chris@simplistix.co.uk> wrote:
Hi All,
I used to do this:
import transaction with transaction: ... print 'hello' ... Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: __exit__
When did that stop working and what should I now do instead?
cheers,
Chris
You can use pure hackery to work around this: ----- t.py ----- def __enter__(*args): print 'enter', args
def __exit__(*args): print 'exit', args
def fixup(): import sys, types self = sys.modules[__name__] mymod = type(__name__, (types.ModuleType,), globals()) sys.modules[__name__] = mymod(__name__) fixup()
You can also use the transaction manager directly instead of the bound methods transaction lifts out of it: import transaction with transaction.manager: pass Wichert.
On 06/02/2013 11:47, Wichert Akkerman wrote:
You can also use the transaction manager directly instead of the bound methods transaction lifts out of it:
import transaction with transaction.manager: pass
yeah, this is what I'll do. Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk
participants (4)
-
Arfrever Frehtes Taifersar Arahesis -
Chris Withers -
Duncan Booth -
Wichert Akkerman