Nick Coghlan added the comment:

As Jonathan notes, the cause for confusion here is that we currently retrieve 
the attributes in the opposite order from the way we execute them, prompting 
the "Why is it looking for __exit__ first?", "Oh, __enter__ must be optional, 
so it didn't care that it was missing" train of thought.

If I recall correctly, rather than stemming from any particular design 
principle, the current behaviour is an artifact of the initial implementation 
that didn't use any dedicated opcodes: since CPython uses a stack-based VM, we 
needed __enter__ to be above __exit__ on the stack, and the most efficient way 
to achieve that was to do the attribute lookups in the reverse order of 
invocation.

When SETUP_WITH was introduced, that original method lookup ordering (and 
associated error message precedence) was preserved, even though the technical 
rationale for that ordering no longer applied.

Given that, I don't see any problem with switching the lookup order now, and I 
agree that should be less confusing, given that the most obvious explanation 
for getting an error message about __enter__ when both are missing is that the 
interpreter tries to call __enter__ before it tries to do anything with 
__exit__. (That intuitive explanation still isn't 100% accurate, but it's 
closer than thinking __enter__ is optional)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27100>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to