[Zope-dev] when did transaction lose the ability to be usable as a context manager?

Wichert Akkerman wichert at wiggy.net
Wed Feb 6 11:47:40 UTC 2013


On Feb 6, 2013, at 11:54 , Duncan Booth <duncan.booth at suttoncourtenay.org.uk> wrote:

> Chris Withers <chris at 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.


More information about the Zope-Dev mailing list