Issue 12022: AttributeError should report the same details when raised by lookup_special() as when raised in the REPL (original) (raw)

Created on 2011-05-06 20:07 by dholth, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26809 merged serhiy.storchaka,2021-06-20 08:21
Messages (9)
msg135365 - (view) Author: Daniel Holth (dholth) * Date: 2011-05-06 20:07
"How much do we care about special method lookup?" Python recently bypasses __getattr__ entirely when looking up context managers. http://mail.python.org/pipermail/python-dev/2009-May/089535.html Could this be the reason that ZODB's transaction module, which attempts to be a context manager by declaring manager = ThreadTransactionManager() __enter__ = manager.get __exit__ = manager.__exit__ Does not work in Python 2.7.1 on Ubuntu 11.04 or RHEL5? Frustratingly, the exception is no more specific than an AttributeError, even though hasattr(transaction, '__exit__')? I would prefer to never get AttributeError: transaction.__exit__ when hasattr(transaction, '__exit__') as I find that to be very confusing. Maybe the interpreter could raise SpecialAttributeError('transaction.__exit__ is not sufficiently special') instead. http://svn.zope.org/repos/main/transaction/trunk/transaction/__init__.py
msg135388 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-05-06 23:50
Yes, that's why. I suggest you appeal to python-ideas about the new exception.
msg135484 - (view) Author: Daniel Holth (dholth) * Date: 2011-05-07 17:19
Python should explain AttributeError in the same way when it's raised by the interpreter. The with: statement below should raise the second AttributeError, not the first. import transaction with transaction: pass >>> AttributeError: __exit__ import sys sys.__exit__ >>> AttributeError: 'module' object has no attribute '__exit__'
msg135491 - (view) Author: Daniel Holth (dholth) * Date: 2011-05-07 17:42
Thank you Benjamin for following up on this issue
msg136640 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-05-23 14:09
> hasattr(transaction, '__exit__') http://docs.python.org/dev/reference/datamodel#special-method-names explains that magic methods are looked up on the class, not on the instances. There’s a lot of code out there that erroneously checks for __len__ or __call__ on instances, and this is the second time to my knowledge that a project abused a module-level __enter__ function.
msg354977 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-19 21:38
Would not be better to change an AttributeError to TypeError? Seems this is the only place in the core when missing special method causes an AttributeError and not TypeError.
msg396148 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-19 18:58
I've reproduced this on 3.11 (though the AttributeError is now on __enter__ and not __exit__ as was the case in ): >>> import transaction >>> with transaction: pass ... Traceback (most recent call last): File "", line 1, in AttributeError: __enter__ >>> import sys >>> sys.__exit__ Traceback (most recent call last): File "", line 1, in AttributeError: module 'sys' has no attribute '__exit__' >>>
msg396165 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-20 08:23
PR 26809 makes "with" and "async with" raising TypeError instead of AttributeError for wrong types.
msg396707 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-29 08:27
New changeset 20a88004bae8ead66a205a125e1fe979376fc3ea by Serhiy Storchaka in branch 'main': bpo-12022: Change error type for bad objects in "with" and "async with" (GH-26809) https://github.com/python/cpython/commit/20a88004bae8ead66a205a125e1fe979376fc3ea
History
Date User Action Args
2022-04-11 14:57:17 admin set github: 56231
2021-06-29 08:29:06 serhiy.storchaka set status: open -> closedresolution: fixedstage: patch review -> resolved
2021-06-29 08:27:28 serhiy.storchaka set messages: +
2021-06-21 07:53:47 serhiy.storchaka link issue44471 dependencies
2021-06-20 08:23:21 serhiy.storchaka set messages: +
2021-06-20 08:21:44 serhiy.storchaka set nosy: + gvanrossum
2021-06-20 08:21:10 serhiy.storchaka set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest25391>
2021-06-19 20:27:09 serhiy.storchaka set assignee: serhiy.storchaka
2021-06-19 18:58:23 iritkatriel set nosy: + iritkatrielmessages: + versions: + Python 3.11, - Python 2.7
2019-10-19 21:38:43 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2011-05-23 14:09:44 eric.araujo set nosy: + eric.araujomessages: +
2011-05-08 06:33:55 daniel.urban set nosy: + daniel.urban
2011-05-07 17:42:19 dholth set messages: +
2011-05-07 17:19:47 dholth set messages: + title: improve special method lookup error message -> AttributeError should report the same details when raised by lookup_special() as when raised in the REPL
2011-05-07 16:13:43 benjamin.peterson set status: closed -> openresolution: not a bug -> (no value)title: 'transaction' module-as-context-manager thwarted by Python 2.7.1 -> improve special method lookup error message
2011-05-06 23:50:49 benjamin.peterson set status: open -> closednosy: + benjamin.petersonmessages: + resolution: not a bug
2011-05-06 20:07:21 dholth create