-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AttributeError should report the same details when raised by lookup_special() as when raised in the REPL #56231
Comments
"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 |
Yes, that's why. I suggest you appeal to python-ideas about the new exception. |
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__' |
Thank you Benjamin for following up on this issue |
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. |
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. |
I've reproduced this on 3.11 (though the AttributeError is now on __enter__ and not __exit__ as was the case in msg135484): >>> import transaction
>>> with transaction: pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __enter__
>>> import sys
>>> sys.__exit__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'sys' has no attribute '__exit__'
>>> |
PR 26809 makes "with" and "async with" raising TypeError instead of AttributeError for wrong types. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: