[Python-ideas] AttributeError: __exit__

Daniel Holth dholth at gmail.com
Sat May 7 02:16:21 CEST 2011


I just learned about Python internals from The ZODB transaction module. In
Python < 2.7, the module works as a transaction manager. More or less:

manager = Foo()
__exit__ = manager.__exit__
__enter__ = manager.__enter__

After Python 2.7, it doesn't work.

import transaction
with transaction: pass
>>> AttributeError: __exit__

It should be obvious to even the most casual observer that the exception is
because, after Python 2.7, the with: statement has its own opcode that
bypasses transaction.__getattribute__('__exit__') ->
transaction.__dict__['__exit__']. Instead, CPython calls special_lookup(),
looks for __exit__ on the module type, not the instance, doesn't find it,
and raises the AttributeError.

Instead,

import sys
sys.__exit__
>>> AttributeError: 'module' object has no attribute '__exit__'

The interpreter should at least explain the AttributeError in the same way
as it does when the user triggers it directly.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110506/32d1aef9/attachment.html>


More information about the Python-ideas mailing list