[Python-ideas] AttributeError: __exit__

Guido van Rossum guido at python.org
Sat May 7 02:41:33 CEST 2011


Please file a bug.

On May 6, 2011 5:17 PM, "Daniel Holth" <dholth at gmail.com> wrote:
>
> 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.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110506/36bae8f3/attachment.html>


More information about the Python-ideas mailing list