Skip to content
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

Closed
dholth mannequin opened this issue May 6, 2011 · 9 comments
Assignees
Labels
3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@dholth
Copy link
Mannequin

dholth mannequin commented May 6, 2011

BPO 12022
Nosy @gvanrossum, @benjaminp, @merwok, @durban, @dholth, @serhiy-storchaka, @iritkatriel
PRs
  • bpo-12022: Change error type for bad objects in "with" and "async with" #26809
  • 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:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2021-06-29.08:29:06.821>
    created_at = <Date 2011-05-06.20:07:21.538>
    labels = ['interpreter-core', 'type-bug', '3.11']
    title = 'AttributeError should report the same details when raised by lookup_special() as when raised in the REPL'
    updated_at = <Date 2021-06-29.08:29:06.820>
    user = 'https://github.com/dholth'

    bugs.python.org fields:

    activity = <Date 2021-06-29.08:29:06.820>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2021-06-29.08:29:06.821>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2011-05-06.20:07:21.538>
    creator = 'dholth'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 12022
    keywords = ['patch']
    message_count = 9.0
    messages = ['135365', '135388', '135484', '135491', '136640', '354977', '396148', '396165', '396707']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'benjamin.peterson', 'eric.araujo', 'daniel.urban', 'dholth', 'serhiy.storchaka', 'iritkatriel']
    pr_nums = ['26809']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue12022'
    versions = ['Python 3.11']

    @dholth
    Copy link
    Mannequin Author

    dholth mannequin commented May 6, 2011

    "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

    @dholth dholth mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels May 6, 2011
    @benjaminp
    Copy link
    Contributor

    Yes, that's why. I suggest you appeal to python-ideas about the new exception.

    @benjaminp benjaminp changed the title 'transaction' module-as-context-manager thwarted by Python 2.7.1 improve special method lookup error message May 7, 2011
    @benjaminp benjaminp reopened this May 7, 2011
    @benjaminp benjaminp removed the invalid label May 7, 2011
    @dholth
    Copy link
    Mannequin Author

    dholth mannequin commented May 7, 2011

    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__'

    @dholth dholth mannequin changed the title improve special method lookup error message AttributeError should report the same details when raised by lookup_special() as when raised in the REPL May 7, 2011
    @dholth
    Copy link
    Mannequin Author

    dholth mannequin commented May 7, 2011

    Thank you Benjamin for following up on this issue

    @merwok
    Copy link
    Member

    merwok commented May 23, 2011

    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.

    @serhiy-storchaka
    Copy link
    Member

    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.

    @iritkatriel
    Copy link
    Member

    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__'
    >>>

    @iritkatriel iritkatriel added the 3.11 only security fixes label Jun 19, 2021
    @serhiy-storchaka serhiy-storchaka self-assigned this Jun 19, 2021
    @serhiy-storchaka
    Copy link
    Member

    PR 26809 makes "with" and "async with" raising TypeError instead of AttributeError for wrong types.

    @serhiy-storchaka
    Copy link
    Member

    New changeset 20a8800 by Serhiy Storchaka in branch 'main':
    bpo-12022: Change error type for bad objects in "with" and "async with" (GH-26809)
    20a8800

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants