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

Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr #88190

Closed
maggyero mannequin opened this issue May 3, 2021 · 5 comments
Labels
3.11 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 May 3, 2021

BPO 44024
Nosy @serhiy-storchaka, @maggyero
PRs
  • bpo-44024: Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr #25863
  • 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 = <Date 2022-01-18.20:47:30.233>
    created_at = <Date 2021-05-03.22:50:44.403>
    labels = ['interpreter-core', 'type-feature', '3.11']
    title = 'Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr'
    updated_at = <Date 2022-01-18.21:41:03.567>
    user = 'https://github.com/maggyero'

    bugs.python.org fields:

    activity = <Date 2022-01-18.21:41:03.567>
    actor = 'maggyero'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-18.20:47:30.233>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2021-05-03.22:50:44.403>
    creator = 'maggyero'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44024
    keywords = ['patch']
    message_count = 5.0
    messages = ['392843', '409745', '410889', '410890', '410899']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'maggyero']
    pr_nums = ['25863']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44024'
    versions = ['Python 3.11']

    @maggyero
    Copy link
    Mannequin Author

    maggyero mannequin commented May 3, 2021

    Problem
    -------

    Actual behaviour:

    >>> getattr('foobar', 123)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: getattr(): attribute name must be string
    >>> hasattr('foobar', 123)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: hasattr(): attribute name must be string

    Expected behaviour:

    >>> getattr('foobar', 123)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: attribute name must be string, not 'int'
    >>> hasattr('foobar', 123)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: attribute name must be string, not 'int'

    Motivation:

    >>> setattr('foobar', 123, 'baz')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: attribute name must be string, not 'int'
    >>> delattr('foobar', 123)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: attribute name must be string, not 'int'

    Solution
    --------

    In the function builtin_getattr defined in Python/bltinmodule.c, we remove the following lines:

    if (!PyUnicode_Check(name)) {
            PyErr_SetString(PyExc_TypeError,
                            "getattr(): attribute name must be string");
            return NULL;
        }

    because the expected TypeError message is already implemented in the subsequent call to the functions _PyObject_LookupAttr and PyObject_GetAttr defined in Objects/object.c:

    PyObject *
    PyObject_GetAttr(PyObject *v, PyObject *name)
    {
        PyTypeObject *tp = Py_TYPE(v);
        if (!PyUnicode_Check(name)) {
            PyErr_Format(PyExc_TypeError,
                         "attribute name must be string, not '%.200s'",
                         Py_TYPE(name)->tp_name);
            return NULL;
        }
    
    […]
    
    int
    _PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
    {
        PyTypeObject *tp = Py_TYPE(v);
    
        if (!PyUnicode_Check(name)) {
            PyErr_Format(PyExc_TypeError,
                         "attribute name must be string, not '%.200s'",
                         Py_TYPE(name)->tp_name);
            *result = NULL;
            return -1;
        }
    
    […]

    Likewise, in the function builtin_hasattr_impl defined in Python/bltinmodule.c, we remove the following lines:

    if (!PyUnicode_Check(name)) {
            PyErr_SetString(PyExc_TypeError,
                            "hasattr(): attribute name must be string");
            return NULL;
        }

    because the expected TypeError message is already implemented in the subsequent call to the function _PyObject_LookupAttr defined in Objects/object.c.

    @maggyero maggyero mannequin added 3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 3, 2021
    @maggyero maggyero mannequin changed the title Use common TypeError message for built-in functions getattr and hasattr Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattr May 4, 2021
    @maggyero maggyero mannequin changed the title Use common TypeError message for built-in functions getattr and hasattr Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattr May 4, 2021
    @maggyero maggyero mannequin changed the title Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattr Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr May 5, 2021
    @maggyero maggyero mannequin changed the title Improve the error message for non-string second arguments passed to the built-in functions getattr and hasattr Improve the TypeError message for non-string second arguments passed to the built-in functions getattr and hasattr May 5, 2021
    @wyz23x2 wyz23x2 mannequin removed 3.7 (EOL) end of life 3.8 only security fixes labels May 22, 2021
    @merwok merwok added type-feature A feature request or enhancement and removed 3.9 only security fixes 3.10 only security fixes type-bug An unexpected behavior, bug, or error labels Jan 5, 2022
    @merwok merwok added the type-feature A feature request or enhancement label Jan 5, 2022
    @serhiy-storchaka
    Copy link
    Member

    Good point. That code was originally added in bpo-420304 because every exception raised in PyObject_GetAttr() (including a TypeError for non-string name) was silenced in hasattr() and 3-argument getattr(). It was changed in Python 3, so this code duplication is no longer needed.

    The name of the function was added in error messages in a9b9c9f. Now it will gone. It is a minor regression, but I think that it is fine. Not all error messages contain a function name. In this case the context is clear from the traceback, and the error is not specific to these two functions, setattr() and delattr() raise the same error.

    @serhiy-storchaka
    Copy link
    Member

    New changeset 16bf9bd by Géry Ogam in branch 'main':
    bpo-44024: Improve the TypeError message in getattr and hasattr (GH-25863)
    16bf9bd

    @serhiy-storchaka
    Copy link
    Member

    Thank you for your contribution Géry.

    @maggyero
    Copy link
    Mannequin Author

    maggyero mannequin commented Jan 18, 2022

    Thanks for the review Serhiy.

    @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-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants