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

Fix the AttributeError message for deletion of a missing attribute #88023

Closed
maggyero mannequin opened this issue Apr 15, 2021 · 2 comments
Closed

Fix the AttributeError message for deletion of a missing attribute #88023

maggyero mannequin opened this issue Apr 15, 2021 · 2 comments
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@maggyero
Copy link
Mannequin

maggyero mannequin commented Apr 15, 2021

BPO 43857
Nosy @rhettinger, @pablogsal, @maggyero
PRs
  • bpo-43857: Improve the AttributeError message when deleting a missing attribute #25424
  • 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 = None
    closed_at = None
    created_at = <Date 2021-04-15.16:31:31.975>
    labels = ['interpreter-core', 'type-feature', '3.8', '3.9', '3.10']
    title = 'Fix the AttributeError message for deletion of a missing attribute'
    updated_at = <Date 2021-04-18.05:53:26.612>
    user = 'https://github.com/maggyero'

    bugs.python.org fields:

    activity = <Date 2021-04-18.05:53:26.612>
    actor = 'rhettinger'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2021-04-15.16:31:31.975>
    creator = 'maggyero'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43857
    keywords = ['patch']
    message_count = 1.0
    messages = ['391140']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'pablogsal', 'maggyero']
    pr_nums = ['25424']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue43857'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @maggyero
    Copy link
    Mannequin Author

    maggyero mannequin commented Apr 15, 2021

    Compare the AttributeError messages in this interactive Python session:

    >>> class A:
    ...     y = 0
    ...     __slots__ = ('z',)
    ... 
    >>> A().x
    […]
    AttributeError: 'A' object has no attribute 'x'
    >>> A().x = 1
    […]
    AttributeError: 'A' object has no attribute 'x'
    >>> del A().x
    […]
    AttributeError: 'A' object has no attribute 'x'
    >>> A().y
    0
    >>> A().y = 2
    […]
    AttributeError: 'A' object attribute 'y' is read-only
    >>> del A().y
    […]
    AttributeError: 'A' object attribute 'y' is read-only
    >>> A().z
    […]
    AttributeError: z
    >>> A().z = 3
    >>> del A().z
    […]
    AttributeError: z
    with the `AttributeError` messages in that one:
    
    >>> class B: pass
    ... 
    >>> B().x
    […]
    AttributeError: 'B' object has no attribute 'x'
    >>> B().x = 1
    >>> del B().x
    […]
    AttributeError: x

    The message AttributeError: x from del B().x does not feel right. I expect this message to be the same as the message AttributeError: 'B' object has no attribute 'x' from B().x, since in both cases the object B() has no attribute 'x'.

    I have checked on PyPy 7.3.3 (Python 3.7.9) and it uses the expected message AttributeError: 'B' object has no attribute 'x' from B().x for del B().x. So this confirms my initial suspicion.

    ----

    In CPython, the AttributeError message for attribute retrieval is implemented here (except for slot retrieval):

        if (!suppress) {
            PyErr_Format(PyExc_AttributeError,
                         "'%.50s' object has no attribute '%U'",
                         tp->tp_name, name);
        }

    And the AttributeError messages for attribute assignment and deletion are implemented here (except for slot deletion):

        if (dict == NULL) {
            dictptr = _PyObject_GetDictPtr(obj);
            if (dictptr == NULL) {
                if (descr == NULL) {
                    PyErr_Format(PyExc_AttributeError,
                                 "'%.100s' object has no attribute '%U'",
                                 tp->tp_name, name);
                }
                else {
                    PyErr_Format(PyExc_AttributeError,
                                 "'%.50s' object attribute '%U' is read-only",
                                 tp->tp_name, name);
                }
                goto done;
            }
            res = _PyObjectDict_SetItem(tp, dictptr, name, value);
        }
        else {
            Py_INCREF(dict);
            if (value == NULL)
                res = PyDict_DelItem(dict, name);
            else
                res = PyDict_SetItem(dict, name, value);
            Py_DECREF(dict);
        }
        if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
            PyErr_SetObject(PyExc_AttributeError, name);

    So it is the last line PyErr_SetObject(PyExc_AttributeError, name); that would be updated. Note that _PyObjectDict_SetItem delegates to PyDict_DelItem (if value is NULL) or PyDict_SetItem (if value is not NULL), and that only PyDict_DelItem can set an exception PyExc_KeyError, which is then translated to an exception PyExc_AttributeError in the last line.

    @maggyero maggyero mannequin added 3.7 (EOL) end of life 3.10 only security fixes 3.8 only security fixes 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Apr 15, 2021
    @terryjreedy terryjreedy removed the 3.7 (EOL) end of life label Apr 16, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @furkanonder
    Copy link
    Sponsor Contributor

    @JelleZijlstra Issue seems to be resolved. We can close the issue.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants