This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients benjamin.peterson, ellingtonjp, ncoghlan, rhettinger, xiang.zhang
Date 2016-05-24.06:10:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1464070223.14.0.961241488189.issue27100@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2016-05-24 06:10:23ncoghlansetrecipients: + ncoghlan, rhettinger, benjamin.peterson, xiang.zhang, ellingtonjp
2016-05-24 06:10:23ncoghlansetmessageid: <1464070223.14.0.961241488189.issue27100@psf.upfronthosting.co.za>
2016-05-24 06:10:23ncoghlanlinkissue27100 messages
2016-05-24 06:10:22ncoghlancreate