Re: when did transaction lose the ability to be usable as a context manager?

Wichert Akkerman <[email protected]>
Newsgroups gmane.comp.web.zope.devel,gmane.comp.web.zope.zodb
Message-ID <[email protected]>
On Feb 6, 2013, at 11:54 , Duncan Booth <[email protected]> wrote:

> Chris Withers <[email protected]> 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.
_______________________________________________
Zope-Dev maillist  -  [email protected]
https://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists -
 https://mail.zope.org/mailman/listinfo/zope-announce
 https://mail.zope.org/mailman/listinfo/zope )
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.