Skip to content

AttributeError should report the same details when raised by lookup_special() as when raised in the REPL #56231

Closed

Description

@dholth
mannequin
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']

    Activity

    1. dholth commented on May 6, 2011

      dholthmannequin
      MannequinAuthor

      "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

    2. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      type-bugAn unexpected behavior, bug, or error
      on May 6, 2011
    3. benjaminp commented on May 6, 2011

      @benjaminp
      Contributor

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

    4. changed the title [-]'transaction' module-as-context-manager thwarted by Python 2.7.1[/-] [+]improve special method lookup error message[/+] on May 7, 2011
    5. dholth commented on May 7, 2011

      dholthmannequin
      MannequinAuthor

      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__'
    6. 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[/+] on May 7, 2011
    7. dholth commented on May 7, 2011

      dholthmannequin
      MannequinAuthor

      Thank you Benjamin for following up on this issue

    8. merwok commented on May 23, 2011

      @merwok
      Member

      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.

    9. serhiy-storchaka commented on Oct 19, 2019

      @serhiy-storchaka
      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.

    10. iritkatriel commented on Jun 19, 2021

      @iritkatriel
      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__'
      >>>
    11. serhiy-storchaka commented on Jun 20, 2021

      @serhiy-storchaka
      Member

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

    12. serhiy-storchaka commented on Jun 29, 2021

      @serhiy-storchaka
      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

    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    3.11only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions